Skip to content

Remove 'bogus' error condition in str_pad() #4613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -5697,11 +5697,6 @@ PHP_FUNCTION(str_pad)
}

num_pad_chars = pad_length - ZSTR_LEN(input);
if (num_pad_chars >= INT_MAX) {
php_error_docref(NULL, E_WARNING, "Padding length is too long");
return;
}

result = zend_string_safe_alloc(1, ZSTR_LEN(input), num_pad_chars, 0);
ZSTR_LEN(result) = 0;

Expand Down
36 changes: 36 additions & 0 deletions ext/standard/tests/strings/str_pad_variation1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Test str_pad() function : usage variations - large values for '$pad_length' argument
--FILE--
<?php
/* Prototype : string str_pad ( string $input , int $pad_length [, string $pad_string [, int $pad_type ]] )
* Description: Pad a string to a certain length with another string
* Source code: ext/standard/string.c
*/

/* Test str_pad() function: with unexpected inputs for '$pad_length'
* and expected type for '$input'
*/

echo "*** Testing str_pad() function: with large value for for 'pad_length' argument ***\n";

//defining '$input' argument
$input = "Test string";

$extra_large_pad_length = PHP_INT_MAX*5;
try {
var_dump( str_pad($input, $extra_large_pad_length) );
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}

$php_int_max_pad_length = PHP_INT_MAX;
var_dump( str_pad($input, $php_int_max_pad_length) );


?>
--EXPECTF--
*** Testing str_pad() function: with large value for for 'pad_length' argument ***
str_pad() expects parameter 2 to be int, float given

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

2 changes: 1 addition & 1 deletion ext/standard/tests/strings/str_pad_variation5.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ var_dump( str_pad($input, $pad_length) );
--EXPECTF--
*** Testing str_pad() function: with large value for for 'pad_length' argument ***

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