Skip to content

Commit 5ac681f

Browse files
author
Daniel Pittman
committed
AST Scope variable names must be strings.
The Parser AST scope had an embedded assumption that variable names were always strings, but didn't actually enforce that. A consequence of this is that a couple of tests violated that agreement. This adds an internal error report when someone tries the wrong thing, and cleans up the handful of tests that failed. Signed-off-by: Daniel Pittman <daniel@puppetlabs.com>
1 parent 6712dd0 commit 5ac681f

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

lib/puppet/parser/compiler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ def initvars
436436
# Set the node's parameters into the top-scope as variables.
437437
def set_node_parameters
438438
node.parameters.each do |param, value|
439-
@topscope[param] = value
439+
@topscope[param.to_s] = value
440440
end
441441

442442
# These might be nil.

lib/puppet/parser/scope.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ def undef_as(x,v)
236236
end
237237

238238
def lookupvar(name, options = {})
239+
unless name.is_a? String
240+
raise Puppet::DevError, "Scope variable name is a #{name.class}, not a string"
241+
end
242+
239243
# Save the originating scope for the request
240244
options[:origin] = self unless options[:origin]
241245
table = ephemeral?(name) ? @ephemeral.last : @symtable
@@ -327,6 +331,10 @@ def define_settings(type, params)
327331
# It's preferred that you use self[]= instead of this; only use this
328332
# when you need to set options.
329333
def setvar(name, value, options = {})
334+
unless name.is_a? String
335+
raise Puppet::DevError, "Scope variable name is a #{name.class}, not a string"
336+
end
337+
330338
table = options[:ephemeral] ? @ephemeral.last : @symtable
331339
if table.include?(name)
332340
if options[:append]

spec/unit/parser/ast/vardef_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
describe "when evaluating" do
1212

1313
it "should evaluate arguments" do
14-
name = mock 'name'
15-
value = mock 'value'
14+
name = Puppet::Parser::AST::String.new :value => 'name'
15+
value = Puppet::Parser::AST::String.new :value => 'value'
1616

17-
name.expects(:safeevaluate).with(@scope)
18-
value.expects(:safeevaluate).with(@scope)
17+
name.expects(:safeevaluate).with(@scope).returns('name')
18+
value.expects(:safeevaluate).with(@scope).returns('value')
1919

2020
vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil, :line => nil
2121
vardef.evaluate(@scope)

spec/unit/parser/functions/fqdn_rand_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
node = Puppet::Node.new('localhost')
1111
compiler = Puppet::Parser::Compiler.new(node)
1212
@scope = Puppet::Parser::Scope.new(compiler)
13-
@scope[:fqdn] = "127.0.0.1"
13+
@scope["fqdn"] = "127.0.0.1"
1414
end
1515

1616
it "should exist" do

spec/unit/parser/scope_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
@scope.lookupvar("var").should == "yep"
130130
end
131131

132+
it "should fail if invoked with a non-string name" do
133+
expect { @scope[:foo] }.to raise_error Puppet::DevError
134+
expect { @scope[:foo] = 12 }.to raise_error Puppet::DevError
135+
end
136+
132137
it "should return nil for unset variables" do
133138
@scope["var"].should be_nil
134139
end

0 commit comments

Comments
 (0)