Skip to content

Commit ec45f82

Browse files
sodabrewjustincase
authored andcommitted
Use xmalloc to match existing xcalloc and xfree. Remember to free length_buffers. Comment on this.
1 parent fe27c8b commit ec45f82

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

ext/mysql2/statement.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,19 @@ static void *nogvl_stmt_store_result(void *ptr) {
185185
}
186186
}
187187

188+
/* Free each bind_buffer[i].buffer except when params_enc is non-nil, this means
189+
* the buffer is a Ruby string pointer and not our memory to manage.
190+
*/
188191
#define FREE_BINDS \
189192
for (i = 0; i < argc; i++) { \
190193
if (bind_buffers[i].buffer && NIL_P(params_enc[i])) { \
191194
xfree(bind_buffers[i].buffer); \
192195
} \
193196
} \
194-
if (argc > 0) xfree(bind_buffers);
197+
if (argc > 0) { \
198+
xfree(bind_buffers); \
199+
xfree(length_buffers); \
200+
}
195201

196202
/* call-seq: stmt.execute
197203
*
@@ -247,22 +253,22 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
247253
case T_FIXNUM:
248254
#if SIZEOF_INT < SIZEOF_LONG
249255
bind_buffers[i].buffer_type = MYSQL_TYPE_LONGLONG;
250-
bind_buffers[i].buffer = malloc(sizeof(long long int));
256+
bind_buffers[i].buffer = xmalloc(sizeof(long long int));
251257
*(long*)(bind_buffers[i].buffer) = FIX2LONG(argv[i]);
252258
#else
253259
bind_buffers[i].buffer_type = MYSQL_TYPE_LONG;
254-
bind_buffers[i].buffer = malloc(sizeof(int));
260+
bind_buffers[i].buffer = xmalloc(sizeof(int));
255261
*(long*)(bind_buffers[i].buffer) = FIX2INT(argv[i]);
256262
#endif
257263
break;
258264
case T_BIGNUM:
259265
bind_buffers[i].buffer_type = MYSQL_TYPE_LONGLONG;
260-
bind_buffers[i].buffer = malloc(sizeof(long long int));
266+
bind_buffers[i].buffer = xmalloc(sizeof(long long int));
261267
*(LONG_LONG*)(bind_buffers[i].buffer) = rb_big2ll(argv[i]);
262268
break;
263269
case T_FLOAT:
264270
bind_buffers[i].buffer_type = MYSQL_TYPE_DOUBLE;
265-
bind_buffers[i].buffer = malloc(sizeof(double));
271+
bind_buffers[i].buffer = xmalloc(sizeof(double));
266272
*(double*)(bind_buffers[i].buffer) = NUM2DBL(argv[i]);
267273
break;
268274
case T_STRING:
@@ -282,7 +288,7 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
282288
// TODO: what Ruby type should support MYSQL_TYPE_TIME
283289
if (CLASS_OF(argv[i]) == rb_cTime || CLASS_OF(argv[i]) == cDateTime) {
284290
bind_buffers[i].buffer_type = MYSQL_TYPE_DATETIME;
285-
bind_buffers[i].buffer = malloc(sizeof(MYSQL_TIME));
291+
bind_buffers[i].buffer = xmalloc(sizeof(MYSQL_TIME));
286292

287293
MYSQL_TIME t;
288294
VALUE rb_time = argv[i];
@@ -301,7 +307,7 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
301307

302308
bind_buffers[i].buffer_type = MYSQL_TYPE_DATE;
303309

304-
bind_buffers[i].buffer = malloc(sizeof(MYSQL_TIME));
310+
bind_buffers[i].buffer = xmalloc(sizeof(MYSQL_TIME));
305311

306312
MYSQL_TIME t;
307313
VALUE rb_time = argv[i];

0 commit comments

Comments
 (0)