]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
validate_integer, validate_numeric: explicitely reject hashes in arrays
authorDavid Schmitt <david.schmitt@puppetlabs.com>
Wed, 27 May 2015 19:05:01 +0000 (20:05 +0100)
committerDavid Schmitt <david.schmitt@puppetlabs.com>
Wed, 27 May 2015 19:05:05 +0000 (20:05 +0100)
Without this patch, Ruby 1.8's Hash#to_s behaviour causes [{1=>2}] to be
treated as "12" when validating values.

lib/puppet/parser/functions/validate_integer.rb
lib/puppet/parser/functions/validate_numeric.rb
spec/functions/validate_integer_spec.rb
spec/functions/validate_numeric_spec.rb

index 995f8dbf8bbc45dea71f210c35488e0885912560..95da0c4effb28c140583d9e477f5ff73f199e42b 100644 (file)
@@ -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
index d2e4d16a08faa9a0bdf79373631bb8d879dada83..3a144434b9d77b95a0ba7dd0a13ed3d513e21ce5 100644 (file)
@@ -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
index 3865c4f586ef928d49e9f3def483038063428fef..e95da6a8cd2b7a4040dd190ac8a62ce645b94c47 100755 (executable)
@@ -62,6 +62,11 @@ describe Puppet::Parser::Functions.function(:validate_integer) do
       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
index 1623a3db32b187844d7685c6d42a3d6548193d40..c99d879e9ba5c584da04271833a0e854b4749678 100755 (executable)
@@ -62,6 +62,11 @@ describe Puppet::Parser::Functions.function(:validate_numeric) do
       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