Skip to content

Commit 6938799

Browse files
committed
Added explicit checks for when needle is longer than haystack
1 parent 0dbe537 commit 6938799

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

ext/standard/string.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,6 +2312,10 @@ PHP_FUNCTION(str_begins) {
23122312
Z_PARAM_STR(needle)
23132313
ZEND_PARSE_PARAMETERS_END();
23142314

2315+
if (needle->len > haystack->len) {
2316+
RETURN_BOOL(0);
2317+
}
2318+
23152319
for (i = 0; i < needle->len; i++)
23162320
if (haystack->val[i] != needle->val[i])
23172321
RETURN_BOOL(0);
@@ -2329,6 +2333,10 @@ PHP_FUNCTION(str_ibegins) {
23292333
Z_PARAM_STR(needle)
23302334
ZEND_PARSE_PARAMETERS_END();
23312335

2336+
if (needle->len > haystack->len) {
2337+
RETURN_BOOL(0);
2338+
}
2339+
23322340
for (i = 0; i < needle->len; i++)
23332341
if (tolower(haystack->val[i]) != tolower(needle->val[i]))
23342342
RETURN_BOOL(0);
@@ -2345,6 +2353,10 @@ PHP_FUNCTION(str_ends) {
23452353
Z_PARAM_STR(haystack)
23462354
Z_PARAM_STR(needle)
23472355
ZEND_PARSE_PARAMETERS_END();
2356+
2357+
if (needle->len > haystack->len) {
2358+
RETURN_BOOL(0);
2359+
}
23482360

23492361
for (i = haystack->len - 1, j = needle->len - 1; j >= 0; i--, j--)
23502362
if (haystack->val[i] != needle->val[j])
@@ -2363,6 +2375,10 @@ PHP_FUNCTION(str_iends) {
23632375
Z_PARAM_STR(needle)
23642376
ZEND_PARSE_PARAMETERS_END();
23652377

2378+
if (needle->len > haystack->len) {
2379+
RETURN_BOOL(0);
2380+
}
2381+
23662382
for (i = haystack->len - 1, j = needle->len - 1; j >= 0; i--, j--)
23672383
if (tolower(haystack->val[i]) != tolower(needle->val[j]))
23682384
RETURN_BOOL(0);

ext/standard/tests/strings/str_ends.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ $testStr = "beginningMiddleEnd";
99
var_dump(str_ends($testStr, "End"));
1010
var_dump(str_ends($testStr, "end"));
1111
var_dump(str_ends($testStr, "en"));
12+
var_dump(str_ends($testStr, $testStr."a"));
1213
?>
1314
--EXPECT--
1415
bool(true)
1516
bool(false)
1617
bool(false)
18+
bool(false)

ext/standard/tests/strings/str_iends.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ $testStr = "beginningMiddleEnd";
99
var_dump(str_iends($testStr, "End"));
1010
var_dump(str_iends($testStr, "end"));
1111
var_dump(str_iends($testStr, "en"));
12+
var_dump(str_iends($testStr, $testStr."a"));
1213
?>
1314
--EXPECT--
1415
bool(true)
1516
bool(true)
1617
bool(false)
18+
bool(false)

0 commit comments

Comments
 (0)