]> git.kaiwu.me - njs.git/commitdiff
Fixed a func instance dumping with "name" as getter.
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 28 Feb 2023 06:14:36 +0000 (22:14 -0800)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 28 Feb 2023 06:14:36 +0000 (22:14 -0800)
After njs_value_property() call the value argument might be
overwritten.

This is similar to #615.

src/njs_json.c
src/test/njs_unit_test.c

index 6a37f76de57233a558e9add9eed0205b6db69a96..e71afdf145af06d394e362eb2cfb27906b5eca3d 100644 (file)
@@ -1771,6 +1771,13 @@ njs_dump_terminal(njs_json_stringify_t *stringify, njs_chb_t *chain,
         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 *stringify, njs_chb_t *chain,
 
         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) {
index 4e537917f1bff64b706e2c12744314922927f3c7..46a0ea8893ee79efc970fc409e39d8ff534792f1 100644 (file)
@@ -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),