Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af13f95

Browse files
Fix BITOP cross-slot bug
Fixes #2210
1 parent 7134461 commit af13f95

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

redis_commands.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,12 +2825,12 @@ int redis_bitop_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
28252825
// Verify slot if this is a Cluster request
28262826
if (slot) {
28272827
kslot = cluster_hash_key(key, key_len);
2828-
if (*slot == -1 || kslot != *slot) {
2829-
php_error_docref(NULL, E_WARNING,
2830-
"Warning, not all keys hash to the same slot!");
2828+
if (*slot != -1 && kslot != *slot) {
2829+
php_error_docref(NULL, E_WARNING, "Warning, not all keys hash to the same slot!");
28312830
zend_string_release(zstr);
28322831
if (key_free) efree(key);
28332832
efree(z_args);
2833+
efree(cmdstr.c);
28342834
return FAILURE;
28352835
}
28362836
*slot = kslot;

tests/RedisTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ public function testBitcount() {
230230
$this->assertEquals(5, $this->redis->bitcount('bitcountkey', 0, 9, true));
231231
}
232232

233+
public function testBitop() {
234+
if (!$this->minVersionCheck('2.6.0'))
235+
$this->markTestSkipped();
236+
237+
$this->redis->set("{key}1", "foobar");
238+
$this->redis->set("{key}2", "abcdef");
239+
240+
// Regression test for GitHub issue #2210
241+
$this->assertEquals(6, $this->redis->bitop('AND', '{key}1', '{key}2'));
242+
243+
// Make sure RedisCluster doesn't even send the command. We don't care
244+
// about what Redis returns
245+
@$this->redis->bitop('AND', 'key1', 'key2', 'key3');
246+
$this->assertEquals(NULL, $this->redis->getLastError());
247+
248+
$this->redis->del('{key}1', '{key}2');
249+
}
250+
233251
public function testBitsets() {
234252

235253
$this->redis->del('key');

0 commit comments

Comments
 (0)