cosmo0920 / windows-pr

A collection of Windows functions, constants and macros predefined for you for win32-api
23 stars 9 forks source link

Version 1.2.2: error in buf.strip in #get_last_error of error.rb on Windows 2k8 r2 #10

Closed dalang closed 10 years ago

dalang commented 10 years ago

I encountered this error when use puppet 3.3.2. It contains windows-pr 1.2.2. Each time I run puppet agent, I found error info as below:

C:/Program Files (x86)/Puppet Labs/Puppet/sys/ruby/lib/ruby/gems/1.9.1/gems/windows-pr-1.2.2/lib/windows/error.rb:449:in `strip'
C:/Program Files (x86)/Puppet Labs/Puppet/sys/ruby/lib/ruby/gems/1.9.1/gems/windows-pr-1.2.2/lib/windows/error.rb:449:in `get_last_error'
C:/Program Files (x86)/Puppet Labs/Puppet/puppet/lib/puppet/util/windows/error.rb:11:in `initialize'
C:/Program Files (x86)/Puppet Labs/Puppet/puppet/lib/puppet/util/windows/registry.rb:26:in `new'
C:/Program Files (x86)/Puppet Labs/Puppet/puppet/lib/puppet/util/windows/registry.rb:26:in `rescue in open'
C:/Program Files (x86)/Puppet Labs/Puppet/puppet/lib/puppet/util/windows/registry.rb:21:in `open'
C:/Program Files (x86)/Puppet Labs/Puppet/puppet/lib/puppet/provider/package/windows/package.rb:37:in `block (2 levels) in with_key'
...

I add 'p buf' in to #get_last_error and get the following output:

"\xCF\xB5\xCD\xB3\xD5\xD2\xB2\xBB\xB5\xBD\xD6\xB8\xB6\xA8\xB5\xC4\xCE\xC4\xBC\xFE\xA1\xA3\r\n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\xCF\xB5\xCD\xB3\xD5\xD2\xB2\xBB\xB5\xBD\xD6\xB8\xB6\xA8\xB5\xC4\xCE\xC4\xBC\xFE\xA1\xA3\r\n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"

my puppet agent runs well if I change the buf.strip to buf in #get_last_error. Maybe it is a bug in windows-pr 1.2.2.

djberg96 commented 10 years ago

I don't think this is a bug in windows-pr. I mean, buf is just a string, so calling buf.strip should not raise any kind of error. Very strange.

The output for buf that you posted looks like 2 strings. Is that supposed to be one long string? Does it cause any sort of issue if you just copy/paste that into irb and call .strip on it?

djberg96 commented 10 years ago

I'm also curious what puppet is using that requires windows-pr as I've moved most libraries over to ffi.

dalang commented 10 years ago

Here is a mistake in my last post, I duplicated the buf output, indeed it is a single string.

On Mac OSX 10.9, I copy/paste it into irb and call .strip on it

On Ubuntu 12.04, the same operation but outputs changes:

I agree with you that's not a bug in windows-pr. But there is not doubt something wrong make the error happen. I have no idea how to get rid of it, could you give me a few hints?

phasis68 commented 10 years ago

It seems that your code page is 936 (Simplified Chinese). The current windows-pr is based on US-ASCII code page.

The line 449 of lib/windows/error.rb

buf.strip

should be modified as

if RUBY_VERSION < '1.9.0'
  buf.strip
else
  buf.force_encoding(Encoding.default_external).strip
end
dalang commented 10 years ago

Bingo. Thanks to @phasis68 for pointing out the reason and giving a solution.

Windows 2008 Server R2, I worked on, use Code page 936, which is Microsoft's character encoding for Simplified Chinese. The value of Encoding.default_external is Encoding::GBK in my environment.

This solution resolve my problem perfectly.