diff options
author | Michael Grunder <michael.grunder@gmail.com> | 2022-09-18 11:21:20 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-18 11:21:20 +0300 |
commit | 39a01ac7b5a767185e59edb41a7046021e8af844 (patch) | |
tree | fba6eb67a050fb4fb120b353655a7bbd807d46c5 | |
parent | 59053f10d975840bf19ac43e472651c68b9e9aa6 (diff) |
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.
-rw-r--r-- | library.c | 10 | ||||
-rw-r--r-- | redis.stub.php | 2 | ||||
-rw-r--r-- | redis_arginfo.h | 4 | ||||
-rw-r--r-- | redis_legacy_arginfo.h | 2 | ||||
-rw-r--r-- | tests/RedisTest.php | 7 |
5 files changed, 19 insertions, 6 deletions
@@ -1378,6 +1378,7 @@ redis_lpos_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z int i, numElems; size_t len; zval z_ret; + long lval; if (redis_sock_gets(redis_sock, inbuf, sizeof(inbuf), &len) < 0) { goto failure; @@ -1387,7 +1388,14 @@ redis_lpos_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z if (*inbuf != TYPE_INT && *inbuf != TYPE_BULK) { goto failure; } - ZVAL_LONG(&z_ret, atol(inbuf + 1)); + lval = atol(inbuf + 1); + if (lval > -1) { + ZVAL_LONG(&z_ret, lval); + } else if (redis_sock->null_mbulk_as_null) { + ZVAL_NULL(&z_ret); + } else { + ZVAL_FALSE(&z_ret); + } } else if (ctx == PHPREDIS_CTX_PTR) { if (*inbuf != TYPE_MULTIBULK) { goto failure; diff --git a/redis.stub.php b/redis.stub.php index ba8febf1..57e48d01 100644 --- a/redis.stub.php +++ b/redis.stub.php @@ -241,7 +241,7 @@ class Redis { public function lPop(string $key, int $count = 0): bool|string|array; - public function lPos(string $key, mixed $value, array $options = null): bool|int|array; + public function lPos(string $key, mixed $value, array $options = null): null|bool|int|array; /** * @param mixed $elements diff --git a/redis_arginfo.h b/redis_arginfo.h index fff2314d..ea5ee288 100644 --- a/redis_arginfo.h +++ b/redis_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: b9da355c27e6fb1b776164d40a521703e31713b5 */ + * Stub hash: 2b1fc18e5c464c551df8572363972769b2ec1096 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0) 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 ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, count, IS_LONG, 0, "0") ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Redis_lPos, 0, 2, MAY_BE_BOOL|MAY_BE_LONG|MAY_BE_ARRAY) +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) ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null") diff --git a/redis_legacy_arginfo.h b/redis_legacy_arginfo.h index 4e744ee6..cacf75f3 100644 --- a/redis_legacy_arginfo.h +++ b/redis_legacy_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: b9da355c27e6fb1b776164d40a521703e31713b5 */ + * Stub hash: 2b1fc18e5c464c551df8572363972769b2ec1096 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0) ZEND_ARG_INFO(0, options) diff --git a/tests/RedisTest.php b/tests/RedisTest.php index 5e234de6..49383d47 100644 --- a/tests/RedisTest.php +++ b/tests/RedisTest.php @@ -1049,7 +1049,12 @@ class Redis_Test extends TestSuite $this->assertEquals([0, 1], $this->redis->lPos('key', 'val1', ['count' => 2])); $this->assertEquals([0], $this->redis->lPos('key', 'val1', ['count' => 2, 'maxlen' => 1])); $this->assertEquals([], $this->redis->lPos('key', 'val2', ['count' => 1])); - $this->assertEquals(-1, $this->redis->lPos('key', 'val2')); + + foreach ([[true, NULL], [false, false]] as $optpack) { + list ($setting, $expected) = $optpack; + $this->redis->setOption(Redis::OPT_NULL_MULTIBULK_AS_NULL, $setting); + $this->assertEquals($expected, $this->redis->lPos('key', 'val2')); + } } // ltrim, lsize, lpop |