[njs] Global variables may be accessed incorrectly by nested functions.

Igor Sysoev igor at sysoev.ru
Tue Apr 4 10:24:40 UTC 2017


details:   http://hg.nginx.org/njs/rev/251aa4b128ea
branches:  
changeset: 333:251aa4b128ea
user:      Igor Sysoev <igor at sysoev.ru>
date:      Tue Apr 04 10:47:02 2017 +0300
description:
Global variables may be accessed incorrectly by nested functions.

diffstat:

 njs/njs_variable.c       |  6 +++++-
 njs/test/njs_unit_test.c |  4 ++++
 2 files changed, 9 insertions(+), 1 deletions(-)

diffs (30 lines):

diff -r ada17c8bdd5a -r 251aa4b128ea njs/njs_variable.c
--- a/njs/njs_variable.c	Tue Apr 04 10:47:12 2017 +0300
+++ b/njs/njs_variable.c	Tue Apr 04 10:47:02 2017 +0300
@@ -322,7 +322,11 @@ njs_variable_get(njs_vm_t *vm, njs_parse
         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;
diff -r ada17c8bdd5a -r 251aa4b128ea njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue Apr 04 10:47:12 2017 +0300
+++ b/njs/test/njs_unit_test.c	Tue Apr 04 10:47:02 2017 +0300
@@ -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) {"


More information about the nginx-devel mailing list