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

Skip to content

Commit aa0938a

Browse files
Rework ZRANGE argument handling.
Rework argument parsing for `ZRANGE` so we can pass either a string or an integer so everything will work even when using strict types. Additionally update our docs to use the correct mechanism for adding the `BYSCORE` option. Fixes #2291
1 parent 204a02c commit aa0938a

5 files changed

Lines changed: 34 additions & 24 deletions

File tree

library.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,16 +1073,23 @@ redis_cmd_append_sstr_dbl(smart_string *str, double value)
10731073
return redis_cmd_append_sstr(str, tmp, len);
10741074
}
10751075

1076-
/* Append a zval to a redis command. The value will be serialized if we are
1077-
* configured to do that */
1076+
/* Append a zval to a redis command. If redis_sock is passed as non-null we will
1077+
* the value may be serialized, if we're configured to do that. */
10781078
int redis_cmd_append_sstr_zval(smart_string *str, zval *z, RedisSock *redis_sock) {
1079-
char *val;
1080-
size_t vallen;
10811079
int valfree, retval;
1080+
zend_string *zstr;
1081+
size_t vallen;
1082+
char *val;
10821083

1083-
valfree = redis_pack(redis_sock, z, &val, &vallen);
1084-
retval = redis_cmd_append_sstr(str, val, vallen);
1085-
if (valfree) efree(val);
1084+
if (redis_sock != NULL) {
1085+
valfree = redis_pack(redis_sock, z, &val, &vallen);
1086+
retval = redis_cmd_append_sstr(str, val, vallen);
1087+
if (valfree) efree(val);
1088+
} else {
1089+
zstr = zval_get_string(z);
1090+
retval = redis_cmd_append_sstr_zstr(str, zstr);
1091+
zend_string_release(zstr);
1092+
}
10861093

10871094
return retval;
10881095
}

redis.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4108,9 +4108,9 @@ public function zPopMin(string $key, int $count = null): Redis|array|false;
41084108
* @category zset
41094109
*
41104110
* @example $redis->zRange('zset', 0, -1);
4111-
* @example $redis->zRange('zset', '-inf', 'inf', ['byscore' => true]);
4111+
* @example $redis->zRange('zset', '-inf', 'inf', ['byscore']);
41124112
*/
4113-
public function zRange(string $key, mixed $start, mixed $end, array|bool|null $options = null): Redis|array|false;
4113+
public function zRange(string $key, string|int $start, string|int $end, array|bool|null $options = null): Redis|array|false;
41144114

41154115
/**
41164116
* Retrieve a range of elements from a sorted set by legographical range.

redis_arginfo.h

Lines changed: 3 additions & 3 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: 2d2bd3a96ba44622f4157ee2e06695ffb87a4949 */
2+
* Stub hash: b0d5c56084a89230807e6ba582d2fab536d2e897 */
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")
@@ -1028,8 +1028,8 @@ ZEND_END_ARG_INFO()
10281028

10291029
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_zRange, 0, 3, Redis, MAY_BE_ARRAY|MAY_BE_FALSE)
10301030
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
1031-
ZEND_ARG_TYPE_INFO(0, start, IS_MIXED, 0)
1032-
ZEND_ARG_TYPE_INFO(0, end, IS_MIXED, 0)
1031+
ZEND_ARG_TYPE_MASK(0, start, MAY_BE_STRING|MAY_BE_LONG, NULL)
1032+
ZEND_ARG_TYPE_MASK(0, end, MAY_BE_STRING|MAY_BE_LONG, NULL)
10331033
ZEND_ARG_TYPE_MASK(0, options, MAY_BE_ARRAY|MAY_BE_BOOL|MAY_BE_NULL, "null")
10341034
ZEND_END_ARG_INFO()
10351035

redis_commands.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -746,20 +746,23 @@ static int redis_get_zcmd_flags(const char *kw) {
746746
}
747747

748748
/* Validate ZLEX* min/max argument strings */
749-
#define validate_zlex_arg_zstr(zs_) validate_zlex_arg(ZSTR_VAL((zs_)), ZSTR_LEN((zs_)))
750-
static int validate_zlex_arg(const char *arg, size_t len) {
751-
return (len > 1 && (*arg == '[' || *arg == '(')) ||
752-
(len == 1 && (*arg == '+' || *arg == '-'));
749+
static int validate_zlex_arg(const char *str, size_t len) {
750+
return (len > 1 && (*str == '[' || *str == '(')) ||
751+
(len == 1 && (*str == '+' || *str == '-'));
752+
}
753+
754+
static int validate_zlex_arg_zval(zval *z) {
755+
return Z_TYPE_P(z) == IS_STRING && validate_zlex_arg(Z_STRVAL_P(z), Z_STRLEN_P(z));
753756
}
754757

755758
int redis_zrange_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
756759
char *kw, char **cmd, int *cmd_len, short *slot,
757760
void **ctx)
758761
{
759-
zend_string *dst = NULL, *src = NULL, *sstart = NULL, *send = NULL;
762+
zval *zoptions = NULL, *zstart, *zend;
763+
zend_string *dst = NULL, *src = NULL;
760764
zend_long start = 0, end = 0;
761765
smart_string cmdstr = {0};
762-
zval *zoptions = NULL;
763766
redisZcmdOptions opt;
764767
int min_argc, flags;
765768
short slot2;
@@ -776,8 +779,8 @@ int redis_zrange_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
776779
Z_PARAM_LONG(start)
777780
Z_PARAM_LONG(end)
778781
} else {
779-
Z_PARAM_STR(sstart)
780-
Z_PARAM_STR(send)
782+
Z_PARAM_ZVAL(zstart)
783+
Z_PARAM_ZVAL(zend)
781784
}
782785
Z_PARAM_OPTIONAL
783786
Z_PARAM_ZVAL_OR_NULL(zoptions)
@@ -787,7 +790,7 @@ int redis_zrange_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
787790

788791
if (opt.bylex) {
789792
ZEND_ASSERT(!(flags & REDIS_ZCMD_INT_RANGE));
790-
if (!validate_zlex_arg_zstr(sstart) || !validate_zlex_arg_zstr(send)) {
793+
if (!validate_zlex_arg_zval(zstart) || !validate_zlex_arg_zval(zend)) {
791794
php_error_docref(NULL, E_WARNING, "Legographical args must start with '[' or '(' or be '+' or '-'");
792795
return FAILURE;
793796
}
@@ -812,8 +815,8 @@ int redis_zrange_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
812815
redis_cmd_append_sstr_long(&cmdstr, start);
813816
redis_cmd_append_sstr_long(&cmdstr, end);
814817
} else {
815-
redis_cmd_append_sstr_zstr(&cmdstr, sstart);
816-
redis_cmd_append_sstr_zstr(&cmdstr, send);
818+
redis_cmd_append_sstr_zval(&cmdstr, zstart, NULL);
819+
redis_cmd_append_sstr_zval(&cmdstr, zend, NULL);
817820
}
818821

819822
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, opt.byscore, "BYSCORE");

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: 2d2bd3a96ba44622f4157ee2e06695ffb87a4949 */
2+
* Stub hash: b0d5c56084a89230807e6ba582d2fab536d2e897 */
33

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

0 commit comments

Comments
 (0)