]> git.kaiwu.me - njs.git/commitdiff
Fixed allocation of large array literals.
authorDmitry Volyntsev <xeioex@nginx.com>
Mon, 21 Feb 2022 16:53:16 +0000 (16:53 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Mon, 21 Feb 2022 16:53:16 +0000 (16:53 +0000)
Previously, allocation of large array literals may result in
null-pointer dereference. The reason is that njs_array_alloc() may
return a slow array when size is large enough, but the instruction
code assumes that array is always flat.

The fix is to check fast_array flag before accessing array->start.

This closes #473 issue on Github.

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

index ef0beb5e0b19e62de3fdf92a51762e40becdff3b..ec478659cb14aae03441f35a494d7d653ba778a8 100644 (file)
@@ -1055,14 +1055,16 @@ njs_vmcode_array(njs_vm_t *vm, u_char *pc)
 
         if (code->ctor) {
             /* Array of the form [,,,], [1,,]. */
-            value = array->start;
-            length = array->length;
-
-            do {
-                njs_set_invalid(value);
-                value++;
-                length--;
-            } while (length != 0);
+            if (array->object.fast_array) {
+                value = array->start;
+                length = array->length;
+
+                do {
+                    njs_set_invalid(value);
+                    value++;
+                    length--;
+                } while (length != 0);
+            }
 
         } else {
             /* Array of the form [], [,,1], [1,2,3]. */
index dc18a13b132bb161731131b554cddfb9b7f6202d..ef5b3ca452ce82445e01145cde9443fa4cd2907e 100644 (file)
@@ -13154,6 +13154,10 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("(new Function('return 5' + '** 1'.repeat(2**13)))()"),
       njs_str("5") },
 
+    { njs_str("var a = (new Function('return [' + ','.repeat(2**16) + ']'))();"
+                         "njs.dump(a)"),
+      njs_str("[<65536 empty items>]") },
+
     { njs_str("(new Function('var a = 7; return a' + '= a'.repeat(2**13)))()"),
       njs_str("7") },