[njs] Fixed njs_iterator_to_array() with sparse arrays.

Dmitry Volyntsev xeioex at nginx.com
Thu Jun 9 07:07:59 UTC 2022


details:   https://hg.nginx.org/njs/rev/0b75123c6ea4
branches:  
changeset: 1881:0b75123c6ea4
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Jun 08 22:58:34 2022 -0700
description:
Fixed njs_iterator_to_array() with sparse arrays.

This closes #524 issue on Github.

diffstat:

 src/njs_iterator.c       |  10 +++++-----
 src/test/njs_unit_test.c |   8 ++++++++
 2 files changed, 13 insertions(+), 5 deletions(-)

diffs (45 lines):

diff -r d4cdb9085e4d -r 0b75123c6ea4 src/njs_iterator.c
--- a/src/njs_iterator.c	Wed Jun 08 21:06:16 2022 -0700
+++ b/src/njs_iterator.c	Wed Jun 08 22:58:34 2022 -0700
@@ -686,7 +686,8 @@ njs_iterator_to_array(njs_vm_t *vm, njs_
         return NULL;
     }
 
-    args.data = njs_array_alloc(vm, 1, length, 0);
+    args.data = njs_array_alloc(vm, 0, 0,
+                                njs_min(length, NJS_ARRAY_LARGE_OBJECT_LENGTH));
     if (njs_slow_path(args.data == NULL)) {
         return NULL;
     }
@@ -708,10 +709,9 @@ static njs_int_t
 njs_iterator_to_array_handler(njs_vm_t *vm, njs_iterator_args_t *args,
     njs_value_t *value, int64_t index)
 {
-    njs_array_t  *array;
+    njs_value_t  array;
 
-    array = args->data;
-    array->start[index] = *value;
+    njs_set_array(&array, args->data);
 
-    return NJS_OK;
+    return njs_value_property_i64_set(vm, &array, index, value);
 }
diff -r d4cdb9085e4d -r 0b75123c6ea4 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Wed Jun 08 21:06:16 2022 -0700
+++ b/src/test/njs_unit_test.c	Wed Jun 08 22:58:34 2022 -0700
@@ -11299,6 +11299,14 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("let e = AggregateError([1, 2, 3], 'm'); e"),
       njs_str("AggregateError: m") },
 
+    { njs_str("var v = Object.defineProperty([], 1025, {get: () => 1});"
+              "AggregateError(v).errors[23]"),
+      njs_str("undefined") },
+
+    { njs_str("var v = Object.defineProperty([], 2**20, {get: () => 1});"
+              "AggregateError(v).errors[2**19]"),
+      njs_str("undefined") },
+
     /* Memory object is immutable. */
 
     { njs_str("var e = MemoryError('e'); e.name = 'E'"),



More information about the nginx-devel mailing list