Skip to content

Commit 171b52c

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix GH-20833: mb_str_pad() divide by zero if padding string is invalid in the encoding
2 parents 9b089ed + 03113b0 commit 171b52c

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ PHP NEWS
88
. Fixed bug GH-20767 (build failure with musttail/preserve_none feature
99
on macOs). (David Carlier)
1010

11+
- MbString:
12+
. Fixed bug GH-20833 (mb_str_pad() divide by zero if padding string is
13+
invalid in the encoding). (ndossche)
14+
1115
- Readline:
1216
. Fixed bug GH-18139 (Memory leak when overriding some settings
1317
via readline_info()). (ndossche)

ext/mbstring/mbstring.c

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

58555855
size_t pad_length = mb_get_strlen(pad, encoding);
5856+
if (pad_length == 0) {
5857+
/* Possible with invalidly encoded padding string. */
5858+
zend_argument_must_not_be_empty_error(3);
5859+
RETURN_THROWS();
5860+
}
58565861

58575862
size_t num_mb_pad_chars = pad_to_length - input_length;
58585863

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)