Skip to content

Commit 9d18f23

Browse files
committed
Remove 'bogus' error condition in str_pad()
1 parent 675e975 commit 9d18f23

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

ext/standard/string.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5697,11 +5697,6 @@ PHP_FUNCTION(str_pad)
56975697
}
56985698

56995699
num_pad_chars = pad_length - ZSTR_LEN(input);
5700-
if (num_pad_chars >= INT_MAX) {
5701-
php_error_docref(NULL, E_WARNING, "Padding length is too long");
5702-
return;
5703-
}
5704-
57055700
result = zend_string_safe_alloc(1, ZSTR_LEN(input), num_pad_chars, 0);
57065701
ZSTR_LEN(result) = 0;
57075702

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Test str_pad() function : usage variations - large values for '$pad_length' argument
3+
--FILE--
4+
<?php
5+
/* Prototype : string str_pad ( string $input , int $pad_length [, string $pad_string [, int $pad_type ]] )
6+
* Description: Pad a string to a certain length with another string
7+
* Source code: ext/standard/string.c
8+
*/
9+
10+
/* Test str_pad() function: with unexpected inputs for '$pad_length'
11+
* and expected type for '$input'
12+
*/
13+
14+
echo "*** Testing str_pad() function: with large value for for 'pad_length' argument ***\n";
15+
16+
//defining '$input' argument
17+
$input = "Test string";
18+
19+
$extra_large_pad_length = PHP_INT_MAX*5;
20+
try {
21+
var_dump( str_pad($input, $extra_large_pad_length) );
22+
} catch (\TypeError $e) {
23+
echo $e->getMessage() . "\n";
24+
}
25+
26+
$php_int_max_pad_length = PHP_INT_MAX;
27+
var_dump( str_pad($input, $php_int_max_pad_length) );
28+
29+
30+
?>
31+
--EXPECTF--
32+
*** Testing str_pad() function: with large value for for 'pad_length' argument ***
33+
str_pad() expects parameter 2 to be int, float given
34+
35+
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
36+

ext/standard/tests/strings/str_pad_variation5.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ var_dump( str_pad($input, $pad_length) );
3131
--EXPECTF--
3232
*** Testing str_pad() function: with large value for for 'pad_length' argument ***
3333

34-
Fatal error: Allowed memory size of 134217728 bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
34+
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d

0 commit comments

Comments
 (0)