Skip to content

Fix GH-9008: mb_detect_encoding(): wrong results with null $encodings #9063

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 2 commits 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
27 changes: 13 additions & 14 deletions ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -2694,6 +2694,13 @@ PHP_FUNCTION(mb_strtolower)
}
/* }}} */

static const mbfl_encoding **duplicate_elist(const mbfl_encoding **elist, size_t size)
{
const mbfl_encoding **new_elist = safe_emalloc(size, sizeof(mbfl_encoding*), 0);
memcpy(ZEND_VOIDP(new_elist), elist, size * sizeof(mbfl_encoding*));
return new_elist;
}

/* {{{ Encodings of the given string is returned (as a string) */
PHP_FUNCTION(mb_detect_encoding)
{
Expand All @@ -2707,7 +2714,6 @@ PHP_FUNCTION(mb_detect_encoding)
const mbfl_encoding *ret;
const mbfl_encoding **elist;
size_t size;
bool free_elist;

ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STRING(str, str_len)
Expand All @@ -2721,16 +2727,13 @@ PHP_FUNCTION(mb_detect_encoding)
if (FAILURE == php_mb_parse_encoding_array(encoding_ht, &elist, &size, 2)) {
RETURN_THROWS();
}
free_elist = 1;
} else if (encoding_str) {
if (FAILURE == php_mb_parse_encoding_list(ZSTR_VAL(encoding_str), ZSTR_LEN(encoding_str), &elist, &size, /* persistent */ 0, /* arg_num */ 2, /* allow_pass_encoding */ 0)) {
RETURN_THROWS();
}
free_elist = 1;
} else {
elist = MBSTRG(current_detect_order_list);
elist = duplicate_elist(MBSTRG(current_detect_order_list), MBSTRG(current_detect_order_list_size));
size = MBSTRG(current_detect_order_list_size);
free_elist = 0;
}

if (size == 0) {
Expand All @@ -2739,12 +2742,10 @@ PHP_FUNCTION(mb_detect_encoding)
RETURN_THROWS();
}

if (free_elist) {
remove_non_encodings_from_elist(elist, &size);
if (size == 0) {
efree(ZEND_VOIDP(elist));
RETURN_FALSE;
}
remove_non_encodings_from_elist(elist, &size);
if (size == 0) {
efree(ZEND_VOIDP(elist));
RETURN_FALSE;
}

if (ZEND_NUM_ARGS() < 3) {
Expand All @@ -2761,9 +2762,7 @@ PHP_FUNCTION(mb_detect_encoding)
ret = mbfl_identify_encoding(&string, elist, size, strict);
}

if (free_elist) {
efree(ZEND_VOIDP(elist));
}
efree(ZEND_VOIDP(elist));

if (ret == NULL) {
RETURN_FALSE;
Expand Down
24 changes: 24 additions & 0 deletions ext/mbstring/tests/gh9008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
GH-9008 (mb_detect_encoding(): wrong results with null $encodings)
--EXTENSIONS--
mbstring
--FILE--
<?php
$string = "<?php

function test()
{

}
";

mb_detect_order(["ASCII", "UUENCODE"]);

var_dump(
mb_detect_encoding($string, null, true),
mb_detect_encoding($string, mb_detect_order(), true),
);
?>
--EXPECT--
string(5) "ASCII"
string(5) "ASCII"