Skip to content

Promote warnings to errors in str_pad() #4601

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -5687,18 +5687,18 @@ PHP_FUNCTION(str_pad)
}

if (pad_str_len == 0) {
php_error_docref(NULL, E_WARNING, "Padding string cannot be empty");
zend_throw_error(NULL, "Padding string cannot be empty");
return;
}

if (pad_type_val < STR_PAD_LEFT || pad_type_val > STR_PAD_BOTH) {
php_error_docref(NULL, E_WARNING, "Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH");
zend_throw_error(NULL, "Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH");
return;
}

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");
zend_throw_error(NULL, "Padding length is too long");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error condition looks bogus: I see no reason why the number of padding characters should be limited by INT_MAX. Could you submit a separate PR to drop this entirely?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, maybe something due to before PHP could handle large strings ? (I remember reading something about 2GB string size limit)

return;
}

Expand Down
Binary file modified ext/standard/tests/strings/str_pad.phpt
Binary file not shown.
70 changes: 70 additions & 0 deletions ext/standard/tests/strings/str_pad_variation1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
--TEST--
str_pad() function: usage variations - Non printable chars
--INI--
precision=14
--FILE--
<?php
// Split from str_pad for NUL Bytes
// 7-bit ASCII
$string = chr(0).chr(255).chr(128).chr(234).chr(143);

/* different pad_lengths */
$pad_lengths = [
-PHP_INT_MAX, // huge negative value
-1, // negative value
0, // pad_length < sizeof(input_string)
9, // pad_length <= sizeof(input_string)
10, // pad_length > sizeof(input_string)
16, // pad_length > sizeof(input_string)
];

$pad_string = "=";

/*loop through to use each variant of $pad_length on
each element of $input_strings array */
foreach ($pad_lengths as $pad_length ) {
// default pad_string & pad_type
var_dump( bin2hex( str_pad($string, $pad_length) ) );
// default pad_type
var_dump( bin2hex( str_pad($string, $pad_length, $pad_string) ) );
var_dump( bin2hex( str_pad($string, $pad_length, $pad_string, STR_PAD_LEFT) ) );
var_dump( bin2hex( str_pad($string, $pad_length, $pad_string, STR_PAD_RIGHT) ) );
var_dump( bin2hex( str_pad($string, $pad_length, $pad_string, STR_PAD_BOTH) ) );
}

?>

DONE
--EXPECT--
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(10) "00ff80ea8f"
string(18) "00ff80ea8f20202020"
string(18) "00ff80ea8f3d3d3d3d"
string(18) "3d3d3d3d00ff80ea8f"
string(18) "00ff80ea8f3d3d3d3d"
string(18) "3d3d00ff80ea8f3d3d"
string(20) "00ff80ea8f2020202020"
string(20) "00ff80ea8f3d3d3d3d3d"
string(20) "3d3d3d3d3d00ff80ea8f"
string(20) "00ff80ea8f3d3d3d3d3d"
string(20) "3d3d00ff80ea8f3d3d3d"
string(32) "00ff80ea8f2020202020202020202020"
string(32) "00ff80ea8f3d3d3d3d3d3d3d3d3d3d3d"
string(32) "3d3d3d3d3d3d3d3d3d3d3d00ff80ea8f"
string(32) "00ff80ea8f3d3d3d3d3d3d3d3d3d3d3d"
string(32) "3d3d3d3d3d00ff80ea8f3d3d3d3d3d3d"

DONE