Skip to content

Commit 39a01ac

Browse files
Return false or NULL on empty lpos response (#2151)
Return false or NULL on empty lpos response To be consistent with other PhpRedis methods, we should return either false or NULL when LPOS returns no results, depening on NULL_MBULK_AS_NULL setting.
1 parent 59053f1 commit 39a01ac

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

library.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,7 @@ redis_lpos_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z
13781378
int i, numElems;
13791379
size_t len;
13801380
zval z_ret;
1381+
long lval;
13811382

13821383
if (redis_sock_gets(redis_sock, inbuf, sizeof(inbuf), &len) < 0) {
13831384
goto failure;
@@ -1387,7 +1388,14 @@ redis_lpos_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z
13871388
if (*inbuf != TYPE_INT && *inbuf != TYPE_BULK) {
13881389
goto failure;
13891390
}
1390-
ZVAL_LONG(&z_ret, atol(inbuf + 1));
1391+
lval = atol(inbuf + 1);
1392+
if (lval > -1) {
1393+
ZVAL_LONG(&z_ret, lval);
1394+
} else if (redis_sock->null_mbulk_as_null) {
1395+
ZVAL_NULL(&z_ret);
1396+
} else {
1397+
ZVAL_FALSE(&z_ret);
1398+
}
13911399
} else if (ctx == PHPREDIS_CTX_PTR) {
13921400
if (*inbuf != TYPE_MULTIBULK) {
13931401
goto failure;

redis.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function lMove(string $src, string $dst, string $wherefrom, string $where
241241

242242
public function lPop(string $key, int $count = 0): bool|string|array;
243243

244-
public function lPos(string $key, mixed $value, array $options = null): bool|int|array;
244+
public function lPos(string $key, mixed $value, array $options = null): null|bool|int|array;
245245

246246
/**
247247
* @param mixed $elements

redis_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: b9da355c27e6fb1b776164d40a521703e31713b5 */
2+
* Stub hash: 2b1fc18e5c464c551df8572363972769b2ec1096 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")
@@ -417,7 +417,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Redis_lPop, 0, 1, MAY_BE_B
417417
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, count, IS_LONG, 0, "0")
418418
ZEND_END_ARG_INFO()
419419

420-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Redis_lPos, 0, 2, MAY_BE_BOOL|MAY_BE_LONG|MAY_BE_ARRAY)
420+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Redis_lPos, 0, 2, MAY_BE_NULL|MAY_BE_BOOL|MAY_BE_LONG|MAY_BE_ARRAY)
421421
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
422422
ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0)
423423
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")

redis_legacy_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: b9da355c27e6fb1b776164d40a521703e31713b5 */
2+
* Stub hash: 2b1fc18e5c464c551df8572363972769b2ec1096 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_INFO(0, options)

tests/RedisTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,12 @@ public function testlPos()
10491049
$this->assertEquals([0, 1], $this->redis->lPos('key', 'val1', ['count' => 2]));
10501050
$this->assertEquals([0], $this->redis->lPos('key', 'val1', ['count' => 2, 'maxlen' => 1]));
10511051
$this->assertEquals([], $this->redis->lPos('key', 'val2', ['count' => 1]));
1052-
$this->assertEquals(-1, $this->redis->lPos('key', 'val2'));
1052+
1053+
foreach ([[true, NULL], [false, false]] as $optpack) {
1054+
list ($setting, $expected) = $optpack;
1055+
$this->redis->setOption(Redis::OPT_NULL_MULTIBULK_AS_NULL, $setting);
1056+
$this->assertEquals($expected, $this->redis->lPos('key', 'val2'));
1057+
}
10531058
}
10541059

10551060
// ltrim, lsize, lpop

0 commit comments

Comments
 (0)