Skip to content

validate_integer, validate_numeric: explicitely reject hashes in arrays #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/puppet/parser/functions/validate_integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ module Puppet::Parser::Functions
# check every element of the array
input.each_with_index do |arg, pos|
begin
raise TypeError if arg.is_a?(Hash)
arg = Integer(arg.to_s)
validator.call(arg)
rescue TypeError, ArgumentError
Expand Down
1 change: 1 addition & 0 deletions lib/puppet/parser/functions/validate_numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module Puppet::Parser::Functions
# check every element of the array
input.each_with_index do |arg, pos|
begin
raise TypeError if arg.is_a?(Hash)
arg = Float(arg.to_s)
validator.call(arg)
rescue TypeError, ArgumentError
Expand Down
5 changes: 5 additions & 0 deletions spec/functions/validate_integer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer or Array/)
end

it "should not compile when a Hash is passed as Array" do
Puppet[:code] = "validate_integer([{ 1 => 2 }])"
expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
end

it "should not compile when an explicitly undef variable is passed" do
Puppet[:code] = <<-'ENDofPUPPETcode'
$foo = undef
Expand Down
5 changes: 5 additions & 0 deletions spec/functions/validate_numeric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric or Array/)
end

it "should not compile when a Hash is passed in an Array" do
Puppet[:code] = "validate_numeric([{ 1 => 2 }])"
expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
end

it "should not compile when an explicitly undef variable is passed" do
Puppet[:code] = <<-'ENDofPUPPETcode'
$foo = undef
Expand Down