[njs] Fixed Array.prototype.slice() for UTF8-invalid byte strings.

Dmitry Volyntsev xeioex at nginx.com
Fri May 17 14:53:54 UTC 2019


details:   https://hg.nginx.org/njs/rev/9af8e1b5f3c1
branches:  
changeset: 968:9af8e1b5f3c1
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri May 17 17:52:30 2019 +0300
description:
Fixed Array.prototype.slice() for UTF8-invalid byte strings.

This closes #160 issue on Github.

diffstat:

 njs/njs_array.c          |  31 ++++++++++++++++++-------------
 njs/test/njs_unit_test.c |   3 +++
 2 files changed, 21 insertions(+), 13 deletions(-)

diffs (66 lines):

diff -r 58de3054b28a -r 9af8e1b5f3c1 njs/njs_array.c
--- a/njs/njs_array.c	Tue May 14 13:00:44 2019 +0300
+++ b/njs/njs_array.c	Fri May 17 17:52:30 2019 +0300
@@ -577,7 +577,7 @@ njs_array_prototype_slice_copy(njs_vm_t 
 {
     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 
 
             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 {
diff -r 58de3054b28a -r 9af8e1b5f3c1 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue May 14 13:00:44 2019 +0300
+++ b/njs/test/njs_unit_test.c	Fri May 17 17:52:30 2019 +0300
@@ -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,γ") },
 


More information about the nginx-devel mailing list