]> git.kaiwu.me - njs.git/commitdiff
Fixed Array.prototype.lastIndexOf() with unicode string as "this".
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 27 Apr 2022 23:31:00 +0000 (16:31 -0700)
committerDmitry Volyntsev <xeioex@nginx.com>
Wed, 27 Apr 2022 23:31:00 +0000 (16:31 -0700)
Previously, when lastIndexOf() was called with unicode string as "this"
argument and a negative "fromIndex" argument null-pointer dererence
might occur because njs_string_offset() was called with invalid index
value whereas njs_string_offset() should always be called with valid
index argument.

The fix is to verify that from index is valid.

This closes #482 issue on Github.

src/njs_iterator.c
src/test/njs_unit_test.c

index 90c3046fbbae52888144e482d186c18d19dddc6b..043e4483c7f5848bf65d5d6b24c62c9d0b5e4b24 100644 (file)
@@ -560,11 +560,14 @@ njs_object_iterate_reverse(njs_vm_t *vm, njs_iterator_args_t *args,
         } else {
             /* UTF-8 string. */
 
-            p = njs_string_offset(string_prop.start, end, from);
-            p = njs_utf8_next(p, end);
-
+            p = NULL;
             i = from + 1;
 
+            if (i > to) {
+                p = njs_string_offset(string_prop.start, end, from);
+                p = njs_utf8_next(p, end);
+            }
+
             while (i-- > to) {
                 pos = njs_utf8_prev(p);
 
index def152aa8e3fca2f86e16d3946b117bb5729a571..0b73c77b36683298f30d9a07898fbca775f8c9e5 100644 (file)
@@ -5103,6 +5103,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("Array.prototype.lastIndexOf.call({0:'undefined', length:0}, 'undefined')"),
       njs_str("-1") },
 
+    { njs_str("[1,0,-1,-2].map(v => Array.prototype.lastIndexOf.call('Ф', 'Ф', v))"),
+      njs_str("0,0,0,-1") },
+
     { njs_str("[''].lastIndexOf.call('00000000000000000000000000000а00')"),
       njs_str("-1") },