Skip to content

Commit 3b53d28

Browse files
committed
Fix key leaks in mb_convert_encoding()
1 parent 5477d68 commit 3b53d28

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

ext/mbstring/mbstring.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,7 +3269,7 @@ MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, cons
32693269
{
32703270
HashTable *output, *chash;
32713271
zend_long idx;
3272-
zend_string *key, *key_tmp;
3272+
zend_string *key;
32733273
zval *entry, entry_tmp;
32743274
size_t ckey_len, cval_len;
32753275
char *ckey, *cval;
@@ -3289,7 +3289,8 @@ MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, cons
32893289
/* convert key */
32903290
if (key) {
32913291
ckey = php_mb_convert_encoding(ZSTR_VAL(key), ZSTR_LEN(key), _to_encoding, _from_encodings, &ckey_len);
3292-
key_tmp = zend_string_init(ckey, ckey_len, 0);
3292+
key = zend_string_init(ckey, ckey_len, 0);
3293+
efree(ckey);
32933294
}
32943295
/* convert value */
32953296
ZEND_ASSERT(entry);
@@ -3317,13 +3318,14 @@ MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, cons
33173318
case IS_OBJECT:
33183319
default:
33193320
if (key) {
3320-
efree(key_tmp);
3321+
zend_string_release(key);
33213322
}
33223323
php_error_docref(NULL, E_WARNING, "Object is not supported");
33233324
continue;
33243325
}
33253326
if (key) {
3326-
zend_hash_add(output, key_tmp, &entry_tmp);
3327+
zend_hash_add(output, key, &entry_tmp);
3328+
zend_string_release(key);
33273329
} else {
33283330
zend_hash_index_add(output, idx, &entry_tmp);
33293331
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
mb_convert_encoding() shouldn't leak keys
3+
--FILE--
4+
<?php
5+
6+
$x = "x";
7+
$array = ["foo" . $x => "bar"];
8+
mb_convert_encoding($array, 'UTF-8', 'UTF-8');
9+
var_dump($array);
10+
11+
?>
12+
--EXPECT--
13+
array(1) {
14+
["foox"]=>
15+
string(3) "bar"
16+
}

0 commit comments

Comments
 (0)