Skip to content

Commit 3d7da1f

Browse files
committed
refactoring
1 parent 2559f72 commit 3d7da1f

File tree

5 files changed

+44
-39
lines changed

5 files changed

+44
-39
lines changed

library.c

+10-12
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,7 @@ redis_unserialize(RedisSock* redis_sock, const char *val, int val_len,
20582058
{
20592059

20602060
php_unserialize_data_t var_hash;
2061-
int ret;
2061+
int ret = 0;
20622062

20632063
switch(redis_sock->serializer) {
20642064
case REDIS_SERIALIZER_PHP:
@@ -2067,19 +2067,17 @@ redis_unserialize(RedisSock* redis_sock, const char *val, int val_len,
20672067
#else
20682068
memset(&var_hash, 0, sizeof(var_hash));
20692069
#endif
2070-
if (!php_var_unserialize(z_ret, (const unsigned char**)&val,
2071-
(const unsigned char*)val + val_len, &var_hash)) {
2072-
ret = 0;
2073-
} else {
2070+
if (php_var_unserialize(z_ret, (const unsigned char**)&val,
2071+
(const unsigned char*)val + val_len, &var_hash)
2072+
) {
20742073
ret = 1;
20752074
}
20762075
#if ZEND_MODULE_API_NO >= 20100000
20772076
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
20782077
#else
20792078
var_destroy(&var_hash);
20802079
#endif
2081-
2082-
return ret;
2080+
break;
20832081

20842082
case REDIS_SERIALIZER_IGBINARY:
20852083
#ifdef HAVE_REDIS_IGBINARY
@@ -2108,14 +2106,14 @@ redis_unserialize(RedisSock* redis_sock, const char *val, int val_len,
21082106
}
21092107

21102108
if(igbinary_unserialize((const uint8_t *)val, (size_t)val_len,
2111-
z_ret TSRMLS_CC) == 0)
2112-
{
2113-
return 1;
2109+
z_ret TSRMLS_CC) == 0
2110+
) {
2111+
ret = 1;
21142112
}
21152113
#endif
2116-
return 0;
2114+
break;
21172115
}
2118-
return 0;
2116+
return ret;
21192117
}
21202118

21212119
PHP_REDIS_API int

redis.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -2390,12 +2390,13 @@ PHP_METHOD(Redis, exec)
23902390
total += ri->request_size;
23912391
}
23922392
if (total) {
2393-
request = emalloc(total);
2393+
request = emalloc(total + 1);
23942394
/* concatenate individual elements one by one in the target buffer */
23952395
for (ri = redis_sock->pipeline_head; ri; ri = ri->next) {
23962396
memcpy(request + offset, ri->request_str, ri->request_size);
23972397
offset += ri->request_size;
23982398
}
2399+
request[total] = '\0';
23992400
if (redis_sock_write(redis_sock, request, total TSRMLS_CC) < 0 ||
24002401
redis_sock_read_multibulk_pipeline_reply(
24012402
INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock) < 0

redis_array.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ PHP_METHOD(RedisArray, del)
11371137
RedisArray *ra;
11381138
int *pos, argc = ZEND_NUM_ARGS(), *argc_each;
11391139
HashTable *h_keys;
1140-
zval *redis_inst, **argv;
1140+
zval **argv;
11411141
long total = 0;
11421142
int free_zkeys = 0;
11431143

@@ -1175,9 +1175,6 @@ PHP_METHOD(RedisArray, del)
11751175
free_zkeys = 1;
11761176
}
11771177

1178-
/* prepare call */
1179-
ZVAL_STRINGL(&z_fun, "DEL", 3);
1180-
11811178
/* init data structures */
11821179
h_keys = Z_ARRVAL(z_keys);
11831180
argc = zend_hash_num_elements(h_keys);
@@ -1196,6 +1193,7 @@ PHP_METHOD(RedisArray, del)
11961193
efree(z_args);
11971194
efree(argv);
11981195
efree(pos);
1196+
efree(argc_each);
11991197
RETURN_FALSE;
12001198
}
12011199

@@ -1206,6 +1204,9 @@ PHP_METHOD(RedisArray, del)
12061204
argv[i++] = data;
12071205
} ZEND_HASH_FOREACH_END();
12081206

1207+
/* prepare call */
1208+
ZVAL_STRINGL(&z_fun, "DEL", 3);
1209+
12091210
/* calls */
12101211
for(n = 0; n < ra->count; ++n) { /* for each node */
12111212
/* We don't even need to make a call to this node if no keys go there */
@@ -1235,18 +1236,17 @@ PHP_METHOD(RedisArray, del)
12351236
continue;
12361237
}
12371238

1238-
redis_inst = &ra->redis[n];
12391239
if(ra->index) { /* add MULTI */
1240-
ra_index_multi(redis_inst, MULTI TSRMLS_CC);
1240+
ra_index_multi(&ra->redis[n], MULTI TSRMLS_CC);
12411241
}
12421242

12431243
/* call */
1244-
call_user_function(&redis_ce->function_table, redis_inst, &z_fun, &z_ret, 1, &z_argarray);
1244+
call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
12451245

12461246
if(ra->index) {
12471247
zval_dtor(&z_ret);
1248-
ra_index_del(&z_argarray, redis_inst TSRMLS_CC); /* use SREM to remove keys from node index */
1249-
ra_index_exec(redis_inst, &z_ret, 0 TSRMLS_CC); /* run EXEC */
1248+
ra_index_del(&z_argarray, &ra->redis[n] TSRMLS_CC); /* use SREM to remove keys from node index */
1249+
ra_index_exec(&ra->redis[n], &z_ret, 0 TSRMLS_CC); /* run EXEC */
12501250
}
12511251
total += Z_LVAL(z_ret); /* increment total */
12521252

redis_array_impl.c

+7
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b
8080
#endif
8181
object_init_ex(&ra->redis[i], redis_ce);
8282
call_user_function(&redis_ce->function_table, &ra->redis[i], &z_cons, &z_ret, 0, NULL);
83+
zval_dtor(&z_ret);
8384

8485
/* create socket */
8586
redis_sock = redis_sock_create(host, host_len, port, ra->connect_timeout, ra->pconnect, NULL, retry_interval, b_lazy_connect);
@@ -341,6 +342,8 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
341342
zval_dtor(&z_params_pconnect);
342343
zval_dtor(&z_params_connect_timeout);
343344
zval_dtor(&z_params_lazy_connect);
345+
zval_dtor(&z_dist);
346+
zval_dtor(&z_fun);
344347

345348
return ra;
346349
}
@@ -923,6 +926,7 @@ ra_move_zset(const char *key, int key_len, zval *z_from, zval *z_to, long ttl TS
923926

924927
/* cleanup */
925928
zval_dtor(&z_fun_zadd);
929+
zval_dtor(&z_ret_dest);
926930
zval_dtor(&z_ret);
927931

928932
/* Free the array itself */
@@ -947,6 +951,8 @@ ra_move_string(const char *key, int key_len, zval *z_from, zval *z_to, long ttl
947951

948952
if(Z_TYPE(z_ret) != IS_STRING) { /* key not found or replaced */
949953
/* TODO: report? */
954+
zval_dtor(&z_args[0]);
955+
zval_dtor(&z_ret);
950956
return 0;
951957
}
952958

@@ -1038,6 +1044,7 @@ ra_move_collection(const char *key, int key_len, zval *z_from, zval *z_to,
10381044

10391045
if(Z_TYPE(z_ret) != IS_ARRAY) { /* key not found or replaced */
10401046
/* TODO: report? */
1047+
zval_dtor(&z_ret);
10411048
return 0;
10421049
}
10431050

redis_commands.c

+16-17
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,7 @@ int redis_zrangebyscore_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
545545

546546
/* Check for withscores and limit */
547547
if (IS_WITHSCORES_ARG(zkey->val, zkey->len)) {
548-
#if (PHP_MAJOR_VERSION < 7)
549-
*withscores = (Z_TYPE_P(z_ele) == IS_BOOL && Z_LVAL_P(z_ele));
550-
#else
551-
*withscores = (Z_TYPE_P(z_ele) == IS_TRUE);
552-
#endif
548+
*withscores = zval_is_true(z_ele);
553549
} else if (IS_LIMIT_ARG(zkey->val, zkey->len) && Z_TYPE_P(z_ele) == IS_ARRAY) {
554550
HashTable *htlimit = Z_ARRVAL_P(z_ele);
555551
zval *zoff, *zcnt;
@@ -1234,15 +1230,23 @@ int redis_set_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
12341230
}
12351231

12361232
/* Expiry can't be set < 1 */
1237-
if (expire < 1) return FAILURE;
1233+
if (expire < 1) {
1234+
if (key_free) efree(key);
1235+
if (val_free) efree(val);
1236+
return FAILURE;
1237+
}
12381238
} else if (Z_TYPE_P(v) == IS_STRING && IS_NX_XX_ARG(Z_STRVAL_P(v))) {
12391239
set_type = Z_STRVAL_P(v);
12401240
}
12411241
} ZEND_HASH_FOREACH_END();
12421242
} else if(z_opts && Z_TYPE_P(z_opts) == IS_LONG) {
12431243
/* Grab expiry and fail if it's < 1 */
12441244
expire = Z_LVAL_P(z_opts);
1245-
if (expire < 1) return FAILURE;
1245+
if (expire < 1) {
1246+
if (key_free) efree(key);
1247+
if (val_free) efree(val);
1248+
return FAILURE;
1249+
}
12461250
}
12471251

12481252
/* Now let's construct the command we want */
@@ -1323,6 +1327,8 @@ int redis_brpoplpush_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
13231327
key1_len, key2, key2_len, timeout);
13241328
}
13251329

1330+
if (key1_free) efree(key1);
1331+
if (key2_free) efree(key2);
13261332
return SUCCESS;
13271333
}
13281334

@@ -1707,6 +1713,7 @@ int redis_bitop_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
17071713
php_error_docref(NULL TSRMLS_CC, E_WARNING,
17081714
"Warning, not all keys hash to the same slot!");
17091715
if(key_free) efree(key);
1716+
efree(z_args);
17101717
return FAILURE;
17111718
}
17121719
*slot = kslot;
@@ -2341,7 +2348,6 @@ int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
23412348
if(slot) {
23422349
php_error_docref(NULL TSRMLS_CC, E_WARNING,
23432350
"SORT BY option is not allowed in Redis Cluster");
2344-
if(key_free) efree(key);
23452351
zval_dtor(&z_argv);
23462352
return FAILURE;
23472353
}
@@ -2372,7 +2378,6 @@ int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
23722378
if(cross_slot) {
23732379
php_error_docref(0 TSRMLS_CC, E_WARNING,
23742380
"Error, SORT key and STORE key have different slots!");
2375-
if(key_free) efree(key);
23762381
zval_dtor(&z_argv);
23772382
return FAILURE;
23782383
}
@@ -2394,7 +2399,6 @@ int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
23942399
if(slot) {
23952400
php_error_docref(NULL TSRMLS_CC, E_WARNING,
23962401
"GET option for SORT disabled in Redis Cluster");
2397-
if(key_free) efree(key);
23982402
zval_dtor(&z_argv);
23992403
return FAILURE;
24002404
}
@@ -2429,7 +2433,6 @@ int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
24292433
if(added==0) {
24302434
php_error_docref(NULL TSRMLS_CC, E_WARNING,
24312435
"Array of GET values requested, but none are valid");
2432-
if(key_free) efree(key);
24332436
zval_dtor(&z_argv);
24342437
return FAILURE;
24352438
}
@@ -2460,7 +2463,6 @@ int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
24602463
) {
24612464
php_error_docref(NULL TSRMLS_CC, E_WARNING,
24622465
"LIMIT options on SORT command must be longs or strings");
2463-
if(key_free) efree(key);
24642466
zval_dtor(&z_argv);
24652467
return FAILURE;
24662468
}
@@ -2492,18 +2494,15 @@ int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
24922494
sizeof("SORT")-1);
24932495

24942496
// Iterate through our arguments
2495-
for(zend_hash_internal_pointer_reset(ht_argv);
2496-
(z_ele = zend_hash_get_current_data(ht_argv)) != NULL;
2497-
zend_hash_move_forward(ht_argv))
2498-
{
2497+
ZEND_HASH_FOREACH_VAL(ht_argv, z_ele) {
24992498
// Args are strings or longs
25002499
if (Z_TYPE_P(z_ele) == IS_STRING) {
25012500
redis_cmd_append_sstr(&cmdstr,Z_STRVAL_P(z_ele),
25022501
Z_STRLEN_P(z_ele));
25032502
} else {
25042503
redis_cmd_append_sstr_long(&cmdstr, Z_LVAL_P(z_ele));
25052504
}
2506-
}
2505+
} ZEND_HASH_FOREACH_END();
25072506

25082507
/* Clean up our arguments array. Note we don't have to free any prefixed
25092508
* key as that we didn't duplicate the pointer if we prefixed */

0 commit comments

Comments
 (0)