Skip to content

Commit ed0c0df

Browse files
nielsdosGirgias
authored andcommitted
Fix GH-10627: mb_convert_encoding crashes PHP on Windows
Fixes GH-10627 The php_mb_convert_encoding() function can return NULL on error, but this case was not handled, which led to a NULL pointer dereference and hence a crash. Closes GH-10628 Signed-off-by: George Peter Banyard <girgias@php.net>
1 parent 243865a commit ed0c0df

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ PHP NEWS
3737

3838
- MBString:
3939
. ext/mbstring: fix new_value length check. (Max Kellermann)
40+
. Fix bug GH-10627 (mb_convert_encoding crashes PHP on Windows). (nielsdos)
4041

4142
- Opcache:
4243
. Fix incorrect page_size check. (nielsdos)

ext/mbstring/mbstring.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,9 @@ MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, cons
24462446
ckey = php_mb_convert_encoding(
24472447
ZSTR_VAL(key), ZSTR_LEN(key),
24482448
to_encoding, from_encodings, num_from_encodings, &ckey_len);
2449+
if (!ckey) {
2450+
continue;
2451+
}
24492452
key = zend_string_init(ckey, ckey_len, 0);
24502453
efree(ckey);
24512454
}
@@ -2457,6 +2460,12 @@ MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, cons
24572460
cval = php_mb_convert_encoding(
24582461
Z_STRVAL_P(entry), Z_STRLEN_P(entry),
24592462
to_encoding, from_encodings, num_from_encodings, &cval_len);
2463+
if (!cval) {
2464+
if (key) {
2465+
zend_string_release(key);
2466+
}
2467+
continue;
2468+
}
24602469
ZVAL_STRINGL(&entry_tmp, cval, cval_len);
24612470
efree(cval);
24622471
break;

ext/mbstring/tests/gh10627.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
GH-10627 (mb_convert_encoding crashes PHP on Windows)
3+
--EXTENSIONS--
4+
mbstring
5+
--FILE--
6+
<?php
7+
8+
$str = 'Sökinställningar';
9+
$data = [$str, 'abc'];
10+
var_dump(mb_convert_encoding($data, 'UTF-8', 'auto'));
11+
$data = [$str => 'abc', 'abc' => 'def'];
12+
var_dump(mb_convert_encoding($data, 'UTF-8', 'auto'));
13+
14+
?>
15+
--EXPECTF--
16+
Warning: mb_convert_encoding(): Unable to detect character encoding in %s on line %d
17+
array(1) {
18+
[1]=>
19+
string(3) "abc"
20+
}
21+
22+
Warning: mb_convert_encoding(): Unable to detect character encoding in %s on line %d
23+
array(1) {
24+
["abc"]=>
25+
string(3) "def"
26+
}

0 commit comments

Comments
 (0)