Skip to content

Commit f8560c5

Browse files
authored
Fix crash if a Mysql2::Client object is allocated but never connected (brianmario#1101)
Reproducible with any older version of the mysql2 gem: ruby -r mysql2 -I lib -e 'Mysql2::Client.allocate.close' Before this fix, the line above would crash out with a memory error: malloc: *** error for object 0x...: pointer being freed was not allocated malloc: *** set a breakpoint in malloc_error_break to debug Abort trap: 6
1 parent b52f27e commit f8560c5

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

ext/mysql2/client.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ static VALUE allocate(VALUE klass) {
323323
wrapper->server_version = 0;
324324
wrapper->reconnect_enabled = 0;
325325
wrapper->connect_timeout = 0;
326-
wrapper->initialized = 0; /* means that that the wrapper is initialized */
326+
wrapper->initialized = 0; /* will be set true after calling mysql_init */
327+
wrapper->closed = 1; /* will be set false after calling mysql_real_connect */
327328
wrapper->refcount = 1;
328-
wrapper->closed = 0;
329329
wrapper->client = (MYSQL*)xmalloc(sizeof(MYSQL));
330330

331331
return obj;
@@ -467,6 +467,7 @@ static VALUE rb_mysql_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VA
467467
rb_raise_mysql2_error(wrapper);
468468
}
469469

470+
wrapper->closed = 0;
470471
wrapper->server_version = mysql_get_server_version(wrapper->client);
471472
return self;
472473
}

0 commit comments

Comments
 (0)