Welcome! Log In Create A New Profile

Advanced

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

Dmitry Volyntsev
January 19, 2022 08:18AM
details: https://hg.nginx.org/njs/rev/30865ae17149
branches:
changeset: 1810:30865ae17149
user: Sylvain Etienne <s.etienne@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');"),
_______________________________________________
nginx-devel mailing list -- nginx-devel@nginx.org
To unsubscribe send an email to nginx-devel-leave@nginx.org
Subject Author Views Posted

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

Dmitry Volyntsev 172 January 19, 2022 08:18AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 111
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready