Skip to content

Commit 115ea48

Browse files
committed
Merge branch 'PHP-8.5'
2 parents 6a51337 + e106d68 commit 115ea48

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ PHP NEWS
5656
. Added GB18030-2022 to default encoding list for zh-CN. (HeRaNO)
5757
. Fixed bug GH-20836 (Stack overflow in mb_convert_variables with
5858
recursive array references). (alexandre-daubois)
59+
. Fixed bug GH-21223; mb_guess_encoding no longer crashes when passed huge
60+
list of candidate encodings (with 200,000+ entries). (Jordi Kroon)
5961

6062
- Opcache:
6163
. Fixed bug GH-20051 (apache2 shutdowns when restart is requested during

ext/mbstring/mbstring.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3414,15 +3414,17 @@ MBSTRING_API const mbfl_encoding* mb_guess_encoding_for_strings(const unsigned c
34143414
return *elist;
34153415
}
34163416

3417-
/* Allocate on stack; when we return, this array is automatically freed */
3418-
struct candidate *array = alloca(elist_size * sizeof(struct candidate));
3417+
/* Allocate on stack or heap */
3418+
ALLOCA_FLAG(use_heap)
3419+
struct candidate *array = do_alloca(elist_size * sizeof(struct candidate), use_heap);
34193420
elist_size = init_candidate_array(array, elist_size, elist, strings, str_lengths, n, strict, order_significant);
34203421

34213422
while (n--) {
34223423
start_string(array, elist_size, strings[n], str_lengths[n]);
34233424
elist_size = count_demerits(array, elist_size, strict);
34243425
if (elist_size == 0) {
34253426
/* All candidates were eliminated */
3427+
free_alloca(array, use_heap);
34263428
return NULL;
34273429
}
34283430
}
@@ -3434,7 +3436,10 @@ MBSTRING_API const mbfl_encoding* mb_guess_encoding_for_strings(const unsigned c
34343436
best = i;
34353437
}
34363438
}
3437-
return array[best].enc;
3439+
3440+
const mbfl_encoding *result = array[best].enc;
3441+
free_alloca(array, use_heap);
3442+
return result;
34383443
}
34393444

34403445
/* When doing 'strict' detection, any string which is invalid in the candidate encoding

ext/mbstring/tests/gh21223.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-21223 (Stack overflow in mb_guess_encoding called via mb_detect_encoding)
3+
--EXTENSIONS--
4+
mbstring
5+
--FILE--
6+
<?php
7+
$str = "hello";
8+
9+
$list = [];
10+
for ($i = 0; $i < 500000; $i++) {
11+
$list[] = "UTF-8";
12+
}
13+
14+
var_dump(mb_detect_encoding($str, $list, false));
15+
echo "Done";
16+
?>
17+
--EXPECT--
18+
string(5) "UTF-8"
19+
Done

0 commit comments

Comments
 (0)