Skip to content

Commit

Permalink
Merge pull request #556 from rlerdorf/check_key_fix
Browse files Browse the repository at this point in the history
The key length check was not taking the prefix into account
  • Loading branch information
m6w6 authored Sep 26, 2024
2 parents d15c2e2 + 4b5decd commit 270c06b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion php_memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ zend_bool s_memc_valid_key_binary(zend_string *key)
}

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;
}
}

zend_bool s_memc_valid_key_ascii(zend_string *key, uint64_t verify_key)
{
const char *str = ZSTR_VAL(key);
Expand All @@ -252,7 +264,7 @@ zend_bool s_memc_valid_key_ascii(zend_string *key, uint64_t verify_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, memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_VERIFY_KEY)) \
Expand Down

0 comments on commit 270c06b

Please sign in to comment.