@@ -198,24 +198,30 @@ static inline php_memc_object_t *php_memc_fetch_object(zend_object *obj) {
198
198
(void)memc_user_data; /* avoid unused variable warning */
199
199
200
200
static
201
- zend_bool s_memc_valid_key_binary (const char * key )
201
+ zend_bool s_memc_valid_key_binary (zend_string * key )
202
202
{
203
- return strchr ( key , '\n' ) == NULL ;
203
+ return memchr ( ZSTR_VAL ( key ) , '\n' , ZSTR_LEN ( key ) ) == NULL ;
204
204
}
205
205
206
206
static
207
- zend_bool s_memc_valid_key_ascii (const char * key )
207
+ zend_bool s_memc_valid_key_ascii (zend_string * key )
208
208
{
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 ;
211
217
}
212
218
213
219
#define MEMC_CHECK_KEY (intern , key ) \
214
220
if (UNEXPECTED(ZSTR_LEN(key) == 0 || \
215
221
ZSTR_LEN(key) > MEMC_OBJECT_KEY_MAX_LENGTH || \
216
222
(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) \
219
225
))) { \
220
226
intern->rescode = MEMCACHED_BAD_KEY_PROVIDED; \
221
227
RETURN_FALSE; \
@@ -309,7 +315,7 @@ PHP_INI_MH(OnUpdateSessionPrefixString)
309
315
php_error_docref (NULL , E_WARNING , "memcached.sess_prefix too long (max: %d)" , MEMCACHED_MAX_KEY - 1 );
310
316
return FAILURE ;
311
317
}
312
- if (!s_memc_valid_key_ascii (ZSTR_VAL ( new_value ) )) {
318
+ if (!s_memc_valid_key_ascii (new_value )) {
313
319
php_error_docref (NULL , E_WARNING , "memcached.sess_prefix cannot contain whitespace or control characters" );
314
320
return FAILURE ;
315
321
}
0 commit comments