@@ -184,7 +184,7 @@ static void set_buffer_for_string(MYSQL_BIND* bind_buffer, unsigned long *length
184
184
* the buffer is a Ruby string pointer and not our memory to manage.
185
185
*/
186
186
#define FREE_BINDS \
187
- for (i = 0; i < c ; i++) { \
187
+ for (i = 0; i < bind_count ; i++) { \
188
188
if (bind_buffers[i].buffer && NIL_P(params_enc[i])) { \
189
189
xfree(bind_buffers[i].buffer); \
190
190
} \
@@ -247,8 +247,7 @@ static VALUE rb_mysql_stmt_execute(int argc, VALUE *argv, VALUE self) {
247
247
MYSQL_BIND * bind_buffers = NULL ;
248
248
unsigned long * length_buffers = NULL ;
249
249
unsigned long bind_count ;
250
- long i ;
251
- int c ;
250
+ unsigned long i ;
252
251
MYSQL_STMT * stmt ;
253
252
MYSQL_RES * metadata ;
254
253
VALUE opts ;
@@ -263,24 +262,26 @@ static VALUE rb_mysql_stmt_execute(int argc, VALUE *argv, VALUE self) {
263
262
264
263
conn_enc = rb_to_encoding (wrapper -> encoding );
265
264
266
- // Get count of ordinary arguments, and extract hash opts/keyword arguments
267
- c = rb_scan_args (argc , argv , "*:" , NULL , & opts );
268
-
269
265
stmt = stmt_wrapper -> stmt ;
270
-
271
266
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
+ }
274
275
}
275
276
276
277
// setup any bind variables in the query
277
278
if (bind_count > 0 ) {
278
279
// 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 );
280
281
bind_buffers = xcalloc (bind_count , sizeof (MYSQL_BIND ));
281
282
length_buffers = xcalloc (bind_count , sizeof (unsigned long ));
282
283
283
- for (i = 0 ; i < c ; i ++ ) {
284
+ for (i = 0 ; i < bind_count ; i ++ ) {
284
285
bind_buffers [i ].buffer = NULL ;
285
286
params_enc [i ] = Qnil ;
286
287
0 commit comments