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

Skip to content

Commit eff6a91

Browse files
Enforce offset range limitations for GETBIT and SETBIT
Addresses phpredis#401
1 parent b3c6616 commit eff6a91

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ typedef enum _REDIS_REPLY_TYPE {
4747
#define REDIS_SERIALIZER_PHP 1
4848
#define REDIS_SERIALIZER_IGBINARY 2
4949

50+
/* GETBIT/SETBIT offset range limits */
51+
#define BITOP_MIN_OFFSET 0
52+
#define BITOP_MAX_OFFSET 4294967295
53+
5054
#define IF_MULTI() if(redis_sock->mode == MULTI)
5155
#define IF_MULTI_OR_ATOMIC() if(redis_sock->mode == MULTI || redis_sock->mode == ATOMIC)\
5256

redis.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,11 @@ PHP_METHOD(Redis, getBit)
17391739
RETURN_FALSE;
17401740
}
17411741

1742+
// GETBIT and SETBIT only work for 0 - 2^32-1
1743+
if(offset < BITOP_MIN_OFFSET || offset > BITOP_MAX_OFFSET) {
1744+
RETURN_FALSE;
1745+
}
1746+
17421747
key_free = redis_key_prefix(redis_sock, &key, &key_len TSRMLS_CC);
17431748
cmd_len = redis_cmd_format_static(&cmd, "GETBIT", "sd", key, key_len, (int)offset);
17441749
if(key_free) efree(key);
@@ -1768,6 +1773,11 @@ PHP_METHOD(Redis, setBit)
17681773
RETURN_FALSE;
17691774
}
17701775

1776+
// GETBIT and SETBIT only work for 0 - 2^32-1
1777+
if(offset < BITOP_MIN_OFFSET || offset > BITOP_MAX_OFFSET) {
1778+
RETURN_FALSE;
1779+
}
1780+
17711781
key_free = redis_key_prefix(redis_sock, &key, &key_len TSRMLS_CC);
17721782
cmd_len = redis_cmd_format_static(&cmd, "SETBIT", "sdd", key, key_len, (int)offset, (int)val);
17731783
if(key_free) efree(key);
@@ -2363,15 +2373,6 @@ PHP_METHOD(Redis, sRandMember)
23632373
}
23642374
REDIS_PROCESS_RESPONSE(redis_string_response);
23652375
}
2366-
2367-
/*IF_ATOMIC() {
2368-
// This will be bulk or multi-bulk depending if we passed the optional [COUNT] argument
2369-
if(redis_read_variant_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL) < 0) {
2370-
RETURN_FALSE;
2371-
}
2372-
}
2373-
REDIS_PROCESS_RESPONSE(redis_read_variant_reply);
2374-
*/
23752376
}
23762377
/* }}} */
23772378

tests/TestRedis.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,13 @@ public function testBitsets() {
109109

110110
// values above 1 are changed to 1 but don't overflow on bits to the right.
111111
$this->assertTrue(0 === $this->redis->setBit('key', 0, 0xff));
112-
$this->assertTrue("\x9f" === $this->redis->get('key'));
112+
$this->assertTrue("\x9f" === $this->redis->get('key'));
113+
114+
// Verify valid offset ranges
115+
$this->assertFalse($this->redis->getBit('key', -1));
116+
$this->assertFalse($this->redis->getBit('key', 4294967296));
117+
$this->assertFalse($this->redis->setBit('key', -1, 1));
118+
$this->assertFalse($this->redis->setBit('key', 4294967296, 1));
113119
}
114120

115121
public function test1000() {

0 commit comments

Comments
 (0)