Skip to content

Fix GH-8924 str_split of empty string must return empty array #8945

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: 5 additions & 1 deletion ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -5882,7 +5882,11 @@ PHP_FUNCTION(str_split)
RETURN_THROWS();
}

if (0 == ZSTR_LEN(str) || (size_t)split_length >= ZSTR_LEN(str)) {
if ((size_t)split_length >= ZSTR_LEN(str)) {
if (0 == ZSTR_LEN(str)) {
RETURN_EMPTY_ARRAY();
}

array_init_size(return_value, 1);
add_next_index_stringl(return_value, ZSTR_VAL(str), ZSTR_LEN(str));
return;
Expand Down
14 changes: 12 additions & 2 deletions ext/standard/tests/strings/str_split_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ var_dump( str_split($str,$split_length) );
echo "-- With split_length as default argument --\n";
var_dump( str_split($str) );

echo "Done"
echo "-- Empty string must always return empty array --\n";
var_dump( str_split('') );
var_dump( str_split('', 1) );
var_dump( str_split('', 100) );

?>
--EXPECT--
*** Testing str_split() : basic functionality ***
Expand Down Expand Up @@ -80,4 +84,10 @@ array(22) {
[21]=>
string(1) "e"
}
Done
-- Empty string must always return empty array --
array(0) {
}
array(0) {
}
array(0) {
}
4 changes: 1 addition & 3 deletions ext/standard/tests/strings/str_split_variation3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ echo "Done"
--EXPECTF--
*** Testing str_split() : double quoted strings for 'str' ***
-- Iteration 1 --
array(1) {
[0]=>
string(0) ""
array(0) {
}
-- Iteration 2 --
array(1) {
Expand Down
4 changes: 1 addition & 3 deletions ext/standard/tests/strings/str_split_variation4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ echo "Done"
--EXPECT--
*** Testing str_split() : single quoted strings for 'str' ***
-- Iteration 1 --
array(1) {
[0]=>
string(0) ""
array(0) {
}
-- Iteration 2 --
array(1) {
Expand Down
8 changes: 2 additions & 6 deletions ext/standard/tests/strings/str_split_variation5.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,10 @@ echo "Done"
--EXPECT--
*** Testing str_split() : heredoc strings as 'str' argument ***
-- Iteration 1 --
array(1) {
[0]=>
string(0) ""
array(0) {
}
-- Iteration 2 --
array(1) {
[0]=>
string(0) ""
array(0) {
}
-- Iteration 3 --
array(1) {
Expand Down