Skip to content

Commit 50174ff

Browse files
Eric Wongbrianmario
authored andcommitted
avoid potentially unsafe Ruby C API usage w/o GVL
Using StringValuePtr() /can/ potentially allocate a new String object. This allocation _currently_ impossible in our codebase, but to prevent future/potential bugs from appearing, we'll avoid StringValuePtr() (and any functions not explicitly supported w/o GVL) in nogvl_* regions.
1 parent e36f680 commit 50174ff

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

ext/mysql2/client.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ struct nogvl_connect_args {
4646
struct nogvl_send_query_args {
4747
MYSQL *mysql;
4848
VALUE sql;
49+
const char *sql_ptr;
50+
long sql_len;
4951
mysql_client_wrapper *wrapper;
5052
};
5153

@@ -243,10 +245,8 @@ static VALUE rb_mysql_client_close(VALUE self) {
243245
static VALUE nogvl_send_query(void *ptr) {
244246
struct nogvl_send_query_args *args = ptr;
245247
int rv;
246-
const char *sql = StringValuePtr(args->sql);
247-
long sql_len = RSTRING_LEN(args->sql);
248248

249-
rv = mysql_send_query(args->mysql, sql, sql_len);
249+
rv = mysql_send_query(args->mysql, args->sql_ptr, args->sql_len);
250250

251251
return rv == 0 ? Qtrue : Qfalse;
252252
}
@@ -452,6 +452,8 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
452452
// ensure the string is in the encoding the connection is expecting
453453
args.sql = rb_str_export_to_enc(args.sql, conn_enc);
454454
#endif
455+
args.sql_ptr = StringValuePtr(args.sql);
456+
args.sql_len = RSTRING_LEN(args.sql);
455457

456458
// see if this connection is still waiting on a result from a previous query
457459
if (wrapper->active == 0) {

0 commit comments

Comments
 (0)