Skip to content

Commit a0ca592

Browse files
committed
Use a local scope to avoid leaking the temporary variable for bind_count
1 parent 620a055 commit a0ca592

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

ext/mysql2/statement.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static void set_buffer_for_string(MYSQL_BIND* bind_buffer, unsigned long *length
184184
* the buffer is a Ruby string pointer and not our memory to manage.
185185
*/
186186
#define FREE_BINDS \
187-
for (i = 0; i < c; i++) { \
187+
for (i = 0; i < bind_count; i++) { \
188188
if (bind_buffers[i].buffer && NIL_P(params_enc[i])) { \
189189
xfree(bind_buffers[i].buffer); \
190190
} \
@@ -247,8 +247,7 @@ static VALUE rb_mysql_stmt_execute(int argc, VALUE *argv, VALUE self) {
247247
MYSQL_BIND *bind_buffers = NULL;
248248
unsigned long *length_buffers = NULL;
249249
unsigned long bind_count;
250-
long i;
251-
int c;
250+
unsigned long i;
252251
MYSQL_STMT *stmt;
253252
MYSQL_RES *metadata;
254253
VALUE opts;
@@ -263,24 +262,26 @@ static VALUE rb_mysql_stmt_execute(int argc, VALUE *argv, VALUE self) {
263262

264263
conn_enc = rb_to_encoding(wrapper->encoding);
265264

266-
// Get count of ordinary arguments, and extract hash opts/keyword arguments
267-
c = rb_scan_args(argc, argv, "*:", NULL, &opts);
268-
269265
stmt = stmt_wrapper->stmt;
270-
271266
bind_count = mysql_stmt_param_count(stmt);
272-
if (c != (long)bind_count) {
273-
rb_raise(cMysql2Error, "Bind parameter count (%ld) doesn't match number of arguments (%d)", bind_count, c);
267+
268+
// Get count of ordinary arguments, and extract hash opts/keyword arguments
269+
// Use a local scope to avoid leaking the temporary count variable
270+
{
271+
int c = rb_scan_args(argc, argv, "*:", NULL, &opts);
272+
if (c != (long)bind_count) {
273+
rb_raise(cMysql2Error, "Bind parameter count (%ld) doesn't match number of arguments (%d)", bind_count, c);
274+
}
274275
}
275276

276277
// setup any bind variables in the query
277278
if (bind_count > 0) {
278279
// Scratch space for string encoding exports, allocate on the stack
279-
params_enc = alloca(sizeof(VALUE) * c);
280+
params_enc = alloca(sizeof(VALUE) * bind_count);
280281
bind_buffers = xcalloc(bind_count, sizeof(MYSQL_BIND));
281282
length_buffers = xcalloc(bind_count, sizeof(unsigned long));
282283

283-
for (i = 0; i < c; i++) {
284+
for (i = 0; i < bind_count; i++) {
284285
bind_buffers[i].buffer = NULL;
285286
params_enc[i] = Qnil;
286287

0 commit comments

Comments
 (0)