Skip to content

Commit 03113b0

Browse files
committed
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. Closes GH-20834.
1 parent 1c9f117 commit 03113b0

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
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.4.18
44

5+
- MbString:
6+
. Fixed bug GH-20833 (mb_str_pad() divide by zero if padding string is
7+
invalid in the encoding). (ndossche)
8+
59
- Readline:
610
. Fixed bug GH-18139 (Memory leak when overriding some settings
711
via readline_info()). (ndossche)

ext/mbstring/mbstring.c

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

58505850
size_t pad_length = mb_get_strlen(pad, encoding);
5851+
if (pad_length == 0) {
5852+
/* Possible with invalidly encoded padding string. */
5853+
zend_argument_must_not_be_empty_error(3);
5854+
RETURN_THROWS();
5855+
}
58515856

58525857
size_t num_mb_pad_chars = pad_to_length - input_length;
58535858

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)