Skip to content

Commit d7ebdce

Browse files
committed
Normalize strpos() behavior
1 parent aba0ee7 commit d7ebdce

File tree

7 files changed

+16
-35
lines changed

7 files changed

+16
-35
lines changed

ext/iconv/tests/iconv_substr.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var_dump(iconv("ISO-2022-JP", "EUC-JP", iconv_substr(iconv("EUC-JP", "ISO-2022-J
4545
666768696a6b6c
4646
a6a4a8a4aaa4ab
4747
a4aba4ada4afa4b1a4b3a4b5a4b7
48-
bool(false)
48+
string(0) ""
4949
bool(false)
5050
string(14) "This is a test"
5151
string(14) "This is a test"
@@ -55,8 +55,8 @@ string(3) "est"
5555
string(3) "est"
5656
string(5) "This "
5757
string(5) "This "
58+
string(0) ""
5859
bool(false)
59-
bool(false)
60-
bool(false)
60+
string(0) ""
6161
bool(false)
6262
string(10) "ちは ISO-2"

ext/intl/tests/bug62759.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var_dump(intl_get_error_message());
1414
var_dump(grapheme_substr('déjà', -1, 0));
1515
?>
1616
--EXPECT--
17-
bool(false)
17+
string(0) ""
1818
string(0) ""
1919
bool(false)
2020
string(61) "grapheme_substr: invalid parameters: U_ILLEGAL_ARGUMENT_ERROR"

ext/standard/basic_functions.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ function str_ends_with(string $haystack, string $needle): bool {}
607607

608608
function chunk_split(string $str, int $chunklen = 76, string $ending = "\r\n"): string {}
609609

610-
function substr(string $str, int $start, ?int $length = null): string|false {}
610+
function substr(string $str, int $start, ?int $length = null): string {}
611611

612612
function substr_replace(array|string $str, array|string $replace, array|int $start, array|int|null $length = null): string|array {}
613613

ext/standard/basic_functions_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 56fb3ef4c53a1ed7abf6a115567bca47e1a14cda */
2+
* Stub hash: 2c44d43410b90a94af4057ca99274f9262744298 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0)
55
ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0)
@@ -924,7 +924,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_chunk_split, 0, 1, IS_STRING, 0)
924924
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, ending, IS_STRING, 0, "\"\\r\\n\"")
925925
ZEND_END_ARG_INFO()
926926

927-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_substr, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
927+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_substr, 0, 2, IS_STRING, 0)
928928
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
929929
ZEND_ARG_TYPE_INFO(0, start, IS_LONG, 0)
930930
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, length, IS_LONG, 1, "null")

ext/standard/string.c

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,9 +2170,7 @@ PHP_FUNCTION(substr)
21702170
Z_PARAM_LONG_OR_NULL(l, len_is_null)
21712171
ZEND_PARSE_PARAMETERS_END();
21722172

2173-
if (f > (zend_long)ZSTR_LEN(str)) {
2174-
RETURN_FALSE;
2175-
} else if (f < 0) {
2173+
if (f < 0) {
21762174
/* if "from" position is negative, count start position from the end
21772175
* of the string
21782176
*/
@@ -2181,41 +2179,24 @@ PHP_FUNCTION(substr)
21812179
} else {
21822180
f = (zend_long)ZSTR_LEN(str) + f;
21832181
}
2184-
if (!len_is_null) {
2185-
if (l < 0) {
2186-
/* if "length" position is negative, set it to the length
2187-
* needed to stop that many chars from the end of the string
2188-
*/
2189-
if ((size_t)(-l) > ZSTR_LEN(str) - (size_t)f) {
2190-
if ((size_t)(-l) > ZSTR_LEN(str)) {
2191-
RETURN_FALSE;
2192-
} else {
2193-
l = 0;
2194-
}
2195-
} else {
2196-
l = (zend_long)ZSTR_LEN(str) - f + l;
2197-
}
2198-
} else if ((size_t)l > ZSTR_LEN(str) - (size_t)f) {
2199-
goto truncate_len;
2200-
}
2201-
} else {
2202-
goto truncate_len;
2203-
}
2204-
} else if (!len_is_null) {
2182+
} else if ((size_t)f > ZSTR_LEN(str)) {
2183+
RETURN_EMPTY_STRING();
2184+
}
2185+
2186+
if (!len_is_null) {
22052187
if (l < 0) {
22062188
/* if "length" position is negative, set it to the length
22072189
* needed to stop that many chars from the end of the string
22082190
*/
22092191
if ((size_t)(-l) > ZSTR_LEN(str) - (size_t)f) {
2210-
RETURN_FALSE;
2192+
l = 0;
22112193
} else {
22122194
l = (zend_long)ZSTR_LEN(str) - f + l;
22132195
}
22142196
} else if ((size_t)l > ZSTR_LEN(str) - (size_t)f) {
2215-
goto truncate_len;
2197+
l = (zend_long)ZSTR_LEN(str) - f;
22162198
}
22172199
} else {
2218-
truncate_len:
22192200
l = (zend_long)ZSTR_LEN(str) - f;
22202201
}
22212202

ext/standard/tests/strings/bug40754.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,4 @@ strripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
102102
strripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
103103
int(2)
104104
string(8) "abcdeabc"
105-
bool(false)
105+
string(0) ""
13 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)