[njs] Fixed String.prototype.replace() when replacer throws exception.

Dmitry Volyntsev xeioex at nginx.com
Tue Jul 7 12:25:30 UTC 2020


details:   https://hg.nginx.org/njs/rev/e88aef77c653
branches:  
changeset: 1456:e88aef77c653
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Jul 07 12:23:46 2020 +0000
description:
Fixed String.prototype.replace() when replacer throws exception.

This issue was introduced in 1c729f765cfb.
Found by Clang static analyzer.

diffstat:

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

diffs (27 lines):

diff -r 7325a6df5036 -r e88aef77c653 src/njs_string.c
--- a/src/njs_string.c	Tue Jul 07 12:23:45 2020 +0000
+++ b/src/njs_string.c	Tue Jul 07 12:23:46 2020 +0000
@@ -3601,6 +3601,10 @@ njs_string_prototype_replace(njs_vm_t *v
                                 njs_value_arg(&njs_value_undefined),
                                 arguments, 3, &retval);
 
+        if (njs_slow_path(ret != NJS_OK)) {
+            return ret;
+        }
+
         ret = njs_value_to_string(vm, &retval, &retval);
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
diff -r 7325a6df5036 -r e88aef77c653 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Tue Jul 07 12:23:45 2020 +0000
+++ b/src/test/njs_unit_test.c	Tue Jul 07 12:23:46 2020 +0000
@@ -7523,6 +7523,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("'12345'.replace(3, () => ({toString: () => 'aaaa'}))"),
       njs_str("12aaaa45") },
 
+    { njs_str("'ABC'.replace('B', () => {throw 'OOps'})"),
+      njs_str("OOps") },
+
     { njs_str("'abc'.replace(/a/, 'X')"),
       njs_str("Xbc") },
 


More information about the nginx-devel mailing list