]> git.kaiwu.me - njs.git/commitdiff
Core: fix buffering of 0xFF in the UTF-16 decoder
authorDmitry Volyntsev <xeioex@nginx.com>
Sat, 14 Nov 2026 02:32:10 +0000 (18:32 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Sat, 14 Nov 2026 05:31:46 +0000 (21:31 -0800)
Previously, the UTF-16 decoder stored a buffered byte plus one in the
eight-bit ctx->upper field, using zero to mean no buffered byte.  A
byte of 0xFF overflowed to zero and was lost.  This corrupted code
units split across buffers and units reread after an unpaired leading
surrogate.

Now ctx->need indicates whether a byte is buffered, and ctx->upper
stores the byte unchanged.  All byte values can be preserved without
changing the decode context layout or its streaming contract.

The issue was introduced in 29271d0a (0.4.3).

src/njs_utf16.c
src/njs_utf16.h
src/test/unicode_unit_test.c

index b47fbf50bfcba936a3f38c74f07f945a89b4c472..a3085e5251b120839ad675cb2b2dce25945d796d 100644 (file)
@@ -54,9 +54,9 @@ njs_utf16_decode(njs_unicode_decode_t *ctx, const u_char **start,
     uint32_t  unit;
     unsigned  lead;
 
-    if (ctx->upper != 0x00) {
-        lead = ctx->upper - 0x01;
-        ctx->upper = 0x00;
+    if (ctx->need != 0x00) {
+        lead = ctx->upper;
+        ctx->need = 0x00;
 
         goto lead_state;
     }
@@ -66,7 +66,8 @@ pair_state:
     lead = *(*start)++;
 
     if (*start >= end) {
-        ctx->upper = lead + 0x01;
+        ctx->upper = lead;
+        ctx->need = 0x01;
         return NJS_UNICODE_CONTINUE;
     }
 
@@ -89,7 +90,8 @@ lead_state:
 
         (*start)--;
 
-        ctx->upper = lead + 0x01;
+        ctx->upper = lead;
+        ctx->need = 0x01;
         ctx->codepoint = 0x00;
 
         return NJS_UNICODE_ERROR;
index b5675586a4d213eb0c4ede3747e45d1e6b412b7d..d05a817525b0e11a1fdc9c20763db5d3e3ef0213 100644 (file)
@@ -17,6 +17,7 @@ NJS_EXPORT uint32_t njs_utf16_decode(njs_unicode_decode_t *ctx,
 njs_inline void
 njs_utf16_decode_init(njs_unicode_decode_t *ctx)
 {
+    ctx->need = 0x00;
     ctx->upper = 0x00;
     ctx->codepoint = 0x00;
 }
index 35416ea1ba602add75b1e2205476acee7bac9ee8..6d8540ec7cc6ca2764666129331347df7e23c2a0 100644 (file)
@@ -211,7 +211,7 @@ utf16_unit_test()
 {
     int8_t                length, length_to;
     u_char                *start, *end, *end_to;
-    uint32_t              cp, i;
+    uint32_t              cp, i, j;
     njs_unicode_decode_t  ctx;
     u_char                buf[8], to[4];
 
@@ -263,6 +263,43 @@ utf16_unit_test()
             njs_printf("utf16 test decode-encode failed\n");
             return NJS_ERROR;
         }
+
+        njs_utf16_decode_init(&ctx);
+        start = buf;
+
+        for (j = 0; j < (uint32_t) length; j++) {
+            cp = njs_utf16_decode(&ctx, (const u_char **) &start, start + 1);
+
+            if (cp != (j + 1 == (uint32_t) length ? i : NJS_UNICODE_CONTINUE)) {
+                njs_printf("utf16 test byte decoding failed for %05uXD\n", i);
+                return NJS_ERROR;
+            }
+        }
+
+        start = to;
+        (void) njs_utf16_encode(0xD800, &start, end_to);
+
+        njs_utf16_decode_init(&ctx);
+        start = to;
+
+        cp = njs_utf16_decode(&ctx, (const u_char **) &start, to + 2);
+        if (cp != NJS_UNICODE_CONTINUE) {
+            njs_printf("utf16 test pending surrogate failed\n");
+            return NJS_ERROR;
+        }
+
+        start = buf;
+        cp = njs_utf16_decode(&ctx, (const u_char **) &start, buf + length);
+        if (cp != NJS_UNICODE_ERROR || start != buf + 1) {
+            njs_printf("utf16 test unpaired surrogate failed for %05uXD\n", i);
+            return NJS_ERROR;
+        }
+
+        cp = njs_utf16_decode(&ctx, (const u_char **) &start, buf + length);
+        if (cp != i || start != buf + length) {
+            njs_printf("utf16 test rereading code point failed for %05uXD\n", i);
+            return NJS_ERROR;
+        }
     }
 
     /* Surrogate pair. */