Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding-aware Mysql2::Client#escape does not consistently escape/encode binary strings #1362

Open
qcn opened this issue Apr 11, 2024 · 0 comments

Comments

@qcn
Copy link

qcn commented Apr 11, 2024

Using a Mysql2::Client with encoding: 'utf8mb4', passing a standard binary string (ASCII 8-bit encoded) to the encoding-aware #escape method sometimes returns an escaped string with UTF-8 encoding, and sometimes returns an unchanged binary string maintaining its ASCII 8-bit encoding. I'd expect that all binary string inputs would return an output in the same encoding, whether that's escaped UTF-8 or unchanged ASCII 8-bit.

Reproduction:

irb(main):014> client = ::Mysql2::Client.new(mysql_opts.merge({encoding: 'utf8mb4'}))
=> 
#<Mysql2::Client:0x00007fe881b54200
...
irb(main):015> hex1 = '6614ed2fb7e749cda6caab6ca6b34dcc'
=> "6614ed2fb7e749cda6caab6ca6b34dcc"
irb(main):016> hex2 = 'bafe80143bbe4bd3ba785d0679192fbf'
=> "bafe80143bbe4bd3ba785d0679192fbf"
irb(main):017> bin1 = [hex1].pack('H*')
=> "f\x14\xED/\xB7\xE7I\xCD\xA6\xCA\xABl\xA6\xB3M\xCC"
irb(main):018> bin1.encoding
=> #<Encoding:ASCII-8BIT>
irb(main):019> bin2 = [hex2].pack('H*')
=> "\xBA\xFE\x80\x14;\xBEK\xD3\xBAx]\x06y\x19/\xBF"
irb(main):020> bin2.encoding
=> #<Encoding:ASCII-8BIT>
irb(main):021> client.escape(bin1)
=> "f\u0014\\\xED/\xB7\\\xE7Iͦʫl\xA6\xB3M\\\xCC"
irb(main):022> client.escape(bin1).encoding
=> #<Encoding:UTF-8>
irb(main):023> client.escape(bin2)
=> "\xBA\xFE\x80\x14;\xBEK\xD3\xBAx]\x06y\x19/\xBF"
irb(main):024> client.escape(bin2).encoding
=> #<Encoding:ASCII-8BIT>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant