Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_MSGPACK); // Use msg
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_JSON); // Use JSON to serialize/unserialize
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHPSER); // Use phpser serialize/unserialize (signed when phpser.key is set)

// Opt in to rejecting writes when serialization throws (default: false in 6.x).
$redis->setOption(Redis::OPT_STRICT_SERIALIZATION, true);

$redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys

/* Options for the SCAN family of commands, indicating whether to abstract
Expand All @@ -482,6 +485,15 @@ $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_PREFIX);
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_NOPREFIX);
~~~

`OPT_STRICT_SERIALIZATION` also applies to `RedisCluster`. When enabled, a
serialization exception leaves the failed command's keys and TTLs unchanged.
Cluster `mSet()` and `mSetNX()` serialize every value before sending any of the
call's batches. Commands queued by earlier calls remain available to `exec()`.
Preparing these multi-key calls requires additional memory for the packed values.
This option does not make operations atomic across slots when server or
connection errors occur. When disabled, legacy 6.x behavior is preserved,
including writes of empty or partial values after serialization exceptions.


#### getOption
-----
Expand Down
5 changes: 5 additions & 0 deletions cluster_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,11 @@ PHP_REDIS_API short cluster_send_command(redisCluster *c, short slot, const char
{
int resp, timedout = 0;

/* Command construction can invoke serializers that throw. */
if (UNEXPECTED(redis_serialization_failed(c->flags))) {
return -1;
}

if (!cluster_slot(c, slot)) {
zend_throw_exception_ex(redis_cluster_exception_ce, 0,
"The slot %d is not covered by any node in this cluster", slot);
Expand Down
2 changes: 2 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ typedef enum _PUBSUB_TYPE {
#define REDIS_OPT_BACKOFF_BASE 13
#define REDIS_OPT_BACKOFF_CAP 14
#define REDIS_OPT_PACK_IGNORE_NUMBERS 15
#define REDIS_OPT_STRICT_SERIALIZATION 16

/* cluster options */
#define REDIS_FAILOVER_NONE 0
Expand Down Expand Up @@ -275,6 +276,7 @@ typedef struct {
HashTable *subs[REDIS_SUBS_BUCKETS];
redis_serializer serializer;
zend_bool pack_ignore_numbers;
zend_bool strict_serialization;
int compression;
int compression_level;
long dbNumber;
Expand Down
50 changes: 26 additions & 24 deletions library.c
Original file line number Diff line number Diff line change
Expand Up @@ -4314,7 +4314,7 @@ static int serialize_generic_zval(char **dst, size_t *len, zval *zsrc) {
PHP_REDIS_API int
redis_pack(const RedisSock *redis_sock, zval *z, char **val, size_t *val_len) {
size_t tmplen;
int tmpfree;
int valfree;
char *tmp;

/* Don't pack actual numbers if the user asked us not to */
Expand All @@ -4325,15 +4325,19 @@ redis_pack(const RedisSock *redis_sock, zval *z, char **val, size_t *val_len) {
}

/* First serialize */
tmpfree = redis_serialize(redis_sock, z, &tmp, &tmplen);
valfree = redis_serialize(redis_sock, z, val, val_len);

/* Now attempt compression */
if (redis_compress(redis_sock, val, val_len, tmp, tmplen)) {
if (tmpfree) efree(tmp);
if (!redis_serialization_failed(redis_sock) &&
redis_compress(redis_sock, &tmp, &tmplen, *val, *val_len))
{
if (valfree) efree(*val);
*val = tmp;
*val_len = tmplen;
return 1;
}

return tmpfree;
return valfree;
}

static void
Expand Down Expand Up @@ -4416,6 +4420,12 @@ redis_serialize(const RedisSock *redis_sock, zval *z, char **val, size_t *val_le

*val = "";
*val_len = 0;

/* Do not invoke another serializer after an earlier argument failed. */
if (UNEXPECTED(redis_serialization_failed(redis_sock))) {
return 0;
}

switch(redis_sock->serializer) {
case REDIS_SERIALIZER_NONE:
switch(Z_TYPE_P(z)) {
Expand Down Expand Up @@ -4443,24 +4453,11 @@ redis_serialize(const RedisSock *redis_sock, zval *z, char **val, size_t *val_le
php_var_serialize(&sstr, z, &ht);
PHP_VAR_SERIALIZE_DESTROY(ht);

if (!sstr.s) {
break;
}
*val = estrndup(ZSTR_VAL(sstr.s), ZSTR_LEN(sstr.s));
*val_len = ZSTR_LEN(sstr.s);

smart_str_free(&sstr);

return 1;
break;

case REDIS_SERIALIZER_MSGPACK:
#ifdef HAVE_REDIS_MSGPACK
php_msgpack_serialize(&sstr, z);
*val = estrndup(ZSTR_VAL(sstr.s), ZSTR_LEN(sstr.s));
*val_len = ZSTR_LEN(sstr.s);
smart_str_free(&sstr);

return 1;
#endif
break;
case REDIS_SERIALIZER_IGBINARY:
Expand Down Expand Up @@ -4488,17 +4485,22 @@ redis_serialize(const RedisSock *redis_sock, zval *z, char **val, size_t *val_le
case REDIS_SERIALIZER_JSON:
#ifdef HAVE_REDIS_JSON
php_json_encode(&sstr, z, PHP_JSON_OBJECT_AS_ARRAY);
*val = estrndup(ZSTR_VAL(sstr.s), ZSTR_LEN(sstr.s));
*val_len = ZSTR_LEN(sstr.s);
smart_str_free(&sstr);
return 1;
#endif
break;
default:
ZEND_UNREACHABLE();
}

return 0;
if (sstr.s == NULL || redis_serialization_failed(redis_sock)) {
smart_str_free(&sstr);
return 0;
}

*val = estrndup(ZSTR_VAL(sstr.s), ZSTR_LEN(sstr.s));
*val_len = ZSTR_LEN(sstr.s);
smart_str_free(&sstr);

return 1;
}

PHP_REDIS_API int
Expand Down
5 changes: 5 additions & 0 deletions library.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ int redis_sock_set_context_zval(RedisSock *redis_sock, const zval *zv);

PHP_REDIS_API int redis_sock_set_backoff(RedisSock *redis_sock, const zval *options);

static zend_always_inline zend_bool
redis_serialization_failed(const RedisSock *redis_sock) {
return redis_sock->strict_serialization && EG(exception);
}

PHP_REDIS_API int
redis_serialize(const RedisSock *redis_sock, zval *z, char **val, size_t *val_len);
/* No prefix: leave the borrowed key unchanged and return 0.
Expand Down
42 changes: 12 additions & 30 deletions redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,6 @@ pipeline_enqueue_cmd_strl(RedisSock *redis_sock, const char *cmd, int len) {
smart_string_appendl(&redis_sock->pipeline_cmd, cmd, len);
}

static zend_always_inline void
pipeline_enqueue_cmd(RedisSock *redis_sock, RedisCmd *cmd) {
pipeline_enqueue_cmd_strl(redis_sock, redis_cmd_str(cmd),
redis_cmd_len(cmd));
}

static void
redis_save_callback(RedisSock *redis_sock, FailableResultCallback cb,
RedisCmdCtx ctx)
Expand All @@ -660,22 +654,20 @@ redis_save_callback(RedisSock *redis_sock, FailableResultCallback cb,
}

static int
redis_process_request_strl(RedisSock *redis_sock, const char *cmd, int len) {
int res = SUCCESS;
redis_process_request(RedisSock *redis_sock, RedisCmd *cmd) {
/* Command construction can invoke serializers that throw. */
if (UNEXPECTED(redis_serialization_failed(redis_sock))) {
return FAILURE;
}

if (redis_sock_is_pipeline(redis_sock)) {
pipeline_enqueue_cmd_strl(redis_sock, cmd, len);
} else if (UNEXPECTED(redis_sock_write(redis_sock, cmd, len) < 0)) {
res = FAILURE;
pipeline_enqueue_cmd_strl(redis_sock, redis_cmd_str(cmd),
redis_cmd_len(cmd));
} else if (UNEXPECTED(redis_sock_write_cmd(redis_sock, cmd) < 0)) {
return FAILURE;
}

return res;
}

static int
redis_process_request(RedisSock *redis_sock, RedisCmd *cmd) {
return redis_process_request_strl(redis_sock, redis_cmd_str(cmd),
redis_cmd_len(cmd));
return SUCCESS;
}

static void
Expand All @@ -699,12 +691,7 @@ redis_process_cmd(INTERNAL_FUNCTION_PARAMETERS, redis_cmd_cb *cmd_cb,

ctx = redis_cmd_pop_ctx(cmd);

if (redis_sock_is_pipeline(redis_sock)) {
pipeline_enqueue_cmd(redis_sock, cmd);
res = SUCCESS;
} else {
res = redis_sock_write_cmd(redis_sock, cmd) < 0 ? FAILURE : SUCCESS;
}
res = redis_process_request(redis_sock, cmd);

redis_cmd_free(cmd);

Expand Down Expand Up @@ -761,12 +748,7 @@ redis_process_kw_cmd(INTERNAL_FUNCTION_PARAMETERS, const char *kw, size_t kw_len

ctx = redis_cmd_pop_ctx(cmd);

if (redis_sock_is_pipeline(redis_sock)) {
pipeline_enqueue_cmd(redis_sock, cmd);
res = SUCCESS;
} else {
res = redis_sock_write_cmd(redis_sock, cmd) < 0 ? FAILURE : SUCCESS;
}
res = redis_process_request(redis_sock, cmd);

redis_cmd_free(cmd);

Expand Down
15 changes: 15 additions & 0 deletions redis.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ class Redis {
*/
public const OPT_PACK_IGNORE_NUMBERS = UNKNOWN;

/**
* Reject a command when serialization throws, preserving existing values
* and expirations. Disabled by default for compatibility with 6.x.
*
* Also supported by RedisCluster, where mSet() and mSetNX() serialize all
* values before sending any batch. Commands queued by earlier calls remain
* in the transaction. This does not provide atomicity across cluster slots
* for server or connection errors.
*
* @var int
* @cvalue REDIS_OPT_STRICT_SERIALIZATION
*/
public const OPT_STRICT_SERIALIZATION = UNKNOWN;

/**
* Sets the serializer to none (no serialization).
*
Expand Down Expand Up @@ -4147,6 +4161,7 @@ public function setRange(string $key, int $index, string $value): Redis|int|fals
* | OPT_MAX_RETRIES | int | The maximum number of times Redis will attempt to reconnect if it gets disconnected, before throwing an exception. |
* | OPT_SCAN | enum | Redis::SCAN_RETRY, or Redis::SCAN_NORETRY. Whether PhpRedis should automatically SCAN again when zero keys but a nonzero iterator are returned. |
* | OPT_SERIALIZER | enum | Set the automatic data serializer.<br>`Redis::SERIALIZER_NONE`<br>`Redis::SERIALIZER_PHP`<br>`Redis::SERIALIZER_IGBINARY`<br>`Redis::SERIALIZER_MSGPACK`, `Redis::SERIALIZER_JSON`<br>`Redis::SERIALIZER_PHPSER`|
* | OPT_STRICT_SERIALIZATION | bool | Reject commands whose serialization throws. Defaults to false for 6.x compatibility. Also supported by RedisCluster. |
* | OPT_PREFIX | string | A string PhpRedis will use to prefix every key we read or write. |
* | OPT_READ_TIMEOUT | float | How long PhpRedis will block for a response from Redis before throwing a 'read error on connection' exception. |
* | OPT_TCP_KEEPALIVE | bool | Set or disable TCP_KEEPALIVE on the connection. |
Expand Down
10 changes: 9 additions & 1 deletion redis_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 2de44845003c74a5972aaf8c15e80820ebd5dcfd */
* Stub hash: 9867962b9f9cdebb2f1df9aa308d4006bc5c9053 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 1, "null")
Expand Down Expand Up @@ -1979,6 +1979,8 @@ static zend_class_entry *register_class_Redis(void)
class_entry = zend_register_internal_class_ex(&ce, NULL);
#if (PHP_VERSION_ID >= 80100)
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
#elif (PHP_VERSION_ID >= 80000)
class_entry->ce_flags |= 0;
#endif
#endif

Expand Down Expand Up @@ -2102,6 +2104,12 @@ static zend_class_entry *register_class_Redis(void)
zend_declare_class_constant_ex(class_entry, const_OPT_PACK_IGNORE_NUMBERS_name, &const_OPT_PACK_IGNORE_NUMBERS_value, ZEND_ACC_PUBLIC, NULL);
zend_string_release(const_OPT_PACK_IGNORE_NUMBERS_name);

zval const_OPT_STRICT_SERIALIZATION_value;
ZVAL_LONG(&const_OPT_STRICT_SERIALIZATION_value, REDIS_OPT_STRICT_SERIALIZATION);
zend_string *const_OPT_STRICT_SERIALIZATION_name = zend_string_init_interned("OPT_STRICT_SERIALIZATION", sizeof("OPT_STRICT_SERIALIZATION") - 1, 1);
zend_declare_class_constant_ex(class_entry, const_OPT_STRICT_SERIALIZATION_name, &const_OPT_STRICT_SERIALIZATION_value, ZEND_ACC_PUBLIC, NULL);
zend_string_release(const_OPT_STRICT_SERIALIZATION_name);

zval const_SERIALIZER_NONE_value;
ZVAL_LONG(&const_SERIALIZER_NONE_value, REDIS_SERIALIZER_NONE);
zend_string *const_SERIALIZER_NONE_name = zend_string_init_interned("SERIALIZER_NONE", sizeof("SERIALIZER_NONE") - 1, 1);
Expand Down
Loading
Loading