Skip to content

Commit bcd8770

Browse files
committed
Always check statement handle before using it, set the handle to NULL when closing it
1 parent ef5d61d commit bcd8770

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

ext/mysql2/statement.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ static VALUE intern_usec, intern_sec, intern_min, intern_hour, intern_day, inter
77

88
#define GET_STATEMENT(self) \
99
mysql_stmt_wrapper *stmt_wrapper; \
10-
Data_Get_Struct(self, mysql_stmt_wrapper, stmt_wrapper);
10+
Data_Get_Struct(self, mysql_stmt_wrapper, stmt_wrapper); \
11+
if (!stmt_wrapper->stmt) { rb_raise(cMysql2Error, "Invalid statement handle"); }
1112

1213

1314
static void rb_mysql_stmt_mark(void * ptr) {
@@ -17,11 +18,11 @@ static void rb_mysql_stmt_mark(void * ptr) {
1718
rb_gc_mark(stmt_wrapper->client);
1819
}
1920

20-
static void *nogvl_stmt_close(void *ptr) {
21+
static void *nogvl_stmt_close(void * ptr) {
2122
mysql_stmt_wrapper *stmt_wrapper = (mysql_stmt_wrapper *)ptr;
22-
if (stmt_wrapper->closed == 0) {
23+
if (stmt_wrapper->stmt) {
2324
mysql_stmt_close(stmt_wrapper->stmt);
24-
stmt_wrapper->closed = 1;
25+
stmt_wrapper->stmt = NULL;
2526
}
2627
return NULL;
2728
}
@@ -103,7 +104,6 @@ VALUE rb_mysql_stmt_new(VALUE rb_client, VALUE sql) {
103104
rb_stmt = Data_Make_Struct(cMysql2Statement, mysql_stmt_wrapper, rb_mysql_stmt_mark, rb_mysql_stmt_free, stmt_wrapper);
104105
{
105106
stmt_wrapper->client = rb_client;
106-
stmt_wrapper->closed = 0;
107107
stmt_wrapper->refcount = 1;
108108
stmt_wrapper->stmt = NULL;
109109
}

ext/mysql2/statement.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ extern VALUE cMysql2Statement;
66
typedef struct {
77
VALUE client;
88
MYSQL_STMT *stmt;
9-
int closed;
109
int refcount;
1110
} mysql_stmt_wrapper;
1211

spec/mysql2/statement_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@
674674
it 'should raise an error on subsequent execution' do
675675
stmt = @client.prepare 'SELECT 1'
676676
stmt.close
677-
expect { stmt.execute }.to raise_error(Mysql2::Error)
677+
expect { stmt.execute }.to raise_error(Mysql2::Error, /Invalid statement handle/)
678678
end
679679
end
680680
end

0 commit comments

Comments
 (0)