8000 Fixed LIMIT input for sorts. · jrtkcoder/phpredis@1eef6a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1eef6a6

Browse files
committed
Fixed LIMIT input for sorts.
1 parent d8de200 commit 1eef6a6

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

redis.c

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,26 +2341,38 @@ PHP_METHOD(Redis, sort) {
23412341
zval **z_offset_pp, **z_count_pp;
23422342
// get the two values from the table, check that they are indeed of LONG type
23432343
if(SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(z_cur), 0, (void**)&z_offset_pp) &&
2344-
SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(z_cur), 1, (void**)&z_count_pp) &&
2345-
Z_TYPE_PP(z_offset_pp) == IS_LONG &&
2346-
Z_TYPE_PP(z_count_pp) == IS_LONG) {
2347-
2348-
long limit_low = Z_LVAL_PP(z_offset_pp);
2349-
long limit_high = Z_LVAL_PP(z_count_pp);
2350-
2351-
old_cmd = cmd;
2352-
cmd_len = redis_cmd_format(&cmd, "%s"
2353-
"$5" _NL
2354-
"LIMIT" _NL
2355-
"$%d" _NL
2356-
"%d" _NL
2357-
"$%d" _NL
2358-
"%d" _NL
2359-
, cmd, cmd_len
2360-
, integer_length(limit_low), limit_low
2361-
, integer_length(limit_high), limit_high);
2362-
elements += 3;
2363-
efree(old_cmd);
2344+
SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(z_cur), 1, (void**)&z_count_pp)) {
2345+
2346+
long limit_low, limit_high;
2347+
if((Z_TYPE_PP(z_offset_pp) == IS_LONG || Z_TYPE_PP(z_offset_pp) == IS_STRING) &&
2348+
(Z_TYPE_PP(z_count_pp) == IS_LONG || Z_TYPE_PP(z_count_pp) == IS_STRING)) {
2349+
2350+
2351+
if(Z_TYPE_PP(z_offset_pp) == IS_LONG) {
2352+
limit_low = Z_LVAL_PP(z_offset_pp);
2353+
} else {
2354+
limit_low = atol(Z_STRVAL_PP(z_offset_pp));
2355+
}
2356+
if(Z_TYPE_PP(z_count_pp) == IS_LONG) {
2357+
limit_high = Z_LVAL_PP(z_count_pp);
2358+
} else {
2359+
limit_high = atol(Z_STRVAL_PP(z_count_pp));
2360+
}
2361+
2362+
old_cmd = cmd;
2363+
cmd_len = redis_cmd_format(&cmd, "%s"
2364+
"$5" _NL
2365+
"LIMIT" _NL
2366+
"$%d" _NL
2367+
"%d" _NL
2368+
"$%d" _NL
2369+
"%d" _NL
2370+
, cmd, cmd_len
2371+
, integer_length(limit_low), limit_low
2372+
, integer_length(limit_high), limit_high);
2373+
elements += 3;
2374+
efree(old_cmd);
2375+
}
23642376
}
23652377
}
23662378
}

tests/TestRedis.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,8 @@ public function testSortAsc() {
759759
$this->assertEquals(array_slice($byAgeAsc, 0, 3), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', NULL, 3)); // NULL is transformed to 0 if there is something after it.
760760
$this->assertEquals($byAgeAsc, $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 0, 4));
761761
$this->assertEquals($byAgeAsc, $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array(0, 4))));
762+
$this->assertEquals($byAgeAsc, $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array(0, "4")))); // with strings
763+
$this->assertEquals($byAgeAsc, $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array("0", 4))));
762764
$this->assertEquals(array(), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', NULL, NULL)); // NULL, NULL is the same as (0,0). That returns no element.
763765

764766
// sort by salary and get ages
@@ -2547,12 +2549,14 @@ public function testSerializerPHP() {
25472549

25482550
public function testSerializerIGBinary() {
25492551

2550-
$this->checkSerializer(Redis::SERIALIZER_IGBINARY);
2552+
if(defined('Redis::SERIALIZER_IGBINARY')) {
2553+
$this->checkSerializer(Redis::SERIALIZER_IGBINARY);
25512554

2552-
// with prefix
2553-
$this->redis->setOption(Redis::OPT_PREFIX, "test:");
2554-
$this->checkSerializer(Redis::SERIALIZER_IGBINARY);
2555-
$this->redis->setOption(Redis::OPT_PREFIX, "");
2555+
// with prefix
2556+
$this->redis->setOption(Redis::OPT_PREFIX, "test:");
2557+
$this->checkSerializer(Redis::SERIALIZER_IGBINARY);
2558+
$this->redis->setOption(Redis::OPT_PREFIX, "");
2559+
}
25562560
}
25572561

25582562
private function checkSerializer($mode) {

0 commit comments

Comments
 (0)
0