[ruby-core:115884] Windows Ruby 3.2.2: Non-English character added to Windows Registry String Value

Hello, Compiled Ruby 3.2.2 on Windows with MSVC 2019 - Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30147 for x64. Code: require 'win32/registry' win_oracle_key = "SOFTWARE\\ORACLE" reg=Win32::Registry::HKEY_LOCAL_MACHINE.open(win_oracle_key, Win32::Registry::KEY_ALL_ACCESS) inst_loc_key = "inst_loc" inv_dir="C:\\Program Files\\Tester\\Inventory" reg[inst_loc_key] = inv_dir .... Registry contains: C:\Program Files\Tester\Inventory洀 (Not sure what that character represents.) Can the Ruby Dev Team please let me know how to resolve this issue so that this character does not get added to the string? Thanks in advance.

Hello, This issue is resolved, if a change is made in RUBYDIR\lib\ruby\3.2.0\win32\registry.rb to set termize=0 for type REG_SZ, REG_EXPAND_SZ def write(name, type, data) termsize = 0 case type when REG_SZ, REG_EXPAND_SZ data = data.encode(WCHAR) # Adding WCHAR_SIZE to data size causes garbage concatenated to data in the Windows Registry: #termsize = WCHAR_SIZE ... API.SetValue(@hkey, name, type, data, data.bytesize + termsize) The string data in the Windows Registry does not have junk added after making this code change. Is this fix correct? Can the owner of registry.rb please respond? Thanks. On Sat, Dec 23, 2023 at 5:31 PM Jay Mav <mjay80147@gmail.com> wrote:
Hello, Compiled Ruby 3.2.2 on Windows with MSVC 2019 - Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30147 for x64.
Code: require 'win32/registry' win_oracle_key = "SOFTWARE\\ORACLE" reg=Win32::Registry::HKEY_LOCAL_MACHINE.open(win_oracle_key, Win32::Registry::KEY_ALL_ACCESS) inst_loc_key = "inst_loc" inv_dir="C:\\Program Files\\Tester\\Inventory" reg[inst_loc_key] = inv_dir
....
Registry contains: C:\Program Files\Tester\Inventory洀 (Not sure what that character represents.)
Can the Ruby Dev Team please let me know how to resolve this issue so that this character does not get added to the string?
Thanks in advance.

Similar to what's done for the case REG_MULTI_SZ in registry.rb's write(name, type, data), if the null character is added to the string, then the bytesize of the string includes the null's size which conforms with the Registry API's RegSetValueExW()'s requirement for the data's size: when REG_SZ, REG_EXPAND_SZ enc_data = data.encode(WCHAR) # Add NULL WCHAR for string data instead of merely incrementing the termsize. enc_data += WCHAR_NUL #termsize = WCHAR_SIZE ... API.SetValue(@hkey, name, type, enc_data, enc_data.bytesize + termsize) (After concatenating the null-char to the data, enc_data.bytesize includes the size for the null, so it does not need to be added in termsize.) Can the Ruby Dev Team please comment about this proposed fix or suggest the correct fix? On Sun, Dec 24, 2023 at 9:07 PM Jay Mav <mjay80147@gmail.com> wrote:
Hello,
This issue is resolved, if a change is made in RUBYDIR\lib\ruby\3.2.0\win32\registry.rb to set termize=0 for type REG_SZ, REG_EXPAND_SZ
def write(name, type, data)
termsize = 0 case type when REG_SZ, REG_EXPAND_SZ data = data.encode(WCHAR) # Adding WCHAR_SIZE to data size causes garbage concatenated to data in the Windows Registry: #termsize = WCHAR_SIZE ... API.SetValue(@hkey, name, type, data, data.bytesize + termsize)
The string data in the Windows Registry does not have junk added after making this code change.
Is this fix correct?
Can the owner of registry.rb please respond?
Thanks.
On Sat, Dec 23, 2023 at 5:31 PM Jay Mav <mjay80147@gmail.com> wrote:
Hello, Compiled Ruby 3.2.2 on Windows with MSVC 2019 - Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30147 for x64.
Code: require 'win32/registry' win_oracle_key = "SOFTWARE\\ORACLE" reg=Win32::Registry::HKEY_LOCAL_MACHINE.open(win_oracle_key, Win32::Registry::KEY_ALL_ACCESS) inst_loc_key = "inst_loc" inv_dir="C:\\Program Files\\Tester\\Inventory" reg[inst_loc_key] = inv_dir
....
Registry contains: C:\Program Files\Tester\Inventory洀 (Not sure what that character represents.)
Can the Ruby Dev Team please let me know how to resolve this issue so that this character does not get added to the string?
Thanks in advance.
participants (1)
-
Jay Mav