Skip to content

Commit fa0e11f

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix zend_string leak on case-variant duplicate setcookie() options
2 parents ddbf829 + 0c52780 commit fa0e11f

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

ext/standard/head.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,23 @@ static zend_result php_head_parse_cookie_options_array(HashTable *options, zend_
209209
if (zend_string_equals_literal_ci(key, "expires")) {
210210
*expires = zval_get_long(value);
211211
} else if (zend_string_equals_literal_ci(key, "path")) {
212+
if (*path) {
213+
zend_string_release(*path);
214+
}
212215
*path = zval_get_string(value);
213216
} else if (zend_string_equals_literal_ci(key, "domain")) {
217+
if (*domain) {
218+
zend_string_release(*domain);
219+
}
214220
*domain = zval_get_string(value);
215221
} else if (zend_string_equals_literal_ci(key, "secure")) {
216222
*secure = zval_is_true(value);
217223
} else if (zend_string_equals_literal_ci(key, "httponly")) {
218224
*httponly = zval_is_true(value);
219225
} else if (zend_string_equals_literal_ci(key, "samesite")) {
226+
if (*samesite) {
227+
zend_string_release(*samesite);
228+
}
220229
*samesite = zval_get_string(value);
221230
} else if (zend_string_equals_literal_ci(key, "partitioned")) {
222231
*partitioned = zval_is_true(value);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
setcookie() does not leak when an option array has case-variant duplicate keys
3+
--FILE--
4+
<?php
5+
$base = memory_get_usage();
6+
for ($i = 0; $i < 5000; $i++) {
7+
@setcookie('name', 'value', ['path' => '/aaaaaaaaaaaaaaaa' . $i, 'Path' => '/bbbbbbbbbbbbbbbb' . $i]);
8+
header_remove();
9+
}
10+
// Each duplicate-key call leaked the first path string before the fix,
11+
// growing usage by tens of bytes per iteration (hundreds of KB here).
12+
var_dump(memory_get_usage() - $base < 50000);
13+
?>
14+
--EXPECT--
15+
bool(true)

0 commit comments

Comments
 (0)