[njs] Fixed String.prototype.replaceAll() with zero length argument.

Dmitry Volyntsev xeioex at nginx.com
Thu May 23 00:27:02 UTC 2024


details:   https://hg.nginx.org/njs/rev/e496851c0fe7
branches:  
changeset: 2335:e496851c0fe7
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed May 22 17:26:08 2024 -0700
description:
Fixed String.prototype.replaceAll() with zero length argument.

This fixes #712 issue on Github.

diffstat:

 src/njs_string.c         |  15 ++++-----------
 src/njs_string.h         |   4 ++++
 src/test/njs_unit_test.c |   6 ++++++
 3 files changed, 14 insertions(+), 11 deletions(-)

diffs (76 lines):

diff -r bc09b884022d -r e496851c0fe7 src/njs_string.c
--- a/src/njs_string.c	Tue May 21 23:41:10 2024 -0700
+++ b/src/njs_string.c	Wed May 22 17:26:08 2024 -0700
@@ -1736,7 +1736,9 @@ njs_string_index_of(njs_string_prop_t *s
         } else {
             /* UTF-8 string. */
 
-            p = njs_string_utf8_offset(string->start, end, index);
+            p = (index < string->length)
+                    ? njs_string_utf8_offset(string->start, end, index)
+                    : end;
             end -= search->size - 1;
 
             while (p < end) {
@@ -3231,7 +3233,6 @@ njs_string_prototype_replace(njs_vm_t *v
     njs_int_t          ret;
     njs_str_t          str;
     njs_chb_t          chain;
-    njs_bool_t         is_ascii_string;
     njs_value_t        *this, *search, *replace;
     njs_value_t        search_lvalue, replace_lvalue, replacer, value,
                        arguments[3];
@@ -3378,7 +3379,6 @@ njs_string_prototype_replace(njs_vm_t *v
 
     p_start = string.start;
     increment = s.length != 0 ? s.length : 1;
-    is_ascii_string = njs_is_ascii_string(&string);
 
     do {
         if (func_replace == NULL) {
@@ -3405,14 +3405,7 @@ njs_string_prototype_replace(njs_vm_t *v
             }
         }
 
-        if (is_ascii_string) {
-            p = string.start + pos;
-
-        } else {
-            p = njs_string_utf8_offset(string.start, string.start + string.size,
-                                       pos);
-        }
-
+        p = njs_string_offset(&string, pos);
         (void) njs_string_prop(&ret_string, &value);
 
         njs_chb_append(&chain, p_start, p - p_start);
diff -r bc09b884022d -r e496851c0fe7 src/njs_string.h
--- a/src/njs_string.h	Tue May 21 23:41:10 2024 -0700
+++ b/src/njs_string.h	Wed May 22 17:26:08 2024 -0700
@@ -245,6 +245,10 @@ njs_string_offset(njs_string_prop_t *str
 
     /* UTF-8 string. */
 
+    if (index == (int64_t) string->length) {
+        return string->start + string->size;
+    }
+
     return njs_string_utf8_offset(string->start, string->start + string->size,
                                   index);
 }
diff -r bc09b884022d -r e496851c0fe7 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Tue May 21 23:41:10 2024 -0700
+++ b/src/test/njs_unit_test.c	Wed May 22 17:26:08 2024 -0700
@@ -8888,6 +8888,12 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("var r = 'αβγ'.replaceAll('', 'X'); [r, r.length]"),
       njs_str("XαXβXγX,7") },
 
+    { njs_str("var r = ''.replaceAll('', 'z'); [r, r.length]"),
+      njs_str("z,1") },
+
+    { njs_str("var r = 'α'.padStart(32).replaceAll('', 'z'); [r, r.length]"),
+      njs_str("z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z zαz,65") },
+
     { njs_str("'abc'.replace('b', (m, o, s) => `|${s}|${o}|${m}|`)"),
       njs_str("a|abc|1|b|c") },
 


More information about the nginx-devel mailing list