Skip to content

Commit 751bbaa

Browse files
committed
Micro optimization & fixed invalid key handling
1 parent d61ff37 commit 751bbaa

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

ext/session/tests/session_set_cookie_params_variation7.phpt

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ echo "*** Testing session_set_cookie_params() : array parameter variation ***\n"
2424

2525
// Invalid cases
2626
var_dump(session_set_cookie_params([]));
27-
var_dump(session_set_cookie_params(["unknown_key" => true]));
27+
var_dump(session_set_cookie_params(["unknown_key" => true, "secure_invalid" => true]));
2828

2929
var_dump(ini_get("session.cookie_secure"));
3030
var_dump(ini_get("session.cookie_samesite"));
@@ -51,6 +51,8 @@ bool(false)
5151

5252
Warning: session_set_cookie_params(): Unrecognized key 'unknown_key' found in the options array in %s
5353

54+
Warning: session_set_cookie_params(): Unrecognized key 'secure_invalid' found in the options array in %s
55+
5456
Warning: session_set_cookie_params(): No valid keys were found in the options array in %s
5557
bool(false)
5658
string(1) "0"

ext/standard/head.c

+8-13
Original file line numberDiff line numberDiff line change
@@ -213,32 +213,28 @@ static int php_head_parse_cookie_options_array(zval *options, zend_long *expires
213213

214214
if (*path) {
215215
*path = NULL;
216-
*domain = NULL;
217-
*secure = 0;
218-
*httponly = 0;
219216
php_error_docref(NULL, E_WARNING, "Cannot pass arguments after the options array");
220217
return 0;
221218
}
222219

223220
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(options), key, value) {
224221
if (key) {
225-
ZVAL_DEREF(value);
226-
if(!strcasecmp("expires", ZSTR_VAL(key))) {
222+
if (zend_string_equals_literal_ci(key, "expires")) {
227223
*expires = zval_get_long(value);
228224
found++;
229-
} else if(!strcasecmp("path", ZSTR_VAL(key))) {
225+
} else if (zend_string_equals_literal_ci(key, "path")) {
230226
*path = zval_get_string(value);
231227
found++;
232-
} else if(!strcasecmp("domain", ZSTR_VAL(key))) {
228+
} else if (zend_string_equals_literal_ci(key, "domain")) {
233229
*domain = zval_get_string(value);
234230
found++;
235-
} else if(!strcasecmp("secure", ZSTR_VAL(key))) {
231+
} else if (zend_string_equals_literal_ci(key, "secure")) {
236232
*secure = zval_is_true(value);
237233
found++;
238-
} else if(!strcasecmp("httponly", ZSTR_VAL(key))) {
234+
} else if (zend_string_equals_literal_ci(key, "httponly")) {
239235
*httponly = zval_is_true(value);
240236
found++;
241-
} else if(!strcasecmp("samesite", ZSTR_VAL(key))) {
237+
} else if (zend_string_equals_literal_ci(key, "samesite")) {
242238
*samesite = zval_get_string(value);
243239
found++;
244240
} else {
@@ -265,7 +261,7 @@ PHP_FUNCTION(setcookie)
265261
zval *expires_or_options = NULL;
266262
zend_string *name, *value = NULL, *path = NULL, *domain = NULL, *samesite = NULL;
267263
zend_long expires = 0;
268-
zend_bool secure = 0, httponly = 0, options_array = 0;
264+
zend_bool secure = 0, httponly = 0;
269265

270266
ZEND_PARSE_PARAMETERS_START(1, 7)
271267
Z_PARAM_STR(name)
@@ -280,7 +276,6 @@ PHP_FUNCTION(setcookie)
280276

281277
if (expires_or_options) {
282278
if (Z_TYPE_P(expires_or_options) == IS_ARRAY) {
283-
options_array = 1;
284279
if (!php_head_parse_cookie_options_array(expires_or_options, &expires, &path, &domain, &secure, &httponly, &samesite)) {
285280
RETVAL_FALSE;
286281
goto cleanup;
@@ -297,7 +292,7 @@ PHP_FUNCTION(setcookie)
297292
}
298293

299294
cleanup:
300-
if (options_array) {
295+
if (expires_or_options && Z_TYPE_P(expires_or_options) == IS_ARRAY) {
301296
if (path) {
302297
zend_string_release(path);
303298
}

0 commit comments

Comments
 (0)