Skip to content

Commit 3e922bf

Browse files
committed
Merge branch 'PHP-8.1'
* PHP-8.1: Fix GH-9008: mb_detect_encoding(): wrong results with null $encodings
2 parents b358834 + c2bdaa4 commit 3e922bf

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

ext/mbstring/mbstring.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,6 +2644,13 @@ PHP_FUNCTION(mb_strtolower)
26442644
}
26452645
/* }}} */
26462646

2647+
static const mbfl_encoding **duplicate_elist(const mbfl_encoding **elist, size_t size)
2648+
{
2649+
const mbfl_encoding **new_elist = safe_emalloc(size, sizeof(mbfl_encoding*), 0);
2650+
memcpy(ZEND_VOIDP(new_elist), elist, size * sizeof(mbfl_encoding*));
2651+
return new_elist;
2652+
}
2653+
26472654
/* {{{ Encodings of the given string is returned (as a string) */
26482655
PHP_FUNCTION(mb_detect_encoding)
26492656
{
@@ -2657,7 +2664,6 @@ PHP_FUNCTION(mb_detect_encoding)
26572664
const mbfl_encoding *ret;
26582665
const mbfl_encoding **elist;
26592666
size_t size;
2660-
bool free_elist;
26612667

26622668
ZEND_PARSE_PARAMETERS_START(1, 3)
26632669
Z_PARAM_STRING(str, str_len)
@@ -2671,16 +2677,13 @@ PHP_FUNCTION(mb_detect_encoding)
26712677
if (FAILURE == php_mb_parse_encoding_array(encoding_ht, &elist, &size, 2)) {
26722678
RETURN_THROWS();
26732679
}
2674-
free_elist = 1;
26752680
} else if (encoding_str) {
26762681
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)) {
26772682
RETURN_THROWS();
26782683
}
2679-
free_elist = 1;
26802684
} else {
2681-
elist = MBSTRG(current_detect_order_list);
2685+
elist = duplicate_elist(MBSTRG(current_detect_order_list), MBSTRG(current_detect_order_list_size));
26822686
size = MBSTRG(current_detect_order_list_size);
2683-
free_elist = 0;
26842687
}
26852688

26862689
if (size == 0) {
@@ -2689,12 +2692,10 @@ PHP_FUNCTION(mb_detect_encoding)
26892692
RETURN_THROWS();
26902693
}
26912694

2692-
if (free_elist) {
2693-
remove_non_encodings_from_elist(elist, &size);
2694-
if (size == 0) {
2695-
efree(ZEND_VOIDP(elist));
2696-
RETURN_FALSE;
2697-
}
2695+
remove_non_encodings_from_elist(elist, &size);
2696+
if (size == 0) {
2697+
efree(ZEND_VOIDP(elist));
2698+
RETURN_FALSE;
26982699
}
26992700

27002701
if (ZEND_NUM_ARGS() < 3) {
@@ -2711,9 +2712,7 @@ PHP_FUNCTION(mb_detect_encoding)
27112712
ret = mbfl_identify_encoding(&string, elist, size, strict);
27122713
}
27132714

2714-
if (free_elist) {
2715-
efree(ZEND_VOIDP(elist));
2716-
}
2715+
efree(ZEND_VOIDP(elist));
27172716

27182717
if (ret == NULL) {
27192718
RETURN_FALSE;

ext/mbstring/tests/gh9008.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
GH-9008 (mb_detect_encoding(): wrong results with null $encodings)
3+
--EXTENSIONS--
4+
mbstring
5+
--FILE--
6+
<?php
7+
$string = "<?php
8+
9+
function test()
10+
{
11+
12+
}
13+
";
14+
15+
mb_detect_order(["ASCII", "UUENCODE"]);
16+
17+
var_dump(
18+
mb_detect_encoding($string, null, true),
19+
mb_detect_encoding($string, mb_detect_order(), true),
20+
);
21+
?>
22+
--EXPECT--
23+
string(5) "ASCII"
24+
string(5) "ASCII"

0 commit comments

Comments
 (0)