Skip to content

Promote warnings to errors in substr_count() #4605

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
5 changes: 3 additions & 2 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -5610,8 +5610,8 @@ PHP_FUNCTION(substr_count)
ZEND_PARSE_PARAMETERS_END();

if (needle_len == 0) {
php_error_docref(NULL, E_WARNING, "Empty substring");
RETURN_FALSE;
zend_throw_error(NULL, "Empty substring");
return;
}

p = haystack;
Expand All @@ -5632,6 +5632,7 @@ PHP_FUNCTION(substr_count)
length += (haystack_len - offset);
}
if (length < 0 || ((size_t)length > (haystack_len - offset))) {
/* TODO Check if Candidate to convert to Exception */
php_error_docref(NULL, E_WARNING, "Invalid length value");
Copy link
Member

Choose a reason for hiding this comment

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

why is this warning not converted? invalid length is at least as serious as empty substring IMO.

RETURN_FALSE;
}
Expand Down
26 changes: 17 additions & 9 deletions ext/standard/tests/strings/substr_count_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ Test substr_count() function (basic)
<?php

echo "***Testing basic operations ***\n";
var_dump(@substr_count("", ""));
var_dump(@substr_count("a", ""));
var_dump(@substr_count("", "a"));
var_dump(@substr_count("", "a"));
var_dump(@substr_count("", chr(0)));
try {
substr_count("", "");
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}
try {
substr_count("a", "");
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}
var_dump(substr_count("", "a"));
var_dump(substr_count("", "a"));
var_dump(substr_count("", chr(0)));

$a = str_repeat("abcacba", 100);
var_dump(@substr_count($a, "bca"));
var_dump(substr_count($a, "bca"));

$a = str_repeat("abcacbabca", 100);
var_dump(@substr_count($a, "bca"));
var_dump(substr_count($a, "bca"));
var_dump(substr_count($a, "bca", 200));
var_dump(substr_count($a, "bca", 200, 50));
var_dump(substr_count($a, "bca", -200));
Expand All @@ -26,8 +34,8 @@ echo "Done\n";
?>
--EXPECT--
***Testing basic operations ***
bool(false)
bool(false)
Empty substring
Empty substring
int(0)
int(0)
int(0)
Expand Down