瀏覽代碼

LibRegex: Ensure escaped hexadecimals are exactly 2 digits in length

Timothy Flynn 4 年之前
父節點
當前提交
0c8f2f5aca
共有 2 個文件被更改,包括 6 次插入2 次删除
  1. 4 0
      Tests/LibRegex/Regex.cpp
  2. 2 2
      Userland/Libraries/LibRegex/RegexParser.cpp

+ 4 - 0
Tests/LibRegex/Regex.cpp

@@ -499,6 +499,10 @@ TEST_CASE(ECMA262_parse)
         { "\\/" },                                         // #4189
         { "\\/" },                                         // #4189
         { ",/=-:" },                                       // #4243
         { ",/=-:" },                                       // #4243
         { "\\x" },                                         // Even invalid escapes are allowed if ~unicode.
         { "\\x" },                                         // Even invalid escapes are allowed if ~unicode.
+        { "\\x1" },                                        // Even invalid escapes are allowed if ~unicode.
+        { "\\x1", regex::Error::InvalidPattern, regex::ECMAScriptFlags::Unicode },
+        { "\\x11" },
+        { "\\x11", regex::Error::NoError, regex::ECMAScriptFlags::Unicode },
         { "\\", regex::Error::InvalidTrailingEscape },
         { "\\", regex::Error::InvalidTrailingEscape },
         { "(?", regex::Error::InvalidCaptureGroup },
         { "(?", regex::Error::InvalidCaptureGroup },
         { "\\u1234", regex::Error::NoError, regex::ECMAScriptFlags::Unicode },
         { "\\u1234", regex::Error::NoError, regex::ECMAScriptFlags::Unicode },

+ 2 - 2
Userland/Libraries/LibRegex/RegexParser.cpp

@@ -1450,7 +1450,7 @@ bool ECMA262Parser::parse_atom_escape(ByteCode& stack, size_t& match_length_mini
 
 
     // HexEscape
     // HexEscape
     if (try_skip("x")) {
     if (try_skip("x")) {
-        if (auto hex_escape = read_digits(ReadDigitsInitialZeroState::Allow, true, 2); hex_escape.has_value()) {
+        if (auto hex_escape = read_digits(ReadDigitsInitialZeroState::Allow, true, 2, 2); hex_escape.has_value()) {
             match_length_minimum += 1;
             match_length_minimum += 1;
             stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)hex_escape.value() } });
             stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)hex_escape.value() } });
             return true;
             return true;
@@ -1802,7 +1802,7 @@ bool ECMA262Parser::parse_nonempty_class_ranges(Vector<CompareTypeAndValuePair>&
 
 
             // HexEscape
             // HexEscape
             if (try_skip("x")) {
             if (try_skip("x")) {
-                if (auto hex_escape = read_digits(ReadDigitsInitialZeroState::Allow, true, 2); hex_escape.has_value()) {
+                if (auto hex_escape = read_digits(ReadDigitsInitialZeroState::Allow, true, 2, 2); hex_escape.has_value()) {
                     return { CharClassRangeElement { .code_point = hex_escape.value(), .is_character_class = false } };
                     return { CharClassRangeElement { .code_point = hex_escape.value(), .is_character_class = false } };
                 } else if (!unicode) {
                 } else if (!unicode) {
                     // '\x' is allowed in non-unicode mode, just matches 'x'.
                     // '\x' is allowed in non-unicode mode, just matches 'x'.