diff options
author | Leonardo Rodrigues de Mello <l@lmello.eu.org> | 2013-09-16 11:08:13 -0300 |
---|---|---|
committer | Leonardo Rodrigues de Mello <l@lmello.eu.org> | 2013-09-18 11:19:52 -0300 |
commit | 80a8b7bd1fdb3aadd53d112ce9a20b86d8ea9a62 (patch) | |
tree | 1701fb9cc05f0d766c0d81e3bc3151083f25ed1e /lib | |
parent | 806430224ad0da860be3761ab83f1e574b64fc60 (diff) | |
download | puppet-stdlib-80a8b7bd1fdb3aadd53d112ce9a20b86d8ea9a62.tar.gz puppet-stdlib-80a8b7bd1fdb3aadd53d112ce9a20b86d8ea9a62.tar.bz2 |
(#20681) fix behaviour of delete_undef_values
The issue #20681 describe the error of delete() function
removing the elements from the origin array/hash/string.
This issue affected other delete functions. Because
ruby delete and delete_if functions make destructive
changes to the origin array/hash.
The delete_undef_values removed elements from the
origin array/hash and this is not the desired behaviour.
To solve this, we should dup or clone the array/hash
before using the delete or delete_if ruby functions.
We should also check if args[0] is not nil before using
dup, since dup on nil raises exception.
This fix the problem and add unit tests, so we could
enforce this behaviour and prevent regressions.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/parser/functions/delete_undef_values.rb | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/lib/puppet/parser/functions/delete_undef_values.rb b/lib/puppet/parser/functions/delete_undef_values.rb index 4eecab2..532639e 100644 --- a/lib/puppet/parser/functions/delete_undef_values.rb +++ b/lib/puppet/parser/functions/delete_undef_values.rb @@ -18,16 +18,16 @@ Would return: ['A','',false] raise(Puppet::ParseError, "delete_undef_values(): Wrong number of arguments given " + "(#{args.size})") if args.size < 1 - - result = args[0] + + unless args[0].is_a? Array or args[0].is_a? Hash + raise(Puppet::ParseError, + "delete_undef_values(): expected an array or hash, got #{args[0]} type #{args[0].class} ") + end + result = args[0].dup if result.is_a?(Hash) result.delete_if {|key, val| val.equal? :undef} elsif result.is_a?(Array) result.delete :undef - else - raise(Puppet::ParseError, - "delete_undef_values(): Wrong argument type #{args[0].class} " + - "for first argument") end result end |