Skip to content

Commit 7c53d19

Browse files
committed
Fix the key validity tests to handle strings with embedded nulls
1 parent a536162 commit 7c53d19

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

Diff for: php_memcached.c

+14-8
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,30 @@ static inline php_memc_object_t *php_memc_fetch_object(zend_object *obj) {
198198
(void)memc_user_data; /* avoid unused variable warning */
199199

200200
static
201-
zend_bool s_memc_valid_key_binary(const char *key)
201+
zend_bool s_memc_valid_key_binary(zend_string *key)
202202
{
203-
return strchr(key, '\n') == NULL;
203+
return memchr(ZSTR_VAL(key), '\n', ZSTR_LEN(key)) == NULL;
204204
}
205205

206206
static
207-
zend_bool s_memc_valid_key_ascii(const char *key)
207+
zend_bool s_memc_valid_key_ascii(zend_string *key)
208208
{
209-
while (*key && !iscntrl(*key) && !isspace(*key)) ++key;
210-
return *key == '\0';
209+
const char *str = ZSTR_VAL(key);
210+
size_t len = ZSTR_LEN(key);
211+
212+
for (size_t i = 0; i < len; i++) {
213+
if (iscntrl(str[i]) || isspace(str[i]))
214+
return 0;
215+
}
216+
return 1;
211217
}
212218

213219
#define MEMC_CHECK_KEY(intern, key) \
214220
if (UNEXPECTED(ZSTR_LEN(key) == 0 || \
215221
ZSTR_LEN(key) > MEMC_OBJECT_KEY_MAX_LENGTH || \
216222
(memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) \
217-
? !s_memc_valid_key_binary(ZSTR_VAL(key)) \
218-
: !s_memc_valid_key_ascii(ZSTR_VAL(key)) \
223+
? !s_memc_valid_key_binary(key) \
224+
: !s_memc_valid_key_ascii(key) \
219225
))) { \
220226
intern->rescode = MEMCACHED_BAD_KEY_PROVIDED; \
221227
RETURN_FALSE; \
@@ -309,7 +315,7 @@ PHP_INI_MH(OnUpdateSessionPrefixString)
309315
php_error_docref(NULL, E_WARNING, "memcached.sess_prefix too long (max: %d)", MEMCACHED_MAX_KEY - 1);
310316
return FAILURE;
311317
}
312-
if (!s_memc_valid_key_ascii(ZSTR_VAL(new_value))) {
318+
if (!s_memc_valid_key_ascii(new_value)) {
313319
php_error_docref(NULL, E_WARNING, "memcached.sess_prefix cannot contain whitespace or control characters");
314320
return FAILURE;
315321
}

0 commit comments

Comments
 (0)