8000 Ensure we don't try and send 0 expires for SET · jrtkcoder/phpredis@01b46d0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 01b46d0

Browse files
Ensure we don't try and send 0 expires for SET
As raised by issues phpredis#579, and phpredis#640 phpredis could be made to silently revert to a "SET" call (no expiry) if a non long argument was specified for EX or PX options, as well as an invalid base timeout value. This modification changes the code so we don't even send the command to Redis in such cases, which also avoids a round trip.
1 parent 7165364 commit 01b46d0

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

redis_commands.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ int redis_key_arr_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
10081008
char *kw, char **cmd, int *cmd_len, short *slot,
10091009
void **ctx)
10101010
{
1011-
zval *z_key, *z_arr, **z_val;
1011+
zval *z_arr, **z_val;
10121012
HashTable *ht_arr;
10131013
HashPosition pos;
10141014
smart_str cmdstr = {0};
@@ -1225,23 +1225,31 @@ int redis_set_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
12251225
zend_hash_move_forward(kt))
12261226
{
12271227
// Grab key and value
1228-
type = zend_hash_get_current_key_ex(kt, &k, &ht_key_len, &idx, 0,
1229-
NULL);
1228+
type = zend_hash_get_current_key_ex(kt, &k, &ht_key_len, &idx, 0, NULL);
12301229
zend_hash_get_current_data(kt, (void**)&v);
12311230

1232-
if(type == HASH_KEY_IS_STRING && (Z_TYPE_PP(v) == IS_LONG) &&
1233-
(Z_LVAL_PP(v) > 0) && IS_EX_PX_ARG(k))
1234-
{
1231+
/* Detect PX or EX argument and validate timeout */
1232+
if (type == HASH_KEY_IS_STRING && IS_EX_PX_ARG(k)) {
1233+
/* Set expire type */
12351234
exp_type = k;
1236-
expire = Z_LVAL_PP(v);
1237-
} else if(Z_TYPE_PP(v) == IS_STRING &&
1238-
IS_NX_XX_ARG(Z_STRVAL_PP(v)))
1239-
{
1235+
1236+
/* Try to extract timeout */
1237+
if (Z_TYPE_PP(v) == IS_LONG) {
1238+
expire = Z_LVAL_PP(v);
1239+
} else if (Z_TYPE_PP(v) == IS_STRING) {
1240+
expire = atol(Z_STRVAL_PP(v));
1241+
}
1242+
1243+
/* Expiry can't be set < 1 */
1244+
if (expire < 1) retur 74EC n FAILURE;
1245+
} else if (Z_TYPE_PP(v) == IS_STRING && IS_EX_PX_ARG(k)) {
12401246
set_type = Z_STRVAL_PP(v);
12411247
}
12421248
}
12431249
} else if(z_opts && Z_TYPE_P(z_opts) == IS_LONG) {
1250+
/* Grab expiry and fail if it's < 1 */
12441251
expire = Z_LVAL_P(z_opts);
1252+
if (expire < 1) return FAILURE;
12451253
}
12461254

12471255
/* Now let's construct the command we want */

0 commit comments

Comments
 (0)
0