]> git.kaiwu.me - njs.git/commitdiff
Global variables may be accessed incorrectly by nested functions.
authorIgor Sysoev <igor@sysoev.ru>
Tue, 4 Apr 2017 07:47:02 +0000 (10:47 +0300)
committerIgor Sysoev <igor@sysoev.ru>
Tue, 4 Apr 2017 07:47:02 +0000 (10:47 +0300)
njs/njs_variable.c
njs/test/njs_unit_test.c

index f6e05591b897c94e7e8ff2450cc06f577c14fade..03a28e018ac9992136d0dedccf33466a3adc3317 100644 (file)
@@ -322,7 +322,11 @@ njs_variable_get(njs_vm_t *vm, njs_parser_node_t *node)
         goto not_found;
     }
 
-    n = (node->scope->nesting != vs.scope->nesting);
+    n = 0;
+
+    if (vs.scope->type > NJS_SCOPE_GLOBAL) {
+        n = (node->scope->nesting != vs.scope->nesting);
+    }
 
     var = vs.variable;
     index = var->index;
index 81f729c5d5cc6765c9a86df5892a4cdb0ef92222..4a5aaa7cdf638325961ec9890dde5dbe08a5f876 100644 (file)
@@ -4319,6 +4319,10 @@ static njs_unit_test_t  njs_test[] =
                  "var y = f(); y()"),
       nxt_string("6") },
 
+    { nxt_string("var x; var y = 4;"
+                 "function f() { function h() { x = 3; return y; } }"),
+      nxt_string("undefined") },
+
     /* Recursive fibonacci. */
 
     { nxt_string("function fibo(n) {"