]> git.kaiwu.me - njs.git/commitdiff
Fixed String.prototype.replace() when replacer throws exception.
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 7 Jul 2020 12:23:46 +0000 (12:23 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 7 Jul 2020 12:23:46 +0000 (12:23 +0000)
This issue was introduced in 1c729f765cfb.
Found by Clang static analyzer.

src/njs_string.c
src/test/njs_unit_test.c

index 260c9b02a66fa20052d242993cc2a45769b5110b..27f76f9f1ad9d24132bc817232b4369122489812 100644 (file)
@@ -3601,6 +3601,10 @@ njs_string_prototype_replace(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
                                 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;
index fe8ebd6b14119c0a84e0e12ebf519169c4294804..6146fdd852a8201870736ae678b4956b8ec6b8e3 100644 (file)
@@ -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") },