From 353ca9e8602d6437eea099f08da8918b7836f6f7 Mon Sep 17 00:00:00 2001 From: Matt Basta Date: Mon, 18 Jul 2011 10:49:01 -0700 Subject: [PATCH] Fixed implicit global pollution problem (bug 666165) --- .gitignore | 1 + tests/test_js_pollution.py | 7 ++++--- validator/testcases/javascript/actions.py | 19 ++++++------------- validator/testcases/javascript/traverser.py | 6 +++--- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index ace9eae..664c669 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ .DS_Store .coverage .figleaf +*.swp /validator/constants_local.py /src /extras/jslibs diff --git a/tests/test_js_pollution.py b/tests/test_js_pollution.py index bea301a..6016eaf 100644 --- a/tests/test_js_pollution.py +++ b/tests/test_js_pollution.py @@ -64,9 +64,10 @@ def test_pollution_implicit_from_fun(): assert not _do_test_raw(""" (function() { - a = "foo"; - b = "foo"; - c = "foo"; + var a = "foo"; + var b = "foo"; + var c = "foo"; + var d = "foo"; })() """, ignore_pollution=False).failed() diff --git a/validator/testcases/javascript/actions.py b/validator/testcases/javascript/actions.py index d6ae2a1..bc86f73 100644 --- a/validator/testcases/javascript/actions.py +++ b/validator/testcases/javascript/actions.py @@ -206,10 +206,9 @@ def _define_var(traverser, node): # TODO : Test to make sure len(values) == len(vars) for value in declaration["init"]["elements"]: if vars[0]: - traverser._set_variable( - vars[0], - JSWrapper(traverser._traverse_node(value), - traverser=traverser)) + traverser._set_variable(vars[0], JSWrapper( + traverser._traverse_node(value), + traverser=traverser)) vars = vars[1:] # Pop off the first value # It's being assigned by a JSArray (presumably) @@ -218,9 +217,7 @@ def _define_var(traverser, node): assigner = traverser._traverse_node(declaration["init"]) for value in assigner.value.elements: if vars[0]: - traverser._set_variable( - vars[0], - value) + traverser._set_variable(vars[0], value) vars = vars[1:] elif declaration["id"]["type"] == "ObjectPattern": @@ -242,16 +239,12 @@ def _define_var(traverser, node): init_obj.get(traverser, prop_name)) elif prop["value"]["type"] == "ObjectPattern": - _proc_objpattern(init_obj.get(traverser, - prop_name), + _proc_objpattern(init_obj.get(traverser, prop_name), prop["value"]["properties"]) if init is not None: _proc_objpattern(init_obj=init, properties=declaration["id"]["properties"]) - #else: - # FIXME : Is this even supposed to be possible? - # No idea why this is happening, else: var_name = declaration["id"]["name"] @@ -496,7 +489,7 @@ def _expr_assignment(traverser, node): "readonly" in global_dict else True) - traverser._set_variable(node_left["name"], right) + traverser._set_variable(node_left["name"], right, glob=True) elif node_left["type"] == "MemberExpression": member_object = trace_member(traverser, node_left["object"]) global_overwrite = (member_object.is_global and diff --git a/validator/testcases/javascript/traverser.py b/validator/testcases/javascript/traverser.py index 89a8db0..8db90b4 100644 --- a/validator/testcases/javascript/traverser.py +++ b/validator/testcases/javascript/traverser.py @@ -409,9 +409,9 @@ class Traverser: self._debug("SETTING_OBJECT") - context_count = len(self.contexts) - for i in range(context_count): - context = self.contexts[context_count - i - 1] + i = 0 + for context in reversed(self.contexts): + i += 1 if context.has_var(name): self._debug("SETTING_OBJECT>>LOCAL>>%d" % i) context.set(name, value)