Skip to content

Commit a2fc987

Browse files
committed
Merge pull request #346 from sodabrew/gc_guard_hash_dup
Per #321 use RB_GC_GUARD around rb_hash_dup
2 parents 561d85b + cb4d68b commit a2fc987

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

ext/mysql2/client.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ static VALUE nogvl_use_result(void *ptr) {
375375
static VALUE rb_mysql_client_async_result(VALUE self) {
376376
MYSQL_RES * result;
377377
VALUE resultObj;
378+
VALUE current, is_streaming;
378379
GET_CLIENT(self);
379380

380381
/* if we're not waiting on a result, do nothing */
@@ -388,7 +389,7 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
388389
return rb_raise_mysql2_error(wrapper);
389390
}
390391

391-
VALUE is_streaming = rb_hash_aref(rb_iv_get(self, "@current_query_options"), sym_stream);
392+
is_streaming = rb_hash_aref(rb_iv_get(self, "@current_query_options"), sym_stream);
392393
if(is_streaming == Qtrue) {
393394
result = (MYSQL_RES *)rb_thread_blocking_region(nogvl_use_result, wrapper, RUBY_UBF_IO, 0);
394395
} else {
@@ -404,7 +405,11 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
404405
return Qnil;
405406
}
406407

407-
resultObj = rb_mysql_result_to_obj(self, wrapper->encoding, rb_hash_dup(rb_iv_get(self, "@current_query_options")), result);
408+
current = rb_hash_dup(rb_iv_get(self, "@current_query_options"));
409+
RB_GC_GUARD(current);
410+
Check_Type(current, T_HASH);
411+
resultObj = rb_mysql_result_to_obj(self, wrapper->encoding, current, result);
412+
408413
return resultObj;
409414
}
410415

@@ -549,10 +554,13 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
549554
REQUIRE_CONNECTED(wrapper);
550555
args.mysql = wrapper->client;
551556

552-
rb_iv_set(self, "@current_query_options", rb_hash_dup(rb_iv_get(self, "@query_options")));
553-
current = rb_iv_get(self, "@current_query_options");
557+
current = rb_hash_dup(rb_iv_get(self, "@query_options"));
558+
RB_GC_GUARD(current);
559+
Check_Type(current, T_HASH);
560+
rb_iv_set(self, "@current_query_options", current);
561+
554562
if (rb_scan_args(argc, argv, "11", &args.sql, &opts) == 2) {
555-
opts = rb_funcall(current, intern_merge_bang, 1, opts);
563+
rb_funcall(current, intern_merge_bang, 1, opts);
556564

557565
if (rb_hash_aref(current, sym_async) == Qtrue) {
558566
async = 1;
@@ -930,6 +938,7 @@ static VALUE rb_mysql_client_store_result(VALUE self)
930938
{
931939
MYSQL_RES * result;
932940
VALUE resultObj;
941+
VALUE current;
933942
GET_CLIENT(self);
934943

935944
result = (MYSQL_RES *)rb_thread_blocking_region(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
@@ -942,9 +951,12 @@ static VALUE rb_mysql_client_store_result(VALUE self)
942951
return Qnil;
943952
}
944953

945-
resultObj = rb_mysql_result_to_obj(self, wrapper->encoding, rb_hash_dup(rb_iv_get(self, "@current_query_options")), result);
946-
return resultObj;
954+
current = rb_hash_dup(rb_iv_get(self, "@current_query_options"));
955+
RB_GC_GUARD(current);
956+
Check_Type(current, T_HASH);
957+
resultObj = rb_mysql_result_to_obj(self, wrapper->encoding, current, result);
947958

959+
return resultObj;
948960
}
949961

950962
#ifdef HAVE_RUBY_ENCODING_H

ext/mysql2/result.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ static VALUE rb_mysql_result_fetch_fields(VALUE self) {
394394
GetMysql2Result(self, wrapper);
395395

396396
defaults = rb_iv_get(self, "@query_options");
397+
Check_Type(defaults, T_HASH);
397398
if (rb_hash_aref(defaults, sym_symbolize_keys) == Qtrue) {
398399
symbolizeKeys = 1;
399400
}
@@ -423,6 +424,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
423424
GetMysql2Result(self, wrapper);
424425

425426
defaults = rb_iv_get(self, "@query_options");
427+
Check_Type(defaults, T_HASH);
426428
if (rb_scan_args(argc, argv, "01&", &opts, &block) == 1) {
427429
opts = rb_funcall(defaults, intern_merge, 1, opts);
428430
} else {

0 commit comments

Comments
 (0)