]> git.kaiwu.me - njs.git/commitdiff
String: fix decoding of code units after an unpaired surrogate
authorbasavaraj-sm05 <basavaraj@digiscrypt.com>
Thu, 1 Oct 2026 21:28:25 +0000 (21:28 +0000)
committerDmitry Volyntsev <xeioexception@gmail.com>
Sat, 14 Nov 2026 05:31:46 +0000 (21:31 -0800)
Previously, String.fromCharCode() and String.fromCodePoint() decoded
each argument only once.  When a pending leading surrogate was not
followed by a trailing one, the UTF-16 decoder reported the unpaired
surrogate and left the current unit to be reread.  The buffer was then
overwritten by the next argument, losing or corrupting subsequent
characters.  For example, String.fromCharCode(0xD83D, 0x41, 0x42)
returned "\ufffd\u4241" instead of "\ufffdAB" on little-endian systems.

Now both the size calculation and the encoding pass drain each buffer
before proceeding to the next argument.  Unpaired surrogates are still
replaced with U+FFFD, preserving the existing string representation.

The issue was introduced in 98c26163 (0.4.3).

Co-authored-by: Dmitry Volyntsev <xeioex@nginx.com>
src/njs_string.c
src/test/njs_unit_test.c

index 9a601e26ccbbfd81fdc1a6ad12ea3267d7188543..d25a4684abf02b4ea6ccf2d2c25e760b271958ed 100644 (file)
@@ -1469,7 +1469,7 @@ njs_string_from_char_code(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t is_point, njs_value_t *retval)
 {
     double                num;
-    u_char                *p, *start, *end;
+    u_char                *p, *start, *end, *last;
     ssize_t               len;
     int32_t               code;
     uint32_t              cp;
@@ -1515,18 +1515,25 @@ njs_string_from_char_code(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         len = njs_utf16_encode(code, &start, end);
 
         start = buf;
-        cp = njs_utf16_decode(&ctx, (const u_char **) &start, start + len);
+        last = buf + len;
 
-        if (cp > NJS_UNICODE_MAX_CODEPOINT) {
-            if (cp == NJS_UNICODE_CONTINUE) {
-                continue;
+        /* An unpaired leading surrogate leaves a unit to be reread. */
+
+        do {
+            cp = njs_utf16_decode(&ctx, (const u_char **) &start, last);
+
+            if (cp > NJS_UNICODE_MAX_CODEPOINT) {
+                if (cp == NJS_UNICODE_CONTINUE) {
+                    break;
+                }
+
+                cp = NJS_UNICODE_REPLACEMENT;
             }
 
-            cp = NJS_UNICODE_REPLACEMENT;
-        }
+            size += njs_utf8_size(cp);
+            length++;
 
-        size += njs_utf8_size(cp);
-        length++;
+        } while (start < last);
     }
 
     if (cp == NJS_UNICODE_CONTINUE) {
@@ -1553,17 +1560,22 @@ njs_string_from_char_code(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         len = njs_utf16_encode(code, &start, end);
 
         start = buf;
-        cp = njs_utf16_decode(&ctx, (const u_char **) &start, start + len);
+        last = buf + len;
 
-        if (cp > NJS_UNICODE_MAX_CODEPOINT) {
-            if (cp == NJS_UNICODE_CONTINUE && i + 1 != nargs) {
-                continue;
+        do {
+            cp = njs_utf16_decode(&ctx, (const u_char **) &start, last);
+
+            if (cp > NJS_UNICODE_MAX_CODEPOINT) {
+                if (cp == NJS_UNICODE_CONTINUE && i + 1 != nargs) {
+                    break;
+                }
+
+                cp = NJS_UNICODE_REPLACEMENT;
             }
 
-            cp = NJS_UNICODE_REPLACEMENT;
-        }
+            p = njs_utf8_encode(p, cp);
 
-        p = njs_utf8_encode(p, cp);
+        } while (start < last);
     }
 
     return NJS_OK;
index 88c71ceb230e7b3bbd18f4187602d921cfd79070..acbcfb6ec4a48e42d9a4fb00ec964c2abc9b87ae 100644 (file)
@@ -9366,6 +9366,24 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("String.fromCharCode(0xD83D) + String.fromCharCode(0xDCA9)"),
       njs_str("��") },
 
+    { njs_str("String.fromCharCode(0xD83D, 0x41)"),
+      njs_str("�A") },
+
+    { njs_str("String.fromCharCode(0xD83D, 0x41, 0x42)"),
+      njs_str("�AB") },
+
+    { njs_str("String.fromCharCode(0xD83D, 0x41, 0x42).length"),
+      njs_str("3") },
+
+    { njs_str("String.fromCharCode(0xD83D, 0x41, 0xD83D)"),
+      njs_str("�A�") },
+
+    { njs_str("String.fromCharCode(0xD83D, 0xD83D)"),
+      njs_str("��") },
+
+    { njs_str("String.fromCharCode(0xD83D, 0xD83D, 0xDCA9)"),
+      njs_str("�💩") },
+
     { njs_str("String.fromCodePoint(65 + 65536)"),
       njs_str("𐁁") },
 
@@ -9381,6 +9399,18 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("String.fromCodePoint(0xD83D).length"),
       njs_str("1") },
 
+    { njs_str("String.fromCodePoint(0xD83D, 0x41, 0x42)"),
+      njs_str("�AB") },
+
+    { njs_str("String.fromCodePoint(0xD83D, 0x1F4A9)"),
+      njs_str("�💩") },
+
+    { njs_str("String.fromCodePoint(0xD83D, 0x1F4A9, 0x42)"),
+      njs_str("�💩B") },
+
+    { njs_str("String.fromCodePoint(0xD83D, 0x1F4A9, 0xD83D)"),
+      njs_str("�💩�") },
+
     { njs_str("String.fromCharCode(2**53 + 10)"),
       njs_str("\n") },