Skip to content

Commit fe7f81b

Browse files
Fix memory leaks
1 parent 5ca626a commit fe7f81b

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

redis_array_impl.c

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ ra_call_extractor(RedisArray *ra, const char *key, int key_len, int *out_len TSR
368368
out[*out_len] = 0;
369369
memcpy(out, Z_STRVAL(z_ret), *out_len);
370370

371+
zval_dtor(&z_argv0);
371372
zval_dtor(&z_ret);
372373
return out;
373374
}
@@ -421,6 +422,8 @@ ra_call_distributor(RedisArray *ra, const char *key, int key_len, int *pos TSRML
421422
return 0;
422423
}
423424

425+
zval_dtor(&z_argv0);
426+
424427
*pos = Z_LVAL(z_ret);
425428
zval_dtor(&z_ret);
426429
return 1;
@@ -539,13 +542,12 @@ ra_index_del(zval *z_keys, zval *z_redis TSRMLS_DC) {
539542

540543
void
541544
ra_index_keys(zval *z_pairs, zval *z_redis TSRMLS_DC) {
542-
543545
/* Initialize key array */
544546
zval z_keys, *z_entry_p;
545547
HashPosition pos;
546548
array_init_size(&z_keys, zend_hash_num_elements(Z_ARRVAL_P(z_pairs)));
547-
548-
/* Go through input array and add values to the key array */
549+
550+
/* Go through input array and add values to the key array */
549551
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(z_pairs), &pos);
550552
while ((z_entry_p = zend_hash_get_current_data_ex(Z_ARRVAL_P(z_pairs), &pos)) != NULL) {
551553
zend_string *key;
@@ -587,8 +589,10 @@ ra_index_key(const char *key, int key_len, zval *z_redis TSRMLS_DC) {
587589
/* run SADD */
588590
call_user_function(&redis_ce->function_table, z_redis, &z_fun_sadd, &z_ret, 2, z_args TSRMLS_CC);
589591

590-
/* don't dtor z_ret, since we're returning z_redis */
591-
zval_dtor(&z_args[1]);
592+
zval_dtor(&z_ret);
593+
zval_dtor(&z_fun_sadd);
594+
zval_dtor(&z_args[0]);
595+
zval_dtor(&z_args[1]);
592596
}
593597

594598
void
@@ -617,13 +621,14 @@ ra_index_exec(zval *z_redis, zval *return_value, int keep_all TSRMLS_DC) {
617621

618622
void
619623
ra_index_discard(zval *z_redis, zval *return_value TSRMLS_DC) {
620-
621-
zval z_fun_discard, z_ret;
624+
zval z_fun_discard, z_ret;
622625

623626
/* run DISCARD */
624627
ZVAL_STRING(&z_fun_discard, "DISCARD");
625-
call_user_function(&redis_ce->function_table, z_redis, &z_fun_discard, &z_ret, 0, NULL TSRMLS_CC);
626-
628+
629+
call_user_function(&redis_ce->function_table, z_redis, &z_fun_discard, &z_ret, 0, NULL TSRMLS_CC);
630+
631+
zval_dtor(&z_fun_discard);
627632
zval_dtor(&z_ret);
628633
}
629634

@@ -669,11 +674,11 @@ ra_rehash_scan(zval *z_redis, char ***keys, int **key_lens, const char *cmd, con
669674
char *key;
670675
int key_len;
671676

672-
/* arg */
673-
ZVAL_STRING(&z_arg, arg);
677+
/* Function and argument */
678+
ZVAL_STRING(&z_fun_smembers, cmd);
679+
ZVAL_STRING(&z_arg, arg);
674680

675681
/* run SMEMBERS */
676-
ZVAL_STRING(&z_fun_smembers, cmd);
677682
call_user_function(&redis_ce->function_table, z_redis, &z_fun_smembers, &z_ret, 1, &z_arg TSRMLS_CC);
678683
if(Z_TYPE(z_ret) != IS_ARRAY) { /* failure */
679684
return -1; /* TODO: log error. */
@@ -700,7 +705,9 @@ ra_rehash_scan(zval *z_redis, char ***keys, int **key_lens, const char *cmd, con
700705
}
701706

702707
/* cleanup */
703-
zval_dtor(&z_ret);
708+
zval_dtor(&z_fun_smembers);
709+
zval_dtor(&z_arg);
710+
zval_dtor(&z_ret);
704711

705712
return count;
706713
}
@@ -1064,8 +1071,7 @@ ra_move_list(const char *key, int key_len, zval *z_from, zval *z_to, long ttl TS
10641071

10651072
void
10661073
ra_move_key(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC) {
1067-
1068-
long res[2], type, ttl;
1074+
long res[2], type, ttl;
10691075
zend_bool success = 0;
10701076
if (ra_get_key_type(z_from, key, key_len, z_from, res TSRMLS_CC)) {
10711077
type = res[0];
@@ -1074,7 +1080,7 @@ ra_move_key(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC) {
10741080
ra_index_multi(z_to, MULTI TSRMLS_CC);
10751081
switch(type) {
10761082
case REDIS_STRING:
1077-
success = ra_move_string(key, key_len, z_from, z_to, ttl TSRMLS_CC);
1083+
success = ra_move_string(key, key_len, z_from, z_to, ttl TSRMLS_CC);
10781084
break;
10791085

10801086
case REDIS_SET:
@@ -1086,11 +1092,11 @@ ra_move_key(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC) {
10861092
break;
10871093

10881094
case REDIS_ZSET:
1089-
success = ra_move_zset(key, key_len, z_from, z_to, ttl TSRMLS_CC);
1095+
success = ra_move_zset(key, key_len, z_from, z_to, ttl TSRMLS_CC);
10901096
break;
10911097

10921098
case REDIS_HASH:
1093-
success = ra_move_hash(key, key_len, z_from, z_to, ttl TSRMLS_CC);
1099+
success = ra_move_hash(key, key_len, z_from, z_to, ttl TSRMLS_CC);
10941100
break;
10951101

10961102
default:
@@ -1130,21 +1136,20 @@ static void zval_rehash_callback(zend_fcall_info *z_cb, zend_fcall_info_cache *z
11301136
static void
11311137
ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool b_index,
11321138
zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache TSRMLS_DC) {
1133-
11341139
char **keys;
11351140
int *key_lens;
11361141
long count, i;
11371142
int target_pos;
11381143
zval *z_target;
1139-
1140-
/* list all keys */
1144+
1145+
/* list all keys */
11411146
if(b_index) {
11421147
count = ra_rehash_scan_index(z_redis, &keys, &key_lens TSRMLS_CC);
11431148
} else {
11441149
count = ra_rehash_scan_keys(z_redis, &keys, &key_lens TSRMLS_CC);
11451150
}
1146-
1147-
/* callback */
1151+
1152+
/* callback */
11481153
if(z_cb && z_cb_cache) {
11491154
zval_rehash_callback(z_cb, z_cb_cache, hostname, count TSRMLS_CC);
11501155
}

0 commit comments

Comments
 (0)