Skip to content

Commit

Permalink
The key length check was not taking the prefix into account
Browse files Browse the repository at this point in the history
  • Loading branch information
rlerdorf committed Mar 7, 2024
1 parent dfd038f commit 7f31d86
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion php_memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ zend_bool s_memc_valid_key_binary(zend_string *key)
return memchr(ZSTR_VAL(key), '\n', ZSTR_LEN(key)) == NULL;
}

static
uint32_t s_memc_object_key_max_length(php_memc_object_t *intern) {
memcached_return retval;
char *result;

result = memcached_callback_get(intern->memc, MEMCACHED_CALLBACK_PREFIX_KEY, &retval);
if (retval == MEMCACHED_SUCCESS && result) {
return MEMC_OBJECT_KEY_MAX_LENGTH - strlen(result);
} else {
return MEMC_OBJECT_KEY_MAX_LENGTH;
}
}

static
zend_bool s_memc_valid_key_ascii(zend_string *key)
{
Expand All @@ -245,7 +258,7 @@ zend_bool s_memc_valid_key_ascii(zend_string *key)

#define MEMC_CHECK_KEY(intern, key) \
if (UNEXPECTED(ZSTR_LEN(key) == 0 || \
ZSTR_LEN(key) > MEMC_OBJECT_KEY_MAX_LENGTH || \
ZSTR_LEN(key) > s_memc_object_key_max_length(intern) || \
(memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) \
? !s_memc_valid_key_binary(key) \
: !s_memc_valid_key_ascii(key) \
Expand Down

0 comments on commit 7f31d86

Please sign in to comment.