]> git.kaiwu.me - njs.git/commitdiff
Fixed Array.prototype.slice() for UTF8-invalid byte strings.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 17 May 2019 14:52:30 +0000 (17:52 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 17 May 2019 14:52:30 +0000 (17:52 +0300)
This closes #160 issue on Github.

njs/njs_array.c
njs/test/njs_unit_test.c

index 514035ec0e63529b726b27569cc663a2d5217726..0659a46c6d67d81b97adb292f72fc1ff5240cd1e 100644 (file)
@@ -577,7 +577,7 @@ njs_array_prototype_slice_copy(njs_vm_t *vm, njs_value_t *this,
 {
     size_t             size;
     u_char             *dst;
-    uint32_t           n, len;
+    uint32_t           n;
     njs_ret_t          ret;
     njs_array_t        *array;
     njs_value_t        *value, name;
@@ -623,23 +623,28 @@ njs_array_prototype_slice_copy(njs_vm_t *vm, njs_value_t *this,
 
             if (string.length == 0) {
                 /* Byte string. */
-                len = 0;
+                do {
+                    value = &array->start[n++];
+                    dst = njs_string_short_start(value);
+                    *dst = *src++;
+                    njs_string_short_set(value, 1, 0);
+
+                    length--;
+                } while (length != 0);
 
             } else {
                 /* UTF-8 or ASCII string. */
-                len = 1;
+                do {
+                    value = &array->start[n++];
+                    dst = njs_string_short_start(value);
+                    dst = nxt_utf8_copy(dst, &src, end);
+                    size = dst - njs_string_short_start(value);
+                    njs_string_short_set(value, size, 1);
+
+                    length--;
+                } while (length != 0);
             }
 
-            do {
-                value = &array->start[n++];
-                dst = njs_string_short_start(value);
-                dst = nxt_utf8_copy(dst, &src, end);
-                size = dst - njs_string_short_start(value);
-                njs_string_short_set(value, size, len);
-
-                length--;
-            } while (length != 0);
-
         } else if (njs_is_object(this)) {
 
             do {
index 8f1411a5a59acc5d93f1f7c024501816a0509a62..72ef4ac82adcbdddbc6f14bc89ef154500c48e05 100644 (file)
@@ -3657,6 +3657,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Array.prototype.slice.call('αβZγ')"),
       nxt_string("α,β,Z,γ") },
 
+    { nxt_string("Array.prototype.slice.call(String.bytesFrom(Array(16).fill(0x9d)))[0].charCodeAt(0)"),
+      nxt_string("157") },
+
     { nxt_string("Array.prototype.slice.call('αβZγ', 1)"),
       nxt_string("β,Z,γ") },