Skip to content

Commit e4098da

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fix GH-20833: mb_str_pad() divide by zero if padding string is invalid in the encoding
2 parents 988d29b + 171b52c commit e4098da

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

ext/mbstring/mbstring.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5856,6 +5856,11 @@ PHP_FUNCTION(mb_str_pad)
58565856
}
58575857

58585858
size_t pad_length = mb_get_strlen(pad, encoding);
5859+
if (pad_length == 0) {
5860+
/* Possible with invalidly encoded padding string. */
5861+
zend_argument_must_not_be_empty_error(3);
5862+
RETURN_THROWS();
5863+
}
58595864

58605865
size_t num_mb_pad_chars = pad_to_length - input_length;
58615866

ext/mbstring/tests/gh20833.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
GH-20833 (mb_str_pad() divide by zero if padding string is invalid in the encoding)
3+
--EXTENSIONS--
4+
mbstring
5+
--FILE--
6+
<?php
7+
$utf8 = "test";
8+
$utf32 = mb_convert_encoding($utf8, 'UTF-32', 'UTF-8');
9+
try {
10+
mb_str_pad($utf32, 5, "1" /* invalid for encoding */, STR_PAD_RIGHT, "UTF-32");
11+
} catch (ValueError $e) {
12+
echo $e::class, ": ", $e->getMessage(), "\n";
13+
}
14+
?>
15+
--EXPECT--
16+
ValueError: mb_str_pad(): Argument #3 ($pad_string) must not be empty

0 commit comments

Comments
 (0)