[njs] Fixed a func instance dumping with "name" as getter.

Dmitry Volyntsev xeioex at nginx.com
Tue Feb 28 06:15:47 UTC 2023


details:   https://hg.nginx.org/njs/rev/293fe42c5e1c
branches:  
changeset: 2056:293fe42c5e1c
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Feb 27 22:14:36 2023 -0800
description:
Fixed a func instance dumping with "name" as getter.

After njs_value_property() call the value argument might be
overwritten.

This is similar to #615.

diffstat:

 src/njs_json.c           |  13 +++++++------
 src/test/njs_unit_test.c |   5 +++++
 2 files changed, 12 insertions(+), 6 deletions(-)

diffs (45 lines):

diff -r a79b6a75cfab -r 293fe42c5e1c src/njs_json.c
--- a/src/njs_json.c	Mon Feb 27 22:14:36 2023 -0800
+++ b/src/njs_json.c	Mon Feb 27 22:14:36 2023 -0800
@@ -1771,6 +1771,13 @@ njs_dump_terminal(njs_json_stringify_t *
         break;
 
     case NJS_FUNCTION:
+        if (njs_function(value)->native) {
+            str = njs_str_value("native");
+
+        } else {
+            str = njs_str_value("");
+        }
+
         ret = njs_value_property(stringify->vm, value,
                                  njs_value_arg(&name_string), &tag);
         if (njs_slow_path(ret == NJS_ERROR)) {
@@ -1779,12 +1786,6 @@ njs_dump_terminal(njs_json_stringify_t *
 
         if (njs_is_string(&tag)) {
             njs_string_get(&tag, &str);
-
-        } else if (njs_function(value)->native) {
-            str = njs_str_value("native");
-
-        } else {
-            str = njs_str_value("");
         }
 
         if (str.length != 0) {
diff -r a79b6a75cfab -r 293fe42c5e1c src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Mon Feb 27 22:14:36 2023 -0800
+++ b/src/test/njs_unit_test.c	Mon Feb 27 22:14:36 2023 -0800
@@ -22865,6 +22865,11 @@ static njs_unit_test_t  njs_shell_test[]
               "e" ENTER),
       njs_str("Error: foo") },
 
+    { njs_str("function f() {};" ENTER
+              "Object.defineProperty(f, 'name', { get() {void(0)} })" ENTER
+              "f" ENTER),
+      njs_str("[Function]") },
+
     /* Temporary indexes */
 
     { njs_str("var a = [1,2,3], i; for (i in a) {Object.seal({});}" ENTER),


More information about the nginx-devel mailing list