diff --git a/redis.c b/redis.c index df59a529fc..24b2e5fd90 100755 --- a/redis.c +++ b/redis.c @@ -2270,12 +2270,12 @@ PHP_METHOD(Redis, sMembers) PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keyword_len, int min_argc, RedisSock **out_sock, int has_timeout, int all_keys, int can_serialize) { - zval *object, **z_args, *z_array; + zval **z_args, *z_array; char **keys, *cmd; int cmd_len, *keys_len, *keys_to_free; int i, j, argc = ZEND_NUM_ARGS(), real_argc = 0; int single_array = 0; - int timeout; + int timeout = 0; int pos; int array_size; @@ -2347,10 +2347,9 @@ PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword char *key; unsigned int key_len; unsigned long idx; - int type; zval **z_value_pp; - type = zend_hash_get_current_key_ex(keytable, &key, &key_len, &idx, 0, NULL); + zend_hash_get_current_key_ex(keytable, &key, &key_len, &idx, 0, NULL); if(zend_hash_get_current_data(keytable, (void**)&z_value_pp) == FAILURE) { continue; /* this should never happen, according to the PHP people. */ } @@ -2392,12 +2391,7 @@ PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword real_argc++; } } else { - int nb_keys; - if(!has_timeout) { - nb_keys = argc; - } else if(has_timeout && Z_TYPE_P(z_args[argc - 1]) == IS_LONG) { - nb_keys = argc - 1; - } else { + if(has_timeout && Z_TYPE_P(z_args[argc - 1]) != IS_LONG) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "Syntax error on timeout"); } @@ -2465,7 +2459,6 @@ PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword if(z_args) efree(z_args); - object = getThis(); /* cmd[cmd_len] = 0; php_printf("cmd=[%s]\n", cmd); @@ -2703,10 +2696,9 @@ PHP_METHOD(Redis, sort) { char *key; unsigned int key_len; unsigned long idx; - int type; zval **z_value_pp; - type = zend_hash_get_current_key_ex(keytable, &key, &key_len, &idx, 0, NULL); + zend_hash_get_current_key_ex(keytable, &key, &key_len, &idx, 0, NULL); if(zend_hash_get_current_data(keytable, (void**)&z_value_pp) == FAILURE) { continue; /* this should never happen, according to the PHP people. */ } @@ -3441,7 +3433,7 @@ generic_mset(INTERNAL_FUNCTION_PARAMETERS, char *kw, void (*fun)(INTERNAL_FUNCTI zval *object; RedisSock *redis_sock; - char *cmd, *p; + char *cmd = NULL, *p = NULL; int cmd_len = 0, argc = 0, kw_len = strlen(kw); int step = 0; // 0: compute size; 1: copy strings. zval *z_array; @@ -4205,7 +4197,7 @@ PHP_METHOD(Redis, zIncrBy) PHPAPI void generic_z_command(INTERNAL_FUNCTION_PARAMETERS, char *command, int command_len) { zval *object, *keys_array, *weights_array = NULL, **data; - HashTable *arr_weights_hash, *arr_keys_hash; + HashTable *arr_weights_hash = NULL, *arr_keys_hash; int key_output_len, array_weights_count, array_keys_count, operation_len = 0; char *key_output, *operation; RedisSock *redis_sock; @@ -4655,12 +4647,11 @@ PHPAPI void array_zip_values_and_scores(RedisSock *redis_sock, zval *z_tab, int char *tablekey, *hkey, *hval; unsigned int tablekey_len; - int hkey_len, hval_len; + int hkey_len; unsigned long idx; - int type; zval **z_key_pp, **z_value_pp; - type = zend_hash_get_current_key_ex(keytable, &tablekey, &tablekey_len, &idx, 0, NULL); + zend_hash_get_current_key_ex(keytable, &tablekey, &tablekey_len, &idx, 0, NULL); if(zend_hash_get_current_data(keytable, (void**)&z_key_pp) == FAILURE) { continue; /* this should never happen, according to the PHP people. */ } @@ -4674,14 +4665,13 @@ PHPAPI void array_zip_values_and_scores(RedisSock *redis_sock, zval *z_tab, int zend_hash_move_forward(keytable); /* fetch again */ - type = zend_hash_get_current_key_ex(keytable, &tablekey, &tablekey_len, &idx, 0, NULL); + zend_hash_get_current_key_ex(keytable, &tablekey, &tablekey_len, &idx, 0, NULL); if(zend_hash_get_current_data(keytable, (void**)&z_value_pp) == FAILURE) { continue; /* this should never happen, according to the PHP people. */ } /* get current value, a hash value now. */ hval = Z_STRVAL_PP(z_value_pp); - hval_len = Z_STRLEN_PP(z_value_pp); if(use_atof) { /* zipping a score */ add_assoc_double_ex(z_ret, hkey, 1+hkey_len, atof(hval)); @@ -5298,10 +5288,9 @@ PHP_METHOD(Redis, subscribe) zval *z_tab, **tmp; char *type_response; - int callback_type; - zval *z_o, *z_fun,*z_ret, *z_args[3]; + int callback_type = 0; + zval *z_o, *z_fun = NULL,*z_ret, *z_args[3]; char *method_name; - zend_class_entry *ce; if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oaz|z", &object, redis_ce, &array, &z_callback) == FAILURE) { @@ -5387,6 +5376,8 @@ PHP_METHOD(Redis, subscribe) callback_ft_name = Z_STRVAL_P(z_callback); callback_ft_name_len = strlen(callback_ft_name); callback_type = R_SUB_CALLBACK_FT_TYPE; + MAKE_STD_ZVAL(z_fun); + ZVAL_STRINGL(z_fun, callback_ft_name, callback_ft_name_len, 0); } /* Multibulk Response, format : {message type, originating channel, message payload} */ @@ -5421,14 +5412,12 @@ PHP_METHOD(Redis, subscribe) switch(callback_type) { case R_SUB_CALLBACK_CLASS_TYPE: MAKE_STD_ZVAL(z_ret); - call_user_function(&ce->function_table, &z_o, z_fun, z_ret, 3, z_args TSRMLS_CC); + call_user_function(&redis_ce->function_table, &z_o, z_fun, z_ret, 3, z_args TSRMLS_CC); efree(z_ret); break; case R_SUB_CALLBACK_FT_TYPE: MAKE_STD_ZVAL(z_ret); - MAKE_STD_ZVAL(z_fun); - ZVAL_STRINGL(z_fun, callback_ft_name, callback_ft_name_len, 0); call_user_function(EG(function_table), NULL, z_fun, z_ret, 3, z_args TSRMLS_CC); efree(z_fun); efree(z_ret); diff --git a/redis_array.c b/redis_array.c index 93e843c2bc..7964c8189b 100644 --- a/redis_array.c +++ b/redis_array.c @@ -264,7 +264,7 @@ static void ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, int cmd_len, zval *z_args, zval *z_new_target) { zval **zp_tmp, z_tmp; - char *key; + char *key = NULL; // set to avoid "unused-but-set-variable" int key_len; int i; zval *redis_inst; @@ -715,7 +715,7 @@ PHP_METHOD(RedisArray, mget) int *pos, argc, *argc_each; HashTable *h_keys; HashPosition pointer; - zval **redis_instances, *redis_inst, **argv; + zval **redis_instances, **argv; /* Multi/exec support */ HANDLE_MULTI_EXEC("MGET"); @@ -771,8 +771,6 @@ PHP_METHOD(RedisArray, mget) /* calls */ for(n = 0; n < ra->count; ++n) { /* for each node */ - redis_inst = ra->redis[n]; - /* copy args for MGET call on node. */ MAKE_STD_ZVAL(z_argarray); array_init(z_argarray); diff --git a/tests/TestRedis.php b/tests/TestRedis.php index c3738efd1c..c9e241c991 100644 --- a/tests/TestRedis.php +++ b/tests/TestRedis.php @@ -18,6 +18,8 @@ class Redis_Test extends TestSuite public function setUp() { $this->redis = $this->newInstance(); + $info = $this->redis->info(); + $this->version = (isset($info['redis_version'])?$info['redis_version']:'0.0.0'); } private function newInstance() { @@ -44,6 +46,12 @@ public function reset() $this->tearDown(); } + public function testMinimumVersion() + { + // Minimum server version required for tests + $this->assertTrue(version_compare($this->version, "2.4.0", "ge")); + } + public function testPing() { @@ -848,8 +856,14 @@ public function testSortAsc() { } // SORT list → [ghi, def, abc] - $this->assertEquals(array_reverse($list), $this->redis->sortAsc('list')); - $this->assertEquals(array_reverse($list), $this->redis->sort('list', array('sort' => 'asc'))); + if (version_compare($this->version, "2.5.0", "lt")) { + $this->assertEquals(array_reverse($list), $this->redis->sortAsc('list')); + $this->assertEquals(array_reverse($list), $this->redis->sort('list', array('sort' => 'asc'))); + } else { + // TODO rewrite, from 2.6.0 release notes: + // SORT now will refuse to sort in numerical mode elements that can't be parsed + // as numbers + } // SORT list ALPHA → [abc, def, ghi] $this->assertEquals($list, $this->redis->sortAscAlpha('list')); @@ -883,7 +897,13 @@ public function testSortDesc() { } // SORT list → [ghi, abc, def] - $this->assertEquals(array_reverse($list), $this->redis->sortDesc('list')); + if (version_compare($this->version, "2.5.0", "lt")) { + $this->assertEquals(array_reverse($list), $this->redis->sortDesc('list')); + } else { + // TODO rewrite, from 2.6.0 release notes: + // SORT now will refuse to sort in numerical mode elements that can't be parsed + // as numbers + } // SORT list ALPHA → [abc, def, ghi] $this->assertEquals(array('ghi', 'def', 'abc'), $this->redis->sortDescAlpha('list')); @@ -1580,13 +1600,22 @@ public function testinfo() { "connected_clients", "connected_slaves", "used_memory", - "changes_since_last_save", - "bgsave_in_progress", - "last_save_time", "total_connections_received", "total_commands_processed", "role"); - + if (version_compare($this->version, "2.5.0", "lt")) { + array_push($keys, + "changes_since_last_save", + "bgsave_in_progress", + "last_save_time" + ); + } else { + array_push($keys, + "rdb_changes_since_last_save", + "rdb_bgsave_in_progress", + "rdb_last_save_time" + ); + } foreach($keys as $k) { $this->assertTrue(in_array($k, array_keys($info))); @@ -1595,9 +1624,12 @@ public function testinfo() { // INFO COMMANDSTATS $info = $this->redis->info("COMMANDSTATS"); - foreach($info as $k => $value) { - $this->assertTrue(strpos($k, 'cmdstat_') !== false); - } + $this->assertTrue(is_array($info)); + if (is_array($info)) { + foreach($info as $k => $value) { + $this->assertTrue(strpos($k, 'cmdstat_') !== false); + } + } } public function testSelect() { @@ -1617,16 +1649,16 @@ public function testMset() { $this->assertFalse($this->redis->mset(array())); // set ø → FALSE - + /* * Integer keys */ - // No prefix - $set_array = Array(-1 => 'neg1', -2 => 'neg2', -3 => 'neg3', 1 => 'one', 2 => 'two', '3' => 'three'); - $this->redis->delete(array_keys($set_array)); - $this->assertTrue($this->redis->mset($set_array)); - $this->assertEquals($this->redis->mget(array_keys($set_array)), array_values($set_array)); + // No prefix + $set_array = Array(-1 => 'neg1', -2 => 'neg2', -3 => 'neg3', 1 => 'one', 2 => 'two', '3' => 'three'); + $this->redis->delete(array_keys($set_array)); + $this->assertTrue($this->redis->mset($set_array)); + $this->assertEquals($this->redis->mget(array_keys($set_array)), array_values($set_array)); $this->redis->delete(array_keys($set_array)); // With a prefix @@ -3013,7 +3045,7 @@ public function testScript() { // None should exist $result = $this->redis->script('exists', $s1_sha, $s2_sha, $s3_sha); $this->assertTrue(is_array($result) && count($result) == 3); - $this->assertTrue(count(array_filter($result)) == 0); + $this->assertTrue(is_array($result) && count(array_filter($result)) == 0); // Load them up $this->assertTrue($this->redis->script('load', $s1_src) == $s1_sha); @@ -3022,7 +3054,7 @@ public function testScript() { // They should all exist $result = $this->redis->script('exists', $s1_sha, $s2_sha, $s3_sha); - $this->assertTrue(count(array_filter($result)) == 3); + $this->assertTrue(is_array($result) && count(array_filter($result)) == 3); } public function testEval() { @@ -3093,7 +3125,7 @@ public function testEval() { // Now run our script, and check our values against each other $eval_result = $this->redis->eval($nested_script); - $this->assertTrue(count($this->array_diff_recursive($eval_result, $expected)) == 0); + $this->assertTrue(is_array($eval_result) && count($this->array_diff_recursive($eval_result, $expected)) == 0); /* * Nested reply wihin a multi/pipeline block @@ -3109,7 +3141,7 @@ public function testEval() { $replies = $this->redis->exec(); foreach($replies as $reply) { - $this->assertTrue(count($this->array_diff_recursive($reply, $expected)) == 0); + $this->assertTrue(is_array($reply) && count($this->array_diff_recursive($reply, $expected)) == 0); } } diff --git a/tests/test.php b/tests/test.php index d679e4ff96..1d27062f74 100644 --- a/tests/test.php +++ b/tests/test.php @@ -16,7 +16,7 @@ protected function assertTrue($bool) { $bt = debug_backtrace(false); $count = count($bt); self::$errors []= sprintf("Assertion failed: %s:%d (%s)\n", - $bt[$count - 2]["file"], $bt[$count - 3]["line"], $bt[$count - 1]["function"]); + $bt[0]["file"], $bt[0]["line"], $bt[1]["function"]); } protected function assertEquals($a, $b) { @@ -27,7 +27,7 @@ protected function assertEquals($a, $b) { $count = count($bt); self::$errors []= sprintf("Assertion failed (%s !== %s): %s:%d (%s)\n", print_r($a, true), print_r($b, true), - $bt[$count - 2]["file"], $bt[$count - 3]["line"], $bt[$count - 1]["function"]); + $bt[0]["file"], $bt[0]["line"], $bt[1]["function"]); } public static function run($className) {