puppetlabs/puppetlabs-registry — pull request #62 by joshcooper (2014-08-26 01:40:46+00:00)
Title: (MODULES-1292) Don't try to apply a regex to a binary string

Previously, if you used the registry module with ruby 2 (either x86 or x64)
to manage a binary string with value 'de ad', it would correctly write the
value to the registry, but everytime puppet ran afterward, it would incorrectly
read the "current" value as 'de', losing the 'ad' part.

The root cause is because the registry modules was calling String#scan on
a binary string. In ruby 1.9, the string encoding is based on the current
code page, e.g. cp1252.

irb(main):006:0> ndata = [0xde, 0xad].pack('C*')
=> "\xDE\xAD"
irb(main):007:0> ndata.force_encoding('cp1252')
=> "\xDE\xAD"
irb(main):008:0> ndata.scan(/./)
=> ["\xDE", "\xAD"]

But in ruby 2, the default encoding is UTF-8, and the two byte sequence is
interpreted as a two byte character:

irb(main):002:0> ndata = [0xde, 0xad].pack('C*')
=> "\xDE\xAD"
irb(main):003:0> ndata.force_encoding('UTF-8')
=> "\u07AD"
irb(main):004:0> ndata.scan(/./)
=> ["\u07AD"]

Ruby's registry code should be setting the binary string encoding to
ASCII-8BIT, but does not.

This commit changes the provider to convert the string to bytes, and then
formats each byte as a lowercase hex string. This works regardless of what
encoding ruby is currently using.