diff --git a/NEWS b/NEWS index 97224d6a187..5a03492c242 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,10 @@ PHP NEWS . Fixed bug GH-20767 (build failure with musttail/preserve_none feature on macOs). (David Carlier) +- MbString: + . Fixed bug GH-20833 (mb_str_pad() divide by zero if padding string is + invalid in the encoding). (ndossche) + - Readline: . Fixed bug GH-18139 (Memory leak when overriding some settings via readline_info()). (ndossche) diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c index 9f946508464..8351bdb9d79 100644 --- a/ext/mbstring/mbstring.c +++ b/ext/mbstring/mbstring.c @@ -5853,6 +5853,11 @@ PHP_FUNCTION(mb_str_pad) } size_t pad_length = mb_get_strlen(pad, encoding); + if (pad_length == 0) { + /* Possible with invalidly encoded padding string. */ + zend_argument_must_not_be_empty_error(3); + RETURN_THROWS(); + } size_t num_mb_pad_chars = pad_to_length - input_length; diff --git a/ext/mbstring/tests/gh20833.phpt b/ext/mbstring/tests/gh20833.phpt new file mode 100644 index 00000000000..099aa337923 --- /dev/null +++ b/ext/mbstring/tests/gh20833.phpt @@ -0,0 +1,16 @@ +--TEST-- +GH-20833 (mb_str_pad() divide by zero if padding string is invalid in the encoding) +--EXTENSIONS-- +mbstring +--FILE-- +getMessage(), "\n"; +} +?> +--EXPECT-- +ValueError: mb_str_pad(): Argument #3 ($pad_string) must not be empty