From 88fddbc58725909c739c51a69b4f6f3239445a1c Mon Sep 17 00:00:00 2001 From: usa Date: Wed, 24 Feb 2021 17:15:04 +0900 Subject: [PATCH] Guard sql from GC (#1150) 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. --- ext/mysql2/client.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c index f64f74fdd..267d2b067 100644 --- a/ext/mysql2/client.c +++ b/ext/mysql2/client.c @@ -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; @@ -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);