From: Sylvain Etienne Date: Tue, 18 Jan 2022 07:37:09 +0000 (+0100) Subject: Fixed Buffer.concat() with subarrays. X-Git-Tag: 0.7.2~6 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=2ba9760170305660e375cd2a95dbeacc3c74fe1c;p=njs.git Fixed Buffer.concat() with subarrays. This closes #458 issue on Github. --- diff --git a/src/njs_buffer.c b/src/njs_buffer.c index 1f52603b..0473bcaf 100644 --- a/src/njs_buffer.c +++ b/src/njs_buffer.c @@ -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_value_t *args, njs_uint_t nargs, 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); + src = &njs_typed_array_buffer(arr)->u.u8[arr->offset]; - p = njs_cpymem(p, njs_typed_array_buffer(arr)->u.u8, n); + p = njs_cpymem(p, src, n); len -= n; } @@ -881,8 +882,9 @@ njs_buffer_concat(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, arr = njs_typed_array(&retval); n = njs_min((size_t) len, arr->byte_length); + src = &njs_typed_array_buffer(arr)->u.u8[arr->offset]; - p = njs_cpymem(p, njs_typed_array_buffer(arr)->u.u8, n); + p = njs_cpymem(p, src, n); len -= n; } diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index f2792ef9..f332999a 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -20109,6 +20109,15 @@ static njs_unit_test_t njs_buffer_module_test[] = { 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');"),