Skip to content

Commit f3d5a5a

Browse files
committed
Promote warnings to exceptions in string search related functions
GH-5004
1 parent 17598d5 commit f3d5a5a

15 files changed

+256
-235
lines changed

ext/iconv/tests/iconv_strpos.phpt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ iconv_strpos()
66
iconv.internal_charset=ISO-8859-1
77
--FILE--
88
<?php
9-
function my_error_handler($errno, $errmsg, $filename, $linenum)
10-
{
11-
echo "$errno: $errmsg\n";
12-
}
13-
set_error_handler('my_error_handler');
149
function foo($haystk, $needle, $offset, $to_charset = false, $from_charset = false)
1510
{
1611
if ($from_charset !== false) {
1712
$haystk = iconv($from_charset, $to_charset, $haystk);
1813
}
19-
var_dump(strpos($haystk, $needle, $offset));
14+
try {
15+
var_dump(strpos($haystk, $needle, $offset));
16+
} catch (ValueError $exception) {
17+
echo $exception->getMessage() . "\n";
18+
}
2019
if ($to_charset !== false) {
2120
var_dump(iconv_strpos($haystk, $needle, $offset, $to_charset));
2221
} else {
@@ -42,8 +41,7 @@ bool(false)
4241
bool(false)
4342
int(5)
4443
int(5)
45-
2: %s
46-
bool(false)
44+
Offset not contained in string
4745
bool(false)
4846
int(7)
4947
int(7)

ext/mbstring/tests/bug43841.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ foreach ($offsets as $i) {
3030
echo "mb_strrpos:\n";
3131
var_dump(mb_strrpos('This is na English ta', 'a', $i));
3232
echo "strrpos:\n";
33-
var_dump(strrpos('This is na English ta', 'a', $i));
33+
try {
34+
var_dump(strrpos('This is na English ta', 'a', $i));
35+
} catch (ValueError $exception) {
36+
echo $exception->getMessage() . "\n";
37+
}
3438
}
3539
?>
3640
--EXPECTF--
@@ -45,9 +49,7 @@ mb_strrpos:
4549
Warning: mb_strrpos(): Offset is greater than the length of haystack string in %s on line %d
4650
bool(false)
4751
strrpos:
48-
49-
Warning: strrpos(): Offset not contained in string in %s on line %d
50-
bool(false)
52+
Offset not contained in string
5153

5254
-- Offset is -24 --
5355
Multibyte String:
@@ -60,9 +62,7 @@ mb_strrpos:
6062
Warning: mb_strrpos(): Offset is greater than the length of haystack string in %s on line %d
6163
bool(false)
6264
strrpos:
63-
64-
Warning: strrpos(): Offset not contained in string in %s on line %d
65-
bool(false)
65+
Offset not contained in string
6666

6767
-- Offset is -13 --
6868
Multibyte String:

ext/mbstring/tests/bug45923.phpt

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ function section($func, $haystack, $needle)
1010
echo "\n------- $func -----------\n\n";
1111
foreach(array(0, 3, 6, 9, 11, 12, -1, -3, -6, -20) as $offset) {
1212
echo "> Offset: $offset\n";
13-
var_dump($func($haystack,$needle,$offset));
13+
try {
14+
var_dump($func($haystack,$needle,$offset));
15+
} catch (ValueError $exception) {
16+
echo $exception->getMessage() . "\n";
17+
}
1418
}
1519
}
1620

@@ -40,19 +44,15 @@ bool(false)
4044
> Offset: 11
4145
bool(false)
4246
> Offset: 12
43-
44-
Warning: strpos(): Offset not contained in string in %s on line %d
45-
bool(false)
47+
Offset not contained in string
4648
> Offset: -1
4749
bool(false)
4850
> Offset: -3
4951
int(8)
5052
> Offset: -6
5153
int(8)
5254
> Offset: -20
53-
54-
Warning: strpos(): Offset not contained in string in %s on line %d
55-
bool(false)
55+
Offset not contained in string
5656

5757
------- mb_strpos -----------
5858

@@ -94,19 +94,15 @@ bool(false)
9494
> Offset: 11
9595
bool(false)
9696
> Offset: 12
97-
98-
Warning: stripos(): Offset not contained in string in %s on line %d
99-
bool(false)
97+
Offset not contained in string
10098
> Offset: -1
10199
bool(false)
102100
> Offset: -3
103101
int(8)
104102
> Offset: -6
105103
int(8)
106104
> Offset: -20
107-
108-
Warning: stripos(): Offset not contained in string in %s on line %d
109-
bool(false)
105+
Offset not contained in string
110106

111107
------- mb_stripos -----------
112108

@@ -148,19 +144,15 @@ bool(false)
148144
> Offset: 11
149145
bool(false)
150146
> Offset: 12
151-
152-
Warning: strrpos(): Offset not contained in string in %s on line %d
153-
bool(false)
147+
Offset not contained in string
154148
> Offset: -1
155149
int(8)
156150
> Offset: -3
157151
int(8)
158152
> Offset: -6
159153
int(4)
160154
> Offset: -20
161-
162-
Warning: strrpos(): Offset not contained in string in %s on line %d
163-
bool(false)
155+
Offset not contained in string
164156

165157
------- mb_strrpos -----------
166158

@@ -202,19 +194,15 @@ bool(false)
202194
> Offset: 11
203195
bool(false)
204196
> Offset: 12
205-
206-
Warning: strripos(): Offset not contained in string in %s on line %d
207-
bool(false)
197+
Offset not contained in string
208198
> Offset: -1
209199
int(8)
210200
> Offset: -3
211201
int(8)
212202
> Offset: -6
213203
int(4)
214204
> Offset: -20
215-
216-
Warning: strripos(): Offset not contained in string in %s on line %d
217-
bool(false)
205+
Offset not contained in string
218206

219207
------- mb_strripos -----------
220208

ext/standard/string.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,8 +1874,8 @@ PHP_FUNCTION(strpos)
18741874
offset += (zend_long)ZSTR_LEN(haystack);
18751875
}
18761876
if (offset < 0 || (size_t)offset > ZSTR_LEN(haystack)) {
1877-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
1878-
RETURN_FALSE;
1877+
zend_value_error("Offset not contained in string");
1878+
return;
18791879
}
18801880

18811881
found = (char*)php_memnstr(ZSTR_VAL(haystack) + offset,
@@ -1910,8 +1910,8 @@ PHP_FUNCTION(stripos)
19101910
offset += (zend_long)ZSTR_LEN(haystack);
19111911
}
19121912
if (offset < 0 || (size_t)offset > ZSTR_LEN(haystack)) {
1913-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
1914-
RETURN_FALSE;
1913+
zend_value_error("Offset not contained in string");
1914+
return;
19151915
}
19161916

19171917
if (ZSTR_LEN(needle) > ZSTR_LEN(haystack)) {
@@ -1952,15 +1952,15 @@ PHP_FUNCTION(strrpos)
19521952

19531953
if (offset >= 0) {
19541954
if ((size_t)offset > ZSTR_LEN(haystack)) {
1955-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
1956-
RETURN_FALSE;
1955+
zend_value_error("Offset not contained in string");
1956+
return;
19571957
}
19581958
p = ZSTR_VAL(haystack) + (size_t)offset;
19591959
e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack);
19601960
} else {
1961-
if (offset < -INT_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
1962-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
1963-
RETURN_FALSE;
1961+
if (offset < -ZEND_LONG_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
1962+
zend_value_error("Offset not contained in string");
1963+
return;
19641964
}
19651965

19661966
p = ZSTR_VAL(haystack);
@@ -2002,16 +2002,16 @@ PHP_FUNCTION(strripos)
20022002
char lowered;
20032003
if (offset >= 0) {
20042004
if ((size_t)offset > ZSTR_LEN(haystack)) {
2005-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
2006-
RETURN_FALSE;
2005+
zend_value_error("Offset not contained in string");
2006+
return;
20072007
}
20082008
p = ZSTR_VAL(haystack) + (size_t)offset;
20092009
e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack) - 1;
20102010
} else {
20112011
p = ZSTR_VAL(haystack);
2012-
if (offset < -INT_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
2013-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
2014-
RETURN_FALSE;
2012+
if (offset < -ZEND_LONG_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
2013+
zend_value_error("Offset not contained in string");
2014+
return;
20152015
}
20162016
e = ZSTR_VAL(haystack) + (ZSTR_LEN(haystack) + (size_t)offset);
20172017
}
@@ -2030,16 +2030,16 @@ PHP_FUNCTION(strripos)
20302030
if (offset >= 0) {
20312031
if ((size_t)offset > ZSTR_LEN(haystack)) {
20322032
zend_string_release_ex(haystack_dup, 0);
2033-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
2034-
RETURN_FALSE;
2033+
zend_value_error("Offset not contained in string");
2034+
return;
20352035
}
20362036
p = ZSTR_VAL(haystack_dup) + offset;
20372037
e = ZSTR_VAL(haystack_dup) + ZSTR_LEN(haystack);
20382038
} else {
2039-
if (offset < -INT_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
2039+
if (offset < -ZEND_LONG_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
20402040
zend_string_release_ex(haystack_dup, 0);
2041-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
2042-
RETURN_FALSE;
2041+
zend_value_error("Offset not contained in string");
2042+
return;
20432043
}
20442044

20452045
p = ZSTR_VAL(haystack_dup);
@@ -5533,8 +5533,8 @@ PHP_FUNCTION(substr_count)
55335533
offset += (zend_long)haystack_len;
55345534
}
55355535
if ((offset < 0) || ((size_t)offset > haystack_len)) {
5536-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
5537-
RETURN_FALSE;
5536+
zend_value_error("Offset not contained in string");
5537+
return;
55385538
}
55395539
p += offset;
55405540

ext/standard/tests/strings/bug40754.phpt

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,52 @@ var_dump(substr_replace("abcde", "x", $v, $v));
1111
var_dump(strspn("abcde", "abc", $v, $v));
1212
var_dump(strcspn("abcde", "abc", $v, $v));
1313

14-
var_dump(substr_count("abcde", "abc", $v, $v));
14+
try {
15+
var_dump(substr_count("abcde", "abc", $v, $v));
16+
} catch (ValueError $exception) {
17+
echo $exception->getMessage() . "\n";
18+
}
19+
1520
var_dump(substr_compare("abcde", "abc", $v, $v));
1621

17-
var_dump(stripos("abcde", "abc", $v));
18-
var_dump(substr_count("abcde", "abc", $v, 1));
22+
try {
23+
stripos("abcde", "abc", $v);
24+
} catch (ValueError $exception) {
25+
echo $exception->getMessage() . "\n";
26+
}
27+
28+
try {
29+
substr_count("abcde", "abc", $v, 1);
30+
} catch (ValueError $exception) {
31+
echo $exception->getMessage() . "\n";
32+
}
33+
1934
var_dump(substr_count("abcde", "abc", 1, $v));
20-
var_dump(strpos("abcde", "abc", $v));
21-
var_dump(stripos("abcde", "abc", $v));
22-
var_dump(strrpos("abcde", "abc", $v));
23-
var_dump(strripos("abcde", "abc", $v));
35+
36+
try {
37+
strpos("abcde", "abc", $v);
38+
} catch (ValueError $exception) {
39+
echo $exception->getMessage() . "\n";
40+
}
41+
42+
try {
43+
stripos("abcde", "abc", $v);
44+
} catch (ValueError $exception) {
45+
echo $exception->getMessage() . "\n";
46+
}
47+
48+
try {
49+
strrpos("abcde", "abc", $v);
50+
} catch (ValueError $exception) {
51+
echo $exception->getMessage() . "\n";
52+
}
53+
54+
try {
55+
strripos("abcde", "abc", $v);
56+
} catch (ValueError $exception) {
57+
echo $exception->getMessage() . "\n";
58+
}
59+
2460
var_dump(strncmp("abcde", "abc", $v));
2561
var_dump(chunk_split("abcde", $v, "abc"));
2662
var_dump(substr("abcde", $v, $v));
@@ -31,33 +67,19 @@ string(4) "bcde"
3167
string(6) "abcdex"
3268
bool(false)
3369
bool(false)
34-
35-
Warning: substr_count(): Offset not contained in string in %s on line %d
36-
bool(false)
70+
Offset not contained in string
3771

3872
Warning: substr_compare(): The start position cannot exceed initial string length in %s on line %d
3973
bool(false)
40-
41-
Warning: stripos(): Offset not contained in string in %s on line %d
42-
bool(false)
43-
44-
Warning: substr_count(): Offset not contained in string in %s on line %d
45-
bool(false)
74+
Offset not contained in string
75+
Offset not contained in string
4676

4777
Warning: substr_count(): Invalid length value in %s on line %d
4878
bool(false)
49-
50-
Warning: strpos(): Offset not contained in string in %s on line %d
51-
bool(false)
52-
53-
Warning: stripos(): Offset not contained in string in %s on line %d
54-
bool(false)
55-
56-
Warning: strrpos(): Offset not contained in string in %s on line %d
57-
bool(false)
58-
59-
Warning: strripos(): Offset not contained in string in %s on line %d
60-
bool(false)
79+
Offset not contained in string
80+
Offset not contained in string
81+
Offset not contained in string
82+
Offset not contained in string
6183
int(2)
6284
string(8) "abcdeabc"
6385
bool(false)

0 commit comments

Comments
 (0)