Skip to content

Commit cf9f7a6

Browse files
committed
validate_integer, validate_numeric: explicitely reject hashes in arrays
Without this patch, Ruby 1.8's Hash#to_s behaviour causes [{1=>2}] to be treated as "12" when validating values.
1 parent b409018 commit cf9f7a6

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

lib/puppet/parser/functions/validate_integer.rb

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ module Puppet::Parser::Functions
109109
# check every element of the array
110110
input.each_with_index do |arg, pos|
111111
begin
112+
raise TypeError if arg.is_a?(Hash)
112113
arg = Integer(arg.to_s)
113114
validator.call(arg)
114115
rescue TypeError, ArgumentError

lib/puppet/parser/functions/validate_numeric.rb

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ module Puppet::Parser::Functions
7171
# check every element of the array
7272
input.each_with_index do |arg, pos|
7373
begin
74+
raise TypeError if arg.is_a?(Hash)
7475
arg = Float(arg.to_s)
7576
validator.call(arg)
7677
rescue TypeError, ArgumentError

spec/functions/validate_integer_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer or Array/)
6363
end
6464

65+
it "should not compile when a Hash is passed as Array" do
66+
Puppet[:code] = "validate_integer([{ 1 => 2 }])"
67+
expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
68+
end
69+
6570
it "should not compile when an explicitly undef variable is passed" do
6671
Puppet[:code] = <<-'ENDofPUPPETcode'
6772
$foo = undef

spec/functions/validate_numeric_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric or Array/)
6363
end
6464

65+
it "should not compile when a Hash is passed in an Array" do
66+
Puppet[:code] = "validate_numeric([{ 1 => 2 }])"
67+
expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
68+
end
69+
6570
it "should not compile when an explicitly undef variable is passed" do
6671
Puppet[:code] = <<-'ENDofPUPPETcode'
6772
$foo = undef

0 commit comments

Comments
 (0)