Skip to content

Fix GH-10627: mb_convert_encoding crashes PHP on Windows #10628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,9 @@ MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, cons
ckey = php_mb_convert_encoding(
ZSTR_VAL(key), ZSTR_LEN(key),
to_encoding, from_encodings, num_from_encodings, &ckey_len);
if (!ckey) {
continue;
}
key = zend_string_init(ckey, ckey_len, 0);
efree(ckey);
}
Expand All @@ -2457,6 +2460,12 @@ MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, cons
cval = php_mb_convert_encoding(
Z_STRVAL_P(entry), Z_STRLEN_P(entry),
to_encoding, from_encodings, num_from_encodings, &cval_len);
if (!cval) {
if (key) {
zend_string_release(key);
}
continue;
}
ZVAL_STRINGL(&entry_tmp, cval, cval_len);
efree(cval);
break;
Expand Down
26 changes: 26 additions & 0 deletions ext/mbstring/tests/gh10627.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
GH-10627 (mb_convert_encoding crashes PHP on Windows)
--EXTENSIONS--
mbstring
--FILE--
<?php

$str = 'S�kinst�llningar';
$data = [$str, 'abc'];
var_dump(mb_convert_encoding($data, 'UTF-8', 'auto'));
$data = [$str => 'abc', 'abc' => 'def'];
var_dump(mb_convert_encoding($data, 'UTF-8', 'auto'));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced --enable-debug on WSL on Ubuntu and it is seems C NULL problem. But 3v4l looks works fine and result is wrong because maybe without --enable-debug option.
@alexdowad what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. That is interesting. I'm not sure of the reason right now.

This bug occurs if 'auto' is passed as the desired encoding, and mbstring is not able to detect the encoding of the string.

I might need to check under which circumstances that can happen. In any case, this bug fix is definitely needed.

?>
--EXPECTF--
Warning: mb_convert_encoding(): Unable to detect character encoding in %s on line %d
array(1) {
[1]=>
string(3) "abc"
}

Warning: mb_convert_encoding(): Unable to detect character encoding in %s on line %d
array(1) {
["abc"]=>
string(3) "def"
}