Skip to content

Commit

Permalink
Verify array input to PG::TypeMapByColumn
Browse files Browse the repository at this point in the history
This could cause a segfault.

Fixes #620
  • Loading branch information
larskanis committed Jan 10, 2025
1 parent c6d1047 commit 1ec3e20
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/pg_type_map_by_column.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pg_tmbc_fit_to_query( VALUE self, VALUE params )
t_tmbc *this = RTYPEDDATA_DATA( self );
t_typemap *default_tm;

Check_Type(params, T_ARRAY);
nfields = (int)RARRAY_LEN( params );
if ( this->nfields != nfields ) {
rb_raise( rb_eArgError, "number of result fields (%d) does not match number of mapped columns (%d)",
Expand Down
6 changes: 6 additions & 0 deletions spec/pg/type_map_by_column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ def decode(res, tuple, field)
expect{ PG::TypeMapByColumn.new( [123] ) }.to raise_error(TypeError, /wrong argument type (Integer|Fixnum)/)
end

it "should raise an error for invalid input when used as type_map" do
map = PG::TypeMapByColumn.new([PG::TextEncoder::Integer.new])
record_encoder = PG::TextEncoder::Record.new(type_map: map)
expect{ record_encoder.encode(123) }.to raise_error(TypeError)
end

it "shouldn't allow result mappings with different number of fields" do
res = @conn.exec( "SELECT 1" )
expect{ res.type_map = PG::TypeMapByColumn.new([]) }.to raise_error(ArgumentError, /mapped columns/)
Expand Down

0 comments on commit 1ec3e20

Please sign in to comment.