Skip to content

Commit 9be31a5

Browse files
committed
Fix #79154: mb_convert_encoding() can modify $from_encoding
We must not modify arrays passed by value.
1 parent 5a9475f commit 9be31a5

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ PHP NEWS
1111
. Fixed bug #79078 (Hypothetical use-after-free in curl_multi_add_handle()).
1212
(cmb)
1313

14+
- MBString:
15+
. Fixed bug #79154 (mb_convert_encoding() can modify $from_encoding). (cmb)
16+
1417
- MySQLnd:
1518
. Fixed bug #79084 (mysqlnd may fetch wrong column indexes with MYSQLI_BOTH).
1619
(cmb)

ext/mbstring/mbstring.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,17 +3232,16 @@ PHP_FUNCTION(mb_convert_encoding)
32323232
_from_encodings = NULL;
32333233

32343234
ZEND_HASH_FOREACH_VAL(target_hash, hash_entry) {
3235-
3236-
convert_to_string_ex(hash_entry);
3235+
zend_string *encoding_str = zval_get_string(hash_entry);
32373236

32383237
if ( _from_encodings) {
32393238
l = strlen(_from_encodings);
3240-
n = strlen(Z_STRVAL_P(hash_entry));
3239+
n = strlen(ZSTR_VAL(encoding_str));
32413240
_from_encodings = erealloc(_from_encodings, l+n+2);
32423241
memcpy(_from_encodings + l, ",", 1);
3243-
memcpy(_from_encodings + l + 1, Z_STRVAL_P(hash_entry), Z_STRLEN_P(hash_entry) + 1);
3242+
memcpy(_from_encodings + l + 1, ZSTR_VAL(encoding_str), ZSTR_LEN(encoding_str) + 1);
32443243
} else {
3245-
_from_encodings = estrdup(Z_STRVAL_P(hash_entry));
3244+
_from_encodings = estrdup(ZSTR_VAL(encoding_str));
32463245
}
32473246
} ZEND_HASH_FOREACH_END();
32483247

ext/mbstring/tests/bug79154.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
Bug 79154 (mb_convert_encoding() can modify $from_encoding)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('mbstring')) die('mbstring extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
class Utf8Encoding
10+
{
11+
public function __toString()
12+
{
13+
return 'UTF-8';
14+
}
15+
}
16+
17+
$utf8encoding = new Utf8Encoding();
18+
$encodings = [$utf8encoding];
19+
var_dump($encodings);
20+
mb_convert_encoding('foo', 'UTF-8', $encodings);
21+
var_dump($encodings);
22+
23+
?>
24+
--EXPECTF--
25+
array(1) {
26+
[0]=>
27+
object(Utf8Encoding)#%d (0) {
28+
}
29+
}
30+
array(1) {
31+
[0]=>
32+
object(Utf8Encoding)#%d (0) {
33+
}
34+
}

0 commit comments

Comments
 (0)