From 3c2567d1573776f618c343ca5fd2a2e5310fddad Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+ndossche@users.noreply.github.com> Date: Sun, 4 Jan 2026 16:11:58 +0100 Subject: [PATCH 1/2] Fix GH-20833: mb_str_pad() divide by zero if padding string is invalid in the encoding If the padding string is not valid in the given encoding, mb_get_strlen() can return 0. --- ext/mbstring/mbstring.c | 5 +++++ ext/mbstring/tests/gh20833.phpt | 14 ++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 ext/mbstring/tests/gh20833.phpt diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c index 3c3200636ec2c..1491e1728cd58 100644 --- a/ext/mbstring/mbstring.c +++ b/ext/mbstring/mbstring.c @@ -5848,6 +5848,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 0000000000000..9732905faf2c6 --- /dev/null +++ b/ext/mbstring/tests/gh20833.phpt @@ -0,0 +1,14 @@ +--TEST-- +GH-20833 (mb_str_pad() divide by zero if padding string is invalid in the encoding) +--FILE-- +getMessage(), "\n"; +} +?> +--EXPECT-- +ValueError: mb_str_pad(): Argument #3 ($pad_string) must not be empty From 2f1070e362e6eccf28ac92e9eba67f0ab15c1acb Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+ndossche@users.noreply.github.com> Date: Sun, 4 Jan 2026 20:52:10 +0100 Subject: [PATCH 2/2] Add missing EXTENSIONS section to phpt test file --- ext/mbstring/tests/gh20833.phpt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ext/mbstring/tests/gh20833.phpt b/ext/mbstring/tests/gh20833.phpt index 9732905faf2c6..099aa3379238f 100644 --- a/ext/mbstring/tests/gh20833.phpt +++ b/ext/mbstring/tests/gh20833.phpt @@ -1,5 +1,7 @@ --TEST-- GH-20833 (mb_str_pad() divide by zero if padding string is invalid in the encoding) +--EXTENSIONS-- +mbstring --FILE--