[njs] Fixed String.prototype.indexOf() for byte-strings.

Dmitry Volyntsev xeioex at nginx.com
Wed Aug 26 14:59:08 UTC 2020


details:   https://hg.nginx.org/njs/rev/657d446001da
branches:  
changeset: 1504:657d446001da
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Aug 26 14:56:47 2020 +0000
description:
Fixed String.prototype.indexOf() for byte-strings.

The issue was introduced in e8a941b394a3 (0.4.3).

This closes #335 issue on Github.

diffstat:

 src/njs_string.c         |  6 +++---
 src/test/njs_unit_test.c |  3 +++
 2 files changed, 6 insertions(+), 3 deletions(-)

diffs (38 lines):

diff -r a2d12799c9f7 -r 657d446001da src/njs_string.c
--- a/src/njs_string.c	Wed Aug 26 14:55:47 2020 +0000
+++ b/src/njs_string.c	Wed Aug 26 14:56:47 2020 +0000
@@ -1999,7 +1999,7 @@ static njs_int_t
 njs_string_prototype_index_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused)
 {
-    int64_t            from;
+    int64_t            from, length;
     njs_int_t          ret;
     njs_value_t        *this, *search, *pos, search_lvalue, pos_lvalue;
     njs_string_prop_t  string, s;
@@ -2029,10 +2029,10 @@ njs_string_prototype_index_of(njs_vm_t *
         return ret;
     }
 
-    (void) njs_string_prop(&string, this);
+    length = njs_string_prop(&string, this);
     (void) njs_string_prop(&s, search);
 
-    from = njs_min(njs_max(from, 0), (int64_t) string.length);
+    from = njs_min(njs_max(from, 0), length);
 
     njs_set_number(&vm->retval, njs_string_index_of(&string, &s, from));
 
diff -r a2d12799c9f7 -r 657d446001da src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Wed Aug 26 14:55:47 2020 +0000
+++ b/src/test/njs_unit_test.c	Wed Aug 26 14:56:47 2020 +0000
@@ -7649,6 +7649,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("var r = new String('undefined').indexOf(x); var x; r"),
       njs_str("0") },
 
+    { njs_str("'a a'.toUTF8().indexOf('a', 1)"),
+      njs_str("2") },
+
     { njs_str("'abc'.lastIndexOf('abcdef')"),
       njs_str("-1") },
 


More information about the nginx-devel mailing list