diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c index 758c00b4261..e052d8e2078 100644 --- a/ext/mbstring/mbstring.c +++ b/ext/mbstring/mbstring.c @@ -3083,8 +3083,9 @@ try_next_encoding: if (strict) { /* This candidate encoding is not valid, eliminate it from consideration */ length--; - if (length == 0) { - return 0; + if (i == length) { + /* The eliminated candidate was the last valid one in the list */ + goto next_iteration; } memmove(&array[i], &array[i+1], (length - i) * sizeof(struct candidate)); goto try_next_encoding; @@ -3100,6 +3101,7 @@ try_next_encoding: } } } +next_iteration: ; } for (size_t i = 0; i < length; i++) { diff --git a/ext/mbstring/tests/gh11476.phpt b/ext/mbstring/tests/gh11476.phpt new file mode 100644 index 00000000000..eb1b66aa6b1 --- /dev/null +++ b/ext/mbstring/tests/gh11476.phpt @@ -0,0 +1,12 @@ +--TEST-- +GH-11476: count_demerits negative-size-param +--EXTENSIONS-- +mbstring +--FILE-- + +--EXPECT-- +string(12) "Windows-1252"