Skip to content

Commit e106d68

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
2 parents 914969d + 37c5a13 commit e106d68

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ PHP NEWS
2828
. Fixed bug GH-21097 (Accessing Dom\Node properties can can throw TypeError).
2929
(ndossche)
3030

31+
- MBString:
32+
. Fixed bug GH-21223; mb_guess_encoding no longer crashes when passed huge
33+
list of candidate encodings (with 200,000+ entries). (Jordi Kroon)
34+
3135
- Opcache:
3236
. Fixed bug GH-20718 ("Insufficient shared memory" when using JIT on Solaris).
3337
(Petr Sumbera)

ext/mbstring/mbstring.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3382,15 +3382,17 @@ MBSTRING_API const mbfl_encoding* mb_guess_encoding_for_strings(const unsigned c
33823382
return *elist;
33833383
}
33843384

3385-
/* Allocate on stack; when we return, this array is automatically freed */
3386-
struct candidate *array = alloca(elist_size * sizeof(struct candidate));
3385+
/* Allocate on stack or heap */
3386+
ALLOCA_FLAG(use_heap)
3387+
struct candidate *array = do_alloca(elist_size * sizeof(struct candidate), use_heap);
33873388
elist_size = init_candidate_array(array, elist_size, elist, strings, str_lengths, n, strict, order_significant);
33883389

33893390
while (n--) {
33903391
start_string(array, elist_size, strings[n], str_lengths[n]);
33913392
elist_size = count_demerits(array, elist_size, strict);
33923393
if (elist_size == 0) {
33933394
/* All candidates were eliminated */
3395+
free_alloca(array, use_heap);
33943396
return NULL;
33953397
}
33963398
}
@@ -3402,7 +3404,10 @@ MBSTRING_API const mbfl_encoding* mb_guess_encoding_for_strings(const unsigned c
34023404
best = i;
34033405
}
34043406
}
3405-
return array[best].enc;
3407+
3408+
const mbfl_encoding *result = array[best].enc;
3409+
free_alloca(array, use_heap);
3410+
return result;
34063411
}
34073412

34083413
/* 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)