Skip to content

Commit 4764bc4

Browse files
authored
Merge pull request #531 from larskanis/fix-530
Fix possible segfault when creating new PG::Result
2 parents 2b87db5 + 2b09abb commit 4764bc4

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

ext/pg_result.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,16 @@ pg_new_result2(PGresult *result, VALUE rb_pgconn)
208208

209209
this = (t_pg_result *)xmalloc(sizeof(*this) + sizeof(*this->fnames) * nfields);
210210
this->pgresult = result;
211+
/* Initialize connection and typemap prior to any object allocations,
212+
* to make sure valid objects are marked. */
213+
this->connection = rb_pgconn;
214+
this->typemap = pg_typemap_all_strings;
215+
this->p_typemap = RTYPEDDATA_DATA( this->typemap );
211216
this->nfields = -1;
212217
this->tuple_hash = Qnil;
213218
this->field_map = Qnil;
214219
this->flags = 0;
215220
self = TypedData_Wrap_Struct(rb_cPGresult, &pgresult_type, this);
216-
RB_OBJ_WRITE(self, &this->connection, rb_pgconn);
217221

218222
if( result ){
219223
t_pg_connection *p_conn = pg_get_connection(rb_pgconn);
@@ -227,8 +231,6 @@ pg_new_result2(PGresult *result, VALUE rb_pgconn)
227231
this->p_typemap = RTYPEDDATA_DATA( this->typemap );
228232
this->flags = p_conn->flags;
229233
} else {
230-
RB_OBJ_WRITE(self, &this->typemap, pg_typemap_all_strings);
231-
this->p_typemap = RTYPEDDATA_DATA( this->typemap );
232234
this->enc_idx = rb_locale_encindex();
233235
}
234236

@@ -1422,8 +1424,9 @@ pgresult_type_map_set(VALUE self, VALUE typemap)
14221424
/* Check type of method param */
14231425
TypedData_Get_Struct(typemap, t_typemap, &pg_typemap_type, p_typemap);
14241426

1425-
RB_OBJ_WRITE(self, &this->typemap, p_typemap->funcs.fit_to_result( typemap, self ));
1426-
this->p_typemap = RTYPEDDATA_DATA( this->typemap );
1427+
typemap = p_typemap->funcs.fit_to_result( typemap, self );
1428+
RB_OBJ_WRITE(self, &this->typemap, typemap);
1429+
this->p_typemap = RTYPEDDATA_DATA( typemap );
14271430

14281431
return typemap;
14291432
}

ext/pg_type_map_by_column.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,7 @@ pg_tmbc_init(VALUE self, VALUE conv_ary)
287287
t_pg_coder *p_coder;
288288
/* Check argument type and store the coder pointer */
289289
TypedData_Get_Struct(obj, t_pg_coder, &pg_coder_type, p_coder);
290-
if( p_coder ){
291-
RB_OBJ_WRITTEN(self, Qnil, p_coder->coder_obj);
292-
}
290+
RB_OBJ_WRITTEN(self, Qnil, p_coder->coder_obj);
293291
this->convs[i].cconv = p_coder;
294292
}
295293
}

0 commit comments

Comments
 (0)