[njs] Fixed String.prototype.replace() for byte strings with regex arg.

Alexander Borisov alexander.borisov at nginx.com
Tue Jul 16 14:32:51 UTC 2019


details:   https://hg.nginx.org/njs/rev/9e6098cb3b2e
branches:  
changeset: 1052:9e6098cb3b2e
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Tue Jul 16 17:32:10 2019 +0300
description:
Fixed String.prototype.replace() for byte strings with regex arg.

diffstat:

 njs/njs_string.c         |  3 ++-
 njs/test/njs_unit_test.c |  3 +++
 2 files changed, 5 insertions(+), 1 deletions(-)

diffs (26 lines):

diff -r f98e8a101b02 -r 9e6098cb3b2e njs/njs_string.c
--- a/njs/njs_string.c	Tue Jul 16 12:57:29 2019 +0300
+++ b/njs/njs_string.c	Tue Jul 16 17:32:10 2019 +0300
@@ -3160,7 +3160,8 @@ njs_string_replace_regexp(njs_vm_t *vm, 
             start = r->part[0].start;
 
             if (start < end) {
-                p = (u_char *) nxt_utf8_next(start, end);
+                p = (r->utf8 != NJS_STRING_BYTE)
+                    ? (u_char *) nxt_utf8_next(start, end) : start + 1;
 
                 r->part[1].start = start;
                 r->part[1].size = p - start;
diff -r f98e8a101b02 -r 9e6098cb3b2e njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue Jul 16 12:57:29 2019 +0300
+++ b/njs/test/njs_unit_test.c	Tue Jul 16 17:32:10 2019 +0300
@@ -5681,6 +5681,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("typeof '0'.replace(/^/g, '$0')"),
       nxt_string("string") },
 
+    { nxt_string("typeof String.bytesFrom(Array(15).fill(0xE3)).replace(/^/g, 1)"),
+      nxt_string("string") },
+
     { nxt_string("/]/"),
       nxt_string("/\\]/") },
 


More information about the nginx-devel mailing list