Skip to content

Commit

Permalink
Guard sql from GC (#1150)
Browse files Browse the repository at this point in the history
In `rb_mysql_query()`, the raw pointer of the sql string is extracted,
and it is passed to `do_send_query()` via `args`.
`do_send_query()` internally releases the GVL, then ruby might do GC
in the function.
Then, the sql string may be GC'ed, and causes SEGV.
Therefore, should guard the sql string until `do_send_query()` ends.
  • Loading branch information
unak authored Feb 24, 2021
1 parent d4bb730 commit 88fddbc
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ext/mysql2/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ static VALUE rb_mysql_query(VALUE self, VALUE sql, VALUE current) {

#ifndef _WIN32
rb_rescue2(do_send_query, (VALUE)&args, disconnect_and_raise, self, rb_eException, (VALUE)0);
(void)RB_GC_GUARD(sql);

if (rb_hash_aref(current, sym_async) == Qtrue) {
return Qnil;
Expand All @@ -810,6 +811,7 @@ static VALUE rb_mysql_query(VALUE self, VALUE sql, VALUE current) {
}
#else
do_send_query((VALUE)&args);
(void)RB_GC_GUARD(sql);

/* this will just block until the result is ready */
return rb_ensure(rb_mysql_client_async_result, self, disconnect_and_mark_inactive, self);
Expand Down

0 comments on commit 88fddbc

Please sign in to comment.