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

String object whose length is incorrect is created #427

Closed
skhrshin opened this issue Feb 8, 2022 · 1 comment · Fixed by #428
Closed

String object whose length is incorrect is created #427

skhrshin opened this issue Feb 8, 2022 · 1 comment · Fixed by #428

Comments

@skhrshin
Copy link

skhrshin commented Feb 8, 2022

Summary

I found a problem where pg creates String object whose length is incorrect.

Steps to reproduce

pg version 1.3.1
PostgreSQL version 12.6

  1. Prepare a table named "test_records" with a VARCHAR column "name".
  2. Insert a record with command INSERT INTO test_records VALUES ('日本語');
  3. Run the following code (substitute ActiveRecord::Base.connection.raw_connection with your appropriate connection object)
to_sql = 'SELECT name FROM test_records'
full_query = "COPY (#{to_sql}) TO STDOUT"
conn = ActiveRecord::Base.connection.raw_connection

deco = PG::TextDecoder::CopyRow.new
rows = []

conn.copy_data(full_query, deco) do
  while (row = conn.get_copy_data) do
    rows << row
  end
end

top = rows[0]
p [top[0], top[0].length]

Expected Result

["日本語", 3]

is printed.

Actual Result

["日本語", 9]

is printed. 9 is actually size in bytes in UTF-8 encoding and not a number of characters.

larskanis added a commit to larskanis/ruby-pg that referenced this issue Feb 8, 2022
The COPY and record decoders use a ruby string as internal buffer.
If this buffer is given to the user, it must be passed through rb_str_modify().

Fixes ged#427
@larskanis
Copy link
Collaborator

I'm able to reproduce the issue. It was present in ruby versions 2.4 to 3.1 and since pg-0.18.0.

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

Successfully merging a pull request may close this issue.

2 participants