From: basavaraj-sm05 Date: Thu, 1 Oct 2026 21:28:25 +0000 (+0000) Subject: String: fix decoding of code units after an unpaired surrogate X-Git-Url: https://git.kaiwu.me/%7Bsubstring-before(@doc,%20'.xml')%7D.html?a=commitdiff_plain;h=aeb779b531822b917984f5b741c918d4cc4d338a;p=njs.git String: fix decoding of code units after an unpaired surrogate 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 --- diff --git a/src/njs_string.c b/src/njs_string.c index 9a601e26..d25a4684 100644 --- a/src/njs_string.c +++ b/src/njs_string.c @@ -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; diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 88c71ceb..acbcfb6e 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -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") },