From 202204a82ac2160780e12a51ffa6b1357b3dea35 Mon Sep 17 00:00:00 2001 From: Christian Kaeser Date: Sun, 8 Nov 2015 18:36:16 +0100 Subject: [PATCH] Limit hexadecimal escape sequence length Helps with cases like "\xFFecho" by limiting the number of hex digits that will be parsed to maximum suitable for the char type. This rule differs from the C/C++ standard, but ChaiScript does not offer the same workaround options. Furthermore, without it having hexadecimal sequences longer than can fit into the char type is undefined behavior anyway. --- include/chaiscript/language/chaiscript_parser.hpp | 8 ++++++++ unittests/hex_escapes.chai | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index e50046d..6548521 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -981,6 +981,14 @@ namespace chaiscript if (is_hex_char) { hex_matches.push_back(t_char); + + if (hex_matches.size() == 2*sizeof(char_type)) { + // This rule differs from the C/C++ standard, but ChaiScript + // does not offer the same workaround options, and having + // hexadecimal sequences longer than can fit into the char + // type is undefined behavior anyway. + process_hex(); + } return; } else { process_hex(); diff --git a/unittests/hex_escapes.chai b/unittests/hex_escapes.chai index 14ec62e..fdd0a8a 100644 --- a/unittests/hex_escapes.chai +++ b/unittests/hex_escapes.chai @@ -1,6 +1,6 @@ assert_equal("\x39", "9") -assert_equal("\x039", "9") +assert_equal("\x39ec", "9ec") assert_equal("\x39g", "9g") assert_equal("b\x39g", "b9g") assert_equal("\x39\x38g", "98g")