Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/pg_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ alloc_query_params(struct query_params_data *paramsData)
int nParams;
int i=0;
t_pg_coder *conv;
unsigned int required_pool_size;
size_t required_pool_size;
char *memory_pool;

Check_Type(paramsData->params, T_ARRAY);
Expand Down
19 changes: 19 additions & 0 deletions spec/pg/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2855,6 +2855,25 @@ def wait_check_socket(conn)
end
end

it "should encode big strings with typecasting without overflow" do
big_count = 100
step = 42_949_673
big_len = (step - 3) / 2
wrap = (big_count * step) & 0xffff_ffff

big = 'A'.b * big_len
params = Array.new(big_count, big)

tm = PG::TypeMapByClass.new
tm[String] = PG::TextEncoder::Bytea.new.freeze

sql = big_count.times.map{|n| "$#{n+1}" }.join(",")
@conn.exec_params('select '+sql, params, 0, tm)
rescue PG::UnableToSend
# ignore "PQsendQueryParams cannot allocate memory for output buffer"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This happens when the host running the test doesn't have enough free memory?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

libpq allocates 2 GB at the maximum, due to using signed int for the buffer size: https://github.com/postgres/postgres/blob/b45137f315bb49fa4f50ae9cc16fda0a49196610/src/interfaces/libpq/fe-misc.c#L301
So this test will always raise PG::UnableToSend with the current libpq implementation. The important thing here is that it doesn't crash due to memory access error.

end


it "can process #copy_data input queries with row encoder and respects character encoding" do
@conn2.exec( "CREATE TEMP TABLE copytable (col1 TEXT)" )
@conn2.copy_data( "COPY copytable FROM STDOUT" ) do |res|
Expand Down
Loading