[njs] Fixed Array.prototype.lastIndexOf() with undefined arguments.

Dmitry Volyntsev xeioex at nginx.com
Thu Jul 11 18:30:11 UTC 2019


details:   https://hg.nginx.org/njs/rev/71e6a00bd921
branches:  
changeset: 1042:71e6a00bd921
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Jul 11 21:29:59 2019 +0300
description:
Fixed Array.prototype.lastIndexOf() with undefined arguments.

diffstat:

 njs/njs_array.c          |  52 +++++++++++++++++++++++++----------------------
 njs/test/njs_unit_test.c |  15 +++++++++++++
 2 files changed, 43 insertions(+), 24 deletions(-)

diffs (116 lines):

diff -r ea18aa7693da -r 71e6a00bd921 njs/njs_array.c
--- a/njs/njs_array.c	Thu Jul 11 16:10:33 2019 +0300
+++ b/njs/njs_array.c	Thu Jul 11 21:29:59 2019 +0300
@@ -1280,52 +1280,56 @@ static njs_ret_t
 njs_array_prototype_last_index_of(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused)
 {
-    nxt_int_t    i, n, index, length;
-    njs_value_t  *value, *start;
-    njs_array_t  *array;
+    nxt_int_t          k, n, index, length;
+    njs_value_t        *start;
+    njs_array_t        *array;
+    const njs_value_t  *this, *value;
 
     index = -1;
 
-    if (nargs < 2 || !njs_is_array(&args[0])) {
+    this = njs_arg(args, nargs, 0);
+
+    if (!njs_is_array(this)) {
         goto done;
     }
 
-    array = njs_array(&args[0]);
+    array = njs_array(this);
     length = array->length;
 
     if (length == 0) {
         goto done;
     }
 
-    i = length - 1;
-
     if (nargs > 2) {
-        n = njs_number(&args[2]);
-
-        if (n < 0) {
-            i = n + length;
-
-            if (i < 0) {
-                goto done;
-            }
-
-        } else if (n < length) {
-            i = n;
+        n = njs_primitive_value_to_integer(njs_argument(args, 2));
+
+    } else {
+        n = length - 1;
+    }
+
+    if (n >= 0) {
+        k = nxt_min(n, length - 1);
+
+    } else {
+        k = n + length;
+
+        if (k < 0) {
+            goto done;
         }
     }
 
-    value = &args[1];
+    value = njs_arg(args, nargs, 1);
     start = array->start;
 
     do {
-        if (njs_values_strict_equal(value, &start[i])) {
-            index = i;
+        if (njs_values_strict_equal(value, &start[k])) {
+            index = k;
             break;
         }
 
-        i--;
-
-    } while (i >= 0);
+        k--;
+
+    } while (k >= 0);
 
 done:
 
diff -r ea18aa7693da -r 71e6a00bd921 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu Jul 11 16:10:33 2019 +0300
+++ b/njs/test/njs_unit_test.c	Thu Jul 11 21:29:59 2019 +0300
@@ -3810,6 +3810,12 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("[].lastIndexOf(1, -1)"),
       nxt_string("-1") },
 
+    { nxt_string("[undefined].lastIndexOf()"),
+      nxt_string("0") },
+
+    { nxt_string("[undefined].lastIndexOf(undefined)"),
+      nxt_string("0") },
+
     { nxt_string("var a = [1,2,3,4]; a.lastIndexOf()"),
       nxt_string("-1") },
 
@@ -3837,6 +3843,15 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var a = [1,2,3,4,3,4]; a.lastIndexOf(3, -10)"),
       nxt_string("-1") },
 
+    { nxt_string("[1,2,1].lastIndexOf(2,undefined)"),
+      nxt_string("-1") },
+
+    { nxt_string("[1,2,1].lastIndexOf(1,undefined)"),
+      nxt_string("0") },
+
+    { nxt_string("[1,2,1].lastIndexOf(1)"),
+      nxt_string("2") },
+
     { nxt_string("[1,2,3,4].includes()"),
       nxt_string("false") },
 


More information about the nginx-devel mailing list