Skip to content

Commit

Permalink
Fix crash if a Mysql2::Client object is allocated but never connected (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
sodabrew authored Jan 9, 2020
1 parent b52f27e commit f8560c5
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ext/mysql2/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ static VALUE allocate(VALUE klass) {
wrapper->server_version = 0;
wrapper->reconnect_enabled = 0;
wrapper->connect_timeout = 0;
wrapper->initialized = 0; /* means that that the wrapper is initialized */
wrapper->initialized = 0; /* will be set true after calling mysql_init */
wrapper->closed = 1; /* will be set false after calling mysql_real_connect */
wrapper->refcount = 1;
wrapper->closed = 0;
wrapper->client = (MYSQL*)xmalloc(sizeof(MYSQL));

return obj;
Expand Down Expand Up @@ -467,6 +467,7 @@ static VALUE rb_mysql_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VA
rb_raise_mysql2_error(wrapper);
}

wrapper->closed = 0;
wrapper->server_version = mysql_get_server_version(wrapper->client);
return self;
}
Expand Down

0 comments on commit f8560c5

Please sign in to comment.