From 2788da9c51f690a77652c55e5ad8a4cccac40783 Mon Sep 17 00:00:00 2001 From: Florent Rougon Date: Fri, 28 Apr 2017 21:31:04 +0200 Subject: [PATCH] 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. --- simgear/misc/strutils.cxx | 2 +- simgear/misc/strutils_test.cxx | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/simgear/misc/strutils.cxx b/simgear/misc/strutils.cxx index 405a980c..d3a483db 100644 --- a/simgear/misc/strutils.cxx +++ b/simgear/misc/strutils.cxx @@ -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; diff --git a/simgear/misc/strutils_test.cxx b/simgear/misc/strutils_test.cxx index 073d474c..d2c19cd9 100644 --- a/simgear/misc/strutils_test.cxx +++ b/simgear/misc/strutils_test.cxx @@ -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()