strutils::unescape(): fix handling of hexadecimal escape sequences

Hexadecimal escape sequences have no length limit and terminate at the
first character that is not a valid hexadecimal digit.
This commit is contained in:
Florent Rougon
2017-04-28 21:57:08 +02:00
parent 2788da9c51
commit e1c655c570
2 changed files with 7 additions and 1 deletions

View File

@@ -667,8 +667,9 @@ std::string unescape(const char* s)
if (!*++s)
break;
int v = 0;
for (int i = 0; i < 2 && isxdigit(*s); i++, s++)
for (/* empty */; isxdigit(*s); s++) {
v = v * 16 + (isdigit(*s) ? *s - '0' : 10 + tolower(*s) - 'a');
}
r += v;
continue;

View File

@@ -144,6 +144,11 @@ void test_unescape()
SG_CHECK_EQUAL(strutils::unescape("\\ \\n\\t\\x41\\117a"), " \n\tAOa");
// Two chars: '\033' (ESC) followed by '2'
SG_CHECK_EQUAL(strutils::unescape("\\0332"), "\0332");
// Hex escapes have no length limit and terminate at the first character
// that is not a valid hexadecimal digit.
SG_CHECK_EQUAL(strutils::unescape("\\x00020|"), " |");
SG_CHECK_EQUAL(strutils::unescape("\\xA"), "\n");
SG_CHECK_EQUAL(strutils::unescape("\\xA-"), "\n-");
}
void test_compare_versions()