Skip to content

Commit ad45964

Browse files
committed
Using ZVAL_DEREF macros for dereference input variables.
This PR fixes issues phpredis#946 and phpredis#1166.
1 parent 8a50a14 commit ad45964

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@ typedef int strlen_t;
373373
#define PHP_FE_END { NULL, NULL, NULL }
374374
#endif
375375

376+
/* References don't need any actions */
377+
#define ZVAL_DEREF(v) PHPREDIS_NOTUSED(v)
378+
376379
#else
377380
#include <zend_smart_str.h>
378381
#include <ext/standard/php_smart_string.h>

redis.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,7 @@ PHP_REDIS_API void generic_unsubscribe_cmd(INTERNAL_FUNCTION_PARAMETERS,
23792379
}
23802380

23812381
ZEND_HASH_FOREACH_VAL(arr_hash, data) {
2382+
ZVAL_DEREF(data);
23822383
if (Z_TYPE_P(data) == IS_STRING) {
23832384
char *old_cmd = NULL;
23842385
if(*cmd) {

redis_commands.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ int redis_zrangebyscore_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
439439
ZEND_HASH_FOREACH_KEY_VAL(ht_opt, idx, zkey, z_ele) {
440440
/* All options require a string key type */
441441
if (!zkey) continue;
442-
442+
ZVAL_DEREF(z_ele);
443443
/* Check for withscores and limit */
444444
if (IS_WITHSCORES_ARG(zkey->val, zkey->len)) {
445445
*withscores = zval_is_true(z_ele);
@@ -587,7 +587,7 @@ int redis_zinter_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
587587
// Process our weights
588588
ZEND_HASH_FOREACH_VAL(ht_weights, z_ele) {
589589
// Ignore non numeric args unless they're inf/-inf
590-
590+
ZVAL_DEREF(z_ele);
591591
switch (Z_TYPE_P(z_ele)) {
592592
case IS_LONG:
593593
redis_cmd_append_sstr_long(&cmdstr, Z_LVAL_P(z_ele));
@@ -1146,6 +1146,7 @@ int redis_set_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
11461146

11471147
/* Iterate our option array */
11481148
ZEND_HASH_FOREACH_KEY_VAL(kt, idx, zkey, v) {
1149+
ZVAL_DEREF(v);
11491150
/* Detect PX or EX argument and validate timeout */
11501151
if (zkey && IS_EX_PX_ARG(zkey->val)) {
11511152
/* Set expire type */
@@ -1381,6 +1382,7 @@ int redis_hmget_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
13811382

13821383
// Iterate over our member array
13831384
ZEND_HASH_FOREACH_VAL(ht_arr, z_mem) {
1385+
ZVAL_DEREF(z_mem);
13841386
// We can only handle string or long values here
13851387
if ((Z_TYPE_P(z_mem) == IS_STRING && Z_STRLEN_P(z_mem) > 0)
13861388
|| Z_TYPE_P(z_mem) == IS_LONG
@@ -2539,6 +2541,7 @@ static void get_georadius_opts(HashTable *ht, int *withcoord, int *withdist,
25392541

25402542
/* Iterate over our argument array, collating which ones we have */
25412543
ZEND_HASH_FOREACH_KEY_VAL(ht, idx, zkey, optval) {
2544+
ZVAL_DEREF(optval);
25422545
/* If the key is numeric it's a non value option */
25432546
if (zkey) {
25442547
if (zkey->len == 5 && !strcasecmp(zkey->val, "count") && Z_TYPE_P(optval) == IS_LONG) {

tests/RedisTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,11 @@ public function testZX() {
20472047
$this->assertTrue(array('val1', 'val2') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(1, 2))));
20482048
$this->assertTrue(array('val0', 'val1') === $this->redis->zRangeByScore('key', 0, 1, array('limit' => array(0, 100))));
20492049

2050+
// limits as references
2051+
$limit = array(0, 100);
2052+
foreach ($limit as &$val) {}
2053+
$this->assertTrue(array('val0', 'val1') === $this->redis->zRangeByScore('key', 0, 1, array('limit' => $limit)));
2054+
20502055
$this->assertTrue(array('val3') === $this->redis->zRevRangeByScore('key', 3, 0, array('limit' => array(0, 1))));
20512056
$this->assertTrue(array('val3', 'val2') === $this->redis->zRevRangeByScore('key', 3, 0, array('limit' => array(0, 2))));
20522057
$this->assertTrue(array('val2', 'val1') === $this->redis->zRevRangeByScore('key', 3, 0, array('limit' => array(1, 2))));
@@ -2376,6 +2381,11 @@ public function testHashes() {
23762381
$this->assertTrue('456' === $this->redis->hGet('h', 'y'));
23772382
$this->assertTrue(array(123 => 'x', 'y' => '456') === $this->redis->hMget('h', array('123', 'y')));
23782383

2384+
// references
2385+
$keys = array(123, 'y');
2386+
foreach ($keys as &$key) {}
2387+
$this->assertTrue(array(123 => 'x', 'y' => '456') === $this->redis->hMget('h', $keys));
2388+
23792389
// check non-string types.
23802390
$this->redis->del('h1');
23812391
$this->assertTrue(TRUE === $this->redis->hMSet('h1', array('x' => 0, 'y' => array(), 'z' => new stdclass(), 't' => NULL)));

0 commit comments

Comments
 (0)