Skip to content

Commit 443927e

Browse files
alexdowadiluuu1994nielsdos
committed
Fix GH-11476: crash with count_demerits negative-size-param
Co-authored-by: Ilija Tovilo <[email protected]> Co-authored-by: Niels Dossche <[email protected]>
1 parent 4918765 commit 443927e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

ext/mbstring/mbstring.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -3083,8 +3083,9 @@ static size_t count_demerits(struct candidate *array, size_t length, bool strict
30833083
if (strict) {
30843084
/* This candidate encoding is not valid, eliminate it from consideration */
30853085
length--;
3086-
if (length == 0) {
3087-
return 0;
3086+
if (i == length) {
3087+
/* The eliminated candidate was the last valid one in the list */
3088+
goto next_iteration;
30883089
}
30893090
memmove(&array[i], &array[i+1], (length - i) * sizeof(struct candidate));
30903091
goto try_next_encoding;
@@ -3100,6 +3101,7 @@ static size_t count_demerits(struct candidate *array, size_t length, bool strict
31003101
}
31013102
}
31023103
}
3104+
next_iteration: ;
31033105
}
31043106

31053107
for (size_t i = 0; i < length; i++) {

ext/mbstring/tests/gh11476.phpt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
GH-11476: count_demerits negative-size-param
3+
--EXTENSIONS--
4+
mbstring
5+
--FILE--
6+
<?php
7+
$str = str_repeat('a', 250) . chr(246) . str_repeat('a', 9) . chr(246) . str_repeat('a', 6);
8+
$detectedEncoding = mb_detect_encoding($str, mb_list_encodings(), true);
9+
var_dump($detectedEncoding);
10+
?>
11+
--EXPECT--
12+
string(12) "Windows-1252"

0 commit comments

Comments
 (0)