[njs] Fixed Buffer.concat() with subarrays.

Dmitry Volyntsev xeioex at nginx.com
Wed Jan 19 13:12:50 UTC 2022


details:   https://hg.nginx.org/njs/rev/30865ae17149
branches:  
changeset: 1810:30865ae17149
user:      Sylvain Etienne <s.etienne at trusted-objects.com>
date:      Tue Jan 18 08:37:09 2022 +0100
description:
Fixed Buffer.concat() with subarrays.

This closes #458 issue on Github.

diffstat:

 src/njs_buffer.c         |  12 +++++++-----
 src/test/njs_unit_test.c |   9 +++++++++
 2 files changed, 16 insertions(+), 5 deletions(-)

diffs (55 lines):

diff -r 1634e2b7a9e7 -r 30865ae17149 src/njs_buffer.c
--- a/src/njs_buffer.c	Tue Jan 18 15:36:40 2022 +0000
+++ b/src/njs_buffer.c	Tue Jan 18 08:37:09 2022 +0100
@@ -764,7 +764,7 @@ static njs_int_t
 njs_buffer_concat(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused)
 {
-    u_char             *p;
+    u_char             *p, *src;
     size_t             n;
     int64_t            i, len, list_len;
     njs_int_t          ret;
@@ -866,8 +866,9 @@ njs_buffer_concat(njs_vm_t *vm, njs_valu
         for (i = 0; len != 0 && i < list_len; i++) {
             arr = njs_typed_array(&array->start[i]);
             n = njs_min((size_t) len, arr->byte_length);
-
-            p = njs_cpymem(p, njs_typed_array_buffer(arr)->u.u8, n);
+            src = &njs_typed_array_buffer(arr)->u.u8[arr->offset];
+
+            p = njs_cpymem(p, src, n);
 
             len -= n;
         }
@@ -881,8 +882,9 @@ njs_buffer_concat(njs_vm_t *vm, njs_valu
 
             arr = njs_typed_array(&retval);
             n = njs_min((size_t) len, arr->byte_length);
-
-            p = njs_cpymem(p, njs_typed_array_buffer(arr)->u.u8, n);
+            src = &njs_typed_array_buffer(arr)->u.u8[arr->offset];
+
+            p = njs_cpymem(p, src, n);
 
             len -= n;
         }
diff -r 1634e2b7a9e7 -r 30865ae17149 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Tue Jan 18 15:36:40 2022 +0000
+++ b/src/test/njs_unit_test.c	Tue Jan 18 08:37:09 2022 +0100
@@ -20109,6 +20109,15 @@ static njs_unit_test_t  njs_buffer_modul
     { njs_str("Buffer.concat([new Uint8Array(2), new Uint8Array(1)], 6).fill('abc')"),
       njs_str("abcabc") },
 
+    { njs_str("Buffer.concat([Buffer.from('ABCD').slice(2,4), Buffer.from('ABCD').slice(0,2)])"),
+      njs_str("CDAB") },
+
+    { njs_str(njs_declare_sparse_array("list", 2)
+              "list[0] = Buffer.from('ABCD').slice(2,4);"
+              "list[1] = Buffer.from('ABCD').slice(0,2);"
+              "Buffer.concat(list);"),
+      njs_str("CDAB") },
+
     { njs_str(njs_declare_sparse_array("list", 2)
               "list[0] = new Uint8Array(2); list[1] = new Uint8Array(3);"
               "Buffer.concat(list).fill('ab');"),



More information about the nginx-devel mailing list