From: Jay Mav via ruby-core Date: 2023-12-25T22:39:45-08:00 Subject: [ruby-core:115898] Re: Windows Ruby 3.2.2: Non-English character added to Windows Registry String Value --===============5311907710415862403== Content-Type: multipart/alternative; boundary="0000000000006f9965060d63f332" --0000000000006f9965060d63f332 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 =3D data.encode(WCHAR) # Add NULL WCHAR for string data instead of merely incrementing the termsize. enc_data +=3D WCHAR_NUL #termsize =3D 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=E2=80=AFPM Jay Mav wrote= : > Hello, > > This issue is resolved, if a change is made in > RUBYDIR\lib\ruby\3.2.0\win32\registry.rb to set termize=3D0 for type REG_= SZ, > REG_EXPAND_SZ > > def write(name, type, data) > > termsize =3D 0 > case type > when REG_SZ, REG_EXPAND_SZ > data =3D data.encode(WCHAR) > # Adding WCHAR_SIZE to data size causes garbage concatenated to > data in the Windows Registry: > #termsize =3D 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=E2=80=AFPM Jay Mav wro= te: > >> 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 =3D "SOFTWARE\\ORACLE" >> reg=3DWin32::Registry::HKEY_LOCAL_MACHINE.open(win_oracle_key, >> Win32::Registry::KEY_ALL_ACCESS) >> inst_loc_key =3D "inst_loc" >> inv_dir=3D"C:\\Program Files\\Tester\\Inventory" >> reg[inst_loc_key] =3D inv_dir >> >> .... >> >> Registry contains: C:\Program Files\Tester\Inventory=E6=B4=80 >> (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. >> > --0000000000006f9965060d63f332 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

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 t= o the string, then the bytesize of the string includes the null's size = which conforms with the Registry API's=20 RegSetValueExW()'s requirement for the data's size:

=C2=A0when REG_SZ, REG_EXPAND_SZ
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = enc_data =3D data.encode(WCHAR)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 # Add NULL W= CHAR for string data instead of merely incrementing the termsize.
=C2=A0= =C2=A0 =C2=A0 =C2=A0 enc_data +=3D WCHAR_NUL
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 #termsize =3D WCHAR_SIZE

...

(After concatenating the null-cha= r 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=E2=80=AFPM Jay Mav <mjay80147@gmail.com> wrote:
Hello,

This issu= e is resolved, if a change is made in RUBYDIR\lib\ruby\3.2.0\win32\registry= .rb to set termize=3D0 for type REG_SZ, REG_EXPAND_SZ

=C2=A0 =C2=A0 = def write(name, type, data)

=C2=A0 =C2=A0 =C2=A0 termsize =3D 0
= =C2=A0 =C2=A0 =C2=A0 case type
=C2=A0 =C2=A0 =C2=A0 when REG_SZ, REG_EXP= AND_SZ
=C2=A0 =C2=A0 =C2=A0 =C2=A0 data =3D data.encode(WCHAR)
=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # Adding WCHAR_SIZE to data size cause= s garbage concatenated to data in the Windows Registry:
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 #termsize =3D WCHAR_SIZE
...
=C2=A0 =C2=A0 =C2=A0 API.S= etValue(@hkey, name, type, data, data.bytesize + termsize)

The strin= g data in the Windows Registry does not have junk added after making this c= ode change.

Is this fix correct?

Can the owner of registry.rb= please respond?

Thanks.

On Sat, Dec 23, 2023 at 5:31=E2=80=AFPM = Jay Mav <mjay80= 147@gmail.com> wrote:
Hello,
Compiled Ruby 3.2.2 on Windows with= MSVC 2019 - Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30147 fo= r x64.

Code:
require 'win32/registry&#= 39;
win_oracle_key =3D "SOFTWARE\\ORACLE"
reg=3DWin32::Regi= stry::HKEY_LOCAL_MACHINE.open(win_oracle_key, Win32::Registry::KEY_ALL_ACCE= SS)
inst_loc_key =3D "inst_loc"
inv_dir=3D"C:\\Program= Files\\Tester\\Inventory"
reg[inst_loc_key] =3D inv_dir

...= .

Registry contains: C:\Program Files\Tester\Inventory=E6=B4=80
(= Not sure what that character represents.)

Can the Ruby Dev Team plea= se let me know how to resolve this issue so that this character does not ge= t added to the string?

Thanks in advance.
--0000000000006f9965060d63f332-- --===============5311907710415862403== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/ --===============5311907710415862403==--