Skip to content

Commit 9d0a000

Browse files
committed
Promote warnings to exceptions in string search related functions
1 parent 8f8ab37 commit 9d0a000

12 files changed

+225
-190
lines changed

ext/standard/string.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,8 +1883,8 @@ PHP_FUNCTION(strpos)
18831883
offset += (zend_long)ZSTR_LEN(haystack);
18841884
}
18851885
if (offset < 0 || (size_t)offset > ZSTR_LEN(haystack)) {
1886-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
1887-
RETURN_FALSE;
1886+
zend_value_error("Offset not contained in string");
1887+
return;
18881888
}
18891889

18901890
found = (char*)php_memnstr(ZSTR_VAL(haystack) + offset,
@@ -1919,8 +1919,8 @@ PHP_FUNCTION(stripos)
19191919
offset += (zend_long)ZSTR_LEN(haystack);
19201920
}
19211921
if (offset < 0 || (size_t)offset > ZSTR_LEN(haystack)) {
1922-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
1923-
RETURN_FALSE;
1922+
zend_value_error("Offset not contained in string");
1923+
return;
19241924
}
19251925

19261926
if (ZSTR_LEN(needle) > ZSTR_LEN(haystack)) {
@@ -1961,15 +1961,15 @@ PHP_FUNCTION(strrpos)
19611961

19621962
if (offset >= 0) {
19631963
if ((size_t)offset > ZSTR_LEN(haystack)) {
1964-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
1965-
RETURN_FALSE;
1964+
zend_value_error("Offset not contained in string");
1965+
return;
19661966
}
19671967
p = ZSTR_VAL(haystack) + (size_t)offset;
19681968
e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack);
19691969
} else {
19701970
if (offset < -INT_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
1971-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
1972-
RETURN_FALSE;
1971+
zend_value_error("Offset not contained in string");
1972+
return;
19731973
}
19741974

19751975
p = ZSTR_VAL(haystack);
@@ -2011,16 +2011,16 @@ PHP_FUNCTION(strripos)
20112011
char lowered;
20122012
if (offset >= 0) {
20132013
if ((size_t)offset > ZSTR_LEN(haystack)) {
2014-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
2015-
RETURN_FALSE;
2014+
zend_value_error("Offset not contained in string");
2015+
return;
20162016
}
20172017
p = ZSTR_VAL(haystack) + (size_t)offset;
20182018
e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack) - 1;
20192019
} else {
20202020
p = ZSTR_VAL(haystack);
20212021
if (offset < -INT_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
2022-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
2023-
RETURN_FALSE;
2022+
zend_value_error("Offset not contained in string");
2023+
return;
20242024
}
20252025
e = ZSTR_VAL(haystack) + (ZSTR_LEN(haystack) + (size_t)offset);
20262026
}
@@ -2039,16 +2039,16 @@ PHP_FUNCTION(strripos)
20392039
if (offset >= 0) {
20402040
if ((size_t)offset > ZSTR_LEN(haystack)) {
20412041
zend_string_release_ex(haystack_dup, 0);
2042-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
2043-
RETURN_FALSE;
2042+
zend_value_error("Offset not contained in string");
2043+
return;
20442044
}
20452045
p = ZSTR_VAL(haystack_dup) + offset;
20462046
e = ZSTR_VAL(haystack_dup) + ZSTR_LEN(haystack);
20472047
} else {
20482048
if (offset < -INT_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
20492049
zend_string_release_ex(haystack_dup, 0);
2050-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
2051-
RETURN_FALSE;
2050+
zend_value_error("Offset not contained in string");
2051+
return;
20522052
}
20532053

20542054
p = ZSTR_VAL(haystack_dup);
@@ -5580,8 +5580,8 @@ PHP_FUNCTION(substr_count)
55805580
offset += (zend_long)haystack_len;
55815581
}
55825582
if ((offset < 0) || ((size_t)offset > haystack_len)) {
5583-
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
5584-
RETURN_FALSE;
5583+
zend_value_error("Offset not contained in string");
5584+
return;
55855585
}
55865586
p += offset;
55875587

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)

ext/standard/tests/strings/stripos_error.phpt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,28 @@ Test stripos() function : error conditions
99

1010
echo "*** Testing stripos() function: error conditions ***\n";
1111

12-
echo "\n-- Offset beyond the end of the string --";
13-
var_dump( stripos("Hello World", "o", 12) );
12+
echo "\n-- Offset beyond the end of the string --\n";
13+
try {
14+
stripos("Hello World", "o", 12);
15+
} catch (ValueError $exception) {
16+
echo $exception->getMessage() . "\n";
17+
}
1418

15-
echo "\n-- Offset before the start of the string --";
16-
var_dump( stripos("Hello World", "o", -12) );
19+
echo "\n-- Offset before the start of the string --\n";
20+
try {
21+
stripos("Hello World", "o", -12);
22+
} catch (ValueError $exception) {
23+
echo $exception->getMessage() . "\n";
24+
}
1725

1826
echo "*** Done ***";
1927
?>
20-
--EXPECTF--
28+
--EXPECT--
2129
*** Testing stripos() function: error conditions ***
2230

2331
-- Offset beyond the end of the string --
24-
Warning: stripos(): Offset not contained in string in %s on line %d
25-
bool(false)
32+
Offset not contained in string
2633

2734
-- Offset before the start of the string --
28-
Warning: stripos(): Offset not contained in string in %s on line %d
29-
bool(false)
35+
Offset not contained in string
3036
*** Done ***

ext/standard/tests/strings/stripos_variation11.phpt

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ for($index = 0; $index < count($values); $index ++) {
8383
$haystack = $values[$index];
8484
try {
8585
var_dump( stripos($values[$index], $values[$index]) );
86-
} catch (TypeError $e) {
87-
echo $e->getMessage(), "\n";
86+
} catch (Error $e) {
87+
echo get_class($e) . ": " . $e->getMessage(), "\n";
8888
}
8989
try {
9090
var_dump( stripos($values[$index], $values[$index], 1) );
91-
} catch (TypeError $e) {
92-
echo $e->getMessage(), "\n";
91+
} catch (Error $e) {
92+
echo get_class($e) . ": " . $e->getMessage(), "\n";
9393
}
9494
$counter ++;
9595
}
@@ -126,70 +126,54 @@ bool(false)
126126
int(0)
127127
bool(false)
128128
-- Iteration 10 --
129-
stripos() expects parameter 1 to be string, array given
130-
stripos() expects parameter 1 to be string, array given
129+
TypeError: stripos() expects parameter 1 to be string, array given
130+
TypeError: stripos() expects parameter 1 to be string, array given
131131
-- Iteration 11 --
132-
stripos() expects parameter 1 to be string, array given
133-
stripos() expects parameter 1 to be string, array given
132+
TypeError: stripos() expects parameter 1 to be string, array given
133+
TypeError: stripos() expects parameter 1 to be string, array given
134134
-- Iteration 12 --
135-
stripos() expects parameter 1 to be string, array given
136-
stripos() expects parameter 1 to be string, array given
135+
TypeError: stripos() expects parameter 1 to be string, array given
136+
TypeError: stripos() expects parameter 1 to be string, array given
137137
-- Iteration 13 --
138-
stripos() expects parameter 1 to be string, array given
139-
stripos() expects parameter 1 to be string, array given
138+
TypeError: stripos() expects parameter 1 to be string, array given
139+
TypeError: stripos() expects parameter 1 to be string, array given
140140
-- Iteration 14 --
141-
stripos() expects parameter 1 to be string, array given
142-
stripos() expects parameter 1 to be string, array given
141+
TypeError: stripos() expects parameter 1 to be string, array given
142+
TypeError: stripos() expects parameter 1 to be string, array given
143143
-- Iteration 15 --
144144
int(0)
145145
bool(false)
146146
-- Iteration 16 --
147147
int(0)
148-
149-
Warning: stripos(): Offset not contained in string in %s on line %d
150-
bool(false)
148+
ValueError: Offset not contained in string
151149
-- Iteration 17 --
152150
int(0)
153151
bool(false)
154152
-- Iteration 18 --
155153
int(0)
156-
157-
Warning: stripos(): Offset not contained in string in %s on line %d
158-
bool(false)
154+
ValueError: Offset not contained in string
159155
-- Iteration 19 --
160156
int(0)
161157
bool(false)
162158
-- Iteration 20 --
163159
int(0)
164-
165-
Warning: stripos(): Offset not contained in string in %s on line %d
166-
bool(false)
160+
ValueError: Offset not contained in string
167161
-- Iteration 21 --
168162
int(0)
169-
170-
Warning: stripos(): Offset not contained in string in %s on line %d
171-
bool(false)
163+
ValueError: Offset not contained in string
172164
-- Iteration 22 --
173165
int(0)
174-
175-
Warning: stripos(): Offset not contained in string in %s on line %d
176-
bool(false)
166+
ValueError: Offset not contained in string
177167
-- Iteration 23 --
178168
int(0)
179-
180-
Warning: stripos(): Offset not contained in string in %s on line %d
181-
bool(false)
169+
ValueError: Offset not contained in string
182170
-- Iteration 24 --
183171
stripos() expects parameter 1 to be string, resource given
184172
stripos() expects parameter 1 to be string, resource given
185173
-- Iteration 25 --
186174
int(0)
187-
188-
Warning: stripos(): Offset not contained in string in %s on line %d
189-
bool(false)
175+
ValueError: Offset not contained in string
190176
-- Iteration 26 --
191177
int(0)
192-
193-
Warning: stripos(): Offset not contained in string in %s on line %d
194-
bool(false)
178+
ValueError: Offset not contained in string
195179
*** Done ***

ext/standard/tests/strings/stripos_variation7.phpt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@ echo "-- With empty heredoc string --\n";
1616
$empty_string = <<<EOD
1717
EOD;
1818
var_dump( stripos($empty_string, "") );
19-
var_dump( stripos($empty_string, "", 1) );
19+
20+
try {
21+
stripos($empty_string, "", 1);
22+
} catch (ValueError $exception) {
23+
echo $exception->getMessage() . "\n";
24+
}
2025
var_dump( stripos($empty_string, FALSE) );
2126
var_dump( stripos($empty_string, NULL) );
2227

2328
echo "*** Done ***";
2429
?>
25-
--EXPECTF--
30+
--EXPECT--
2631
*** Testing stripos() function: with heredoc strings ***
2732
-- With empty heredoc string --
2833
int(0)
29-
30-
Warning: stripos(): Offset not contained in string in %s on line %d
31-
bool(false)
34+
Offset not contained in string
3235
int(0)
3336
int(0)
3437
*** Done ***
51 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)