Skip to content

Promote warnings to errors in substr_compare() #4596

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
7 changes: 4 additions & 3 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -6133,7 +6133,7 @@ PHP_FUNCTION(strpbrk)
/* {{{ proto int|false substr_compare(string main_str, string str, int offset [, int length [, bool case_sensitivity]])
Binary safe optionally case insensitive comparison of 2 strings from an offset, up to length characters */
PHP_FUNCTION(substr_compare)
{
{
zend_string *s1, *s2;
zend_long offset, len=0;
zend_bool len_is_default=1;
Expand All @@ -6153,8 +6153,8 @@ PHP_FUNCTION(substr_compare)
if (len == 0) {
RETURN_LONG(0L);
} else {
php_error_docref(NULL, E_WARNING, "The length must be greater than or equal to zero");
RETURN_FALSE;
zend_throw_error(NULL, "The length must be greater than or equal to zero");
return;
}
}

Expand All @@ -6164,6 +6164,7 @@ PHP_FUNCTION(substr_compare)
}

if ((size_t)offset > ZSTR_LEN(s1)) {
/* TODO Check if Candidate to convert to Exception */
php_error_docref(NULL, E_WARNING, "The start position cannot exceed initial string length");
RETURN_FALSE;
}
Expand Down
10 changes: 6 additions & 4 deletions ext/standard/tests/strings/bug33605.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
Bug #33605 (substr_compare crashes)
--FILE--
<?php
$res = substr_compare("aa", "a", -99999999, -1, 0);
var_dump($res);
try {
substr_compare("aa", "a", -99999999, -1, 0);
} catch (\Error $e) {
echo $e->getMessage();
}

?>
--EXPECTF--
Warning: substr_compare(): The length must be greater than or equal to zero in %s on line %d
bool(false)
The length must be greater than or equal to zero
11 changes: 7 additions & 4 deletions ext/standard/tests/strings/substr_compare.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ var_dump(substr_compare("abcde", "abc", 5, 1));
var_dump(substr_compare("abcde", "abcdef", -10, 10) < 0);
var_dump(substr_compare("abcde", "abc", 0, 0));
echo "Test\n";
var_dump(substr_compare("abcde", "abc", 0, -1));

try {
substr_compare("abcde", "abc", 0, -1);
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}
var_dump(substr_compare("abcde", "abc", -1, NULL, -5) > 0);

echo "Done\n";
Expand All @@ -29,8 +34,6 @@ int(-1)
bool(true)
int(0)
Test

Warning: substr_compare(): The length must be greater than or equal to zero in %s on line %d
bool(false)
The length must be greater than or equal to zero
bool(true)
Done