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

Skip to content

Commit 9ad4b9e

Browse files
committed
Fixed LIMIT input for sorts.
1 parent d7fc396 commit 9ad4b9e

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
@@ -2343,26 +2343,38 @@ PHP_METHOD(Redis, sort) {
23432343
zval **z_offset_pp, **z_count_pp;
23442344
// get the two values from the table, check that they are indeed of LONG type
23452345
if(SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(z_cur), 0, (void**)&z_offset_pp) &&
2346-
SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(z_cur), 1, (void**)&z_count_pp) &&
2347-
Z_TYPE_PP(z_offset_pp) == IS_LONG &&
2348-
Z_TYPE_PP(z_count_pp) == IS_LONG) {
2349-
2350-
long limit_low = Z_LVAL_PP(z_offset_pp);
2351-
long limit_high = Z_LVAL_PP(z_count_pp);
2352-
2353-
old_cmd = cmd;
2354-
cmd_len = redis_cmd_format(&cmd, "%s"
2355-
"$5" _NL
2356-
"LIMIT" _NL
2357-
"$%d" _NL
2358-
"%d" _NL
2359-
"$%d" _NL
2360-
"%d" _NL
2361-
, cmd, cmd_len
2362-
, integer_length(limit_low), limit_low
2363-
, integer_length(limit_high), limit_high);
2364-
elements += 3;
2365-
efree(old_cmd);
2346+
SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(z_cur), 1, (void**)&z_count_pp)) {
2347+
2348+
long limit_low, limit_high;
2349+
if((Z_TYPE_PP(z_offset_pp) == IS_LONG || Z_TYPE_PP(z_offset_pp) == IS_STRING) &&
2350+
(Z_TYPE_PP(z_count_pp) == IS_LONG || Z_TYPE_PP(z_count_pp) == IS_STRING)) {
2351+
2352+
2353+
if(Z_TYPE_PP(z_offset_pp) == IS_LONG) {
2354+
limit_low = Z_LVAL_PP(z_offset_pp);
2355+
} else {
2356+
limit_low = atol(Z_STRVAL_PP(z_offset_pp));
2357+
}
2358+
if(Z_TYPE_PP(z_count_pp) == IS_LONG) {
2359+
limit_high = Z_LVAL_PP(z_count_pp);
2360+
} else {
2361+
limit_high = atol(Z_STRVAL_PP(z_count_pp));
2362+
}
2363+
2364+
old_cmd = cmd;
2365+
cmd_len = redis_cmd_format(&cmd, "%s"
2366+
"$5" _NL
2367+
"LIMIT" _NL
2368+
"$%d" _NL
2369+
"%d" _NL
2370+
"$%d" _NL
2371+
"%d" _NL
2372+
, cmd, cmd_len
2373+
, integer_length(limit_low), limit_low
2374+
, integer_length(limit_high), limit_high);
2375+
elements += 3;
2376+
efree(old_cmd);
2377+
}
23662378
}
23672379
}
23682380
}

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)