]> git.kaiwu.me - njs.git/commitdiff
Parser: fix overlong code point escape conversion
authorDean Chen <862469039@qq.com>
Sun, 15 Nov 2026 01:19:24 +0000 (06:19 +0500)
committerDmitry Volyntsev <xeioexception@gmail.com>
Mon, 16 Nov 2026 01:23:35 +0000 (17:23 -0800)
Previously, when computing the length of an escaped string, all hex
digits of a "\u{...}" escape were accumulated into a double and
converted to uint64_t before the existing six-digit limit was checked.
For overlong escapes, the parsed value could be outside the range of
uint64_t, including due to rounding with 16 digits.  The conversion is
undefined behavior and aborts builds with UBSan enabled.

Now at most seven digits are parsed, which still rejects longer escapes
with the same SyntaxError while keeping the value in range.

The issue was introduced in 7cff11dc ("Fixed large non-decimal literals."),
which changed njs_number_hex_parse() to return a double.

This closes #1158 issue on GitHub.

src/njs_parser.c
src/test/njs_unit_test.c

index 0fd63bbba355cb636de1d398fa1c710bea3d6b03..53c132d4ffd31f181ee295d82a8010481c30e29b 100644 (file)
@@ -8878,7 +8878,7 @@ njs_parser_escape_string_calc_length(njs_parser_t *parser,
 
                 src++;
                 hex_length = 0;
-                hex_end = end;
+                hex_end = (end - src > 7) ? src + 7 : end;
 
                 goto hex;
 
index acbcfb6ec4a48e42d9a4fb00ec964c2abc9b87ae..be382b3ef52103f9769eac5126489aa5e0f489bb 100644 (file)
@@ -8696,6 +8696,12 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("'\\u{a00000}'"),
       njs_str("SyntaxError: Invalid Unicode code point \"\\u{a00000}\"") },
 
+    { njs_str("'\\u{11111111111111110}'"),
+      njs_str("SyntaxError: Invalid Unicode code point \"\\u{11111111111111110}\"") },
+
+    { njs_str("`\\u{11111111111111110`"),
+      njs_str("SyntaxError: Invalid Unicode code point \"\\u{11111111111111110\"") },
+
     { njs_str("'\\x61'"),
       njs_str("a") },