strutils::unescape(): fix off-by-one error

An octal escape sequence in a string literal can't have more than 3
octal digits after the backslash. The previous code was using up to 4
digits per octal escape sequence.
This commit is contained in:
Florent Rougon
2017-04-28 21:31:04 +02:00
parent 4860a70443
commit 2788da9c51
2 changed files with 3 additions and 1 deletions

View File

@@ -674,7 +674,7 @@ std::string unescape(const char* s)
} else if (*s >= '0' && *s <= '7') {
int v = *s++ - '0';
for (int i = 0; i < 3 && *s >= '0' && *s <= '7'; i++, s++)
for (int i = 0; i < 2 && *s >= '0' && *s <= '7'; i++, s++)
v = v * 8 + *s - '0';
r += v;
continue;

View File

@@ -142,6 +142,8 @@ void test_split()
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");
}
void test_compare_versions()