]> git.kaiwu.me - njs.git/commitdiff
Fix large array literal initialization
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 3 Sep 2026 00:39:41 +0000 (17:39 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 10 Sep 2026 03:11:04 +0000 (20:11 -0700)
Large literals switch to slow arrays above the fast-array capacity limit, but
the previous initialization path left their indexed values unset.  Initialize
slow arrays directly so literals above 32760 elements preserve their contents.

src/njs_vmcode.c
src/test/njs_unit_test.c

index c4bbeff61e97e0fe7ba0452956fd6ddcf98ff22c..6d51ab67cc99564ab4c9112d9b3a666bbaa50c70 100644 (file)
@@ -2075,6 +2075,17 @@ njs_vmcode_property_init(njs_vm_t *vm, njs_value_t *value, njs_value_t *key,
         index = (uint32_t) num;
         array = value->data.u.array;
 
+        if (njs_slow_path(!array->object.fast_array)) {
+            prop = njs_object_property_add(vm, value, njs_number_atom(index),
+                                           0);
+            if (njs_slow_path(prop == NULL)) {
+                return NJS_ERROR;
+            }
+
+            njs_value_assign(njs_prop_value(prop), init);
+            break;
+        }
+
         if (index >= array->length) {
             size = index - array->length;
 
index 3c01172d8a34da4bcfeefb37c8c1f606fe918346..c30604f4b58d88ad044dba0357668da1708ff782 100644 (file)
@@ -5001,6 +5001,11 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("[1,2].length"),
       njs_str("2") },
 
+    { njs_str("var src = 'return [' + '1,'.repeat(32760) + '1]';"
+              "var a = Function(src)();"
+              "[a.length, a[0], a[32760], Object.keys(a).length]"),
+      njs_str("32761,1,1,32761") },
+
     { njs_str("var a = [1,2]; a.length"),
       njs_str("2") },