Skip to content

Commit 256a34e

Browse files
committed
strtok warns in case the string to split was not set.
Close GH-10016.
1 parent 7936c80 commit 256a34e

File tree

7 files changed

+94
-5
lines changed

7 files changed

+94
-5
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ PHP NEWS
9999
- Standard:
100100
. E_NOTICEs emitted by unserialize() have been promoted to E_WARNING. (timwolla)
101101
. Make array_pad's $length warning less confusing. (nielsdos)
102+
. E_WARNING emitted by strtok in the caase both arguments are not provided when
103+
starting tokenisation. (David Carlier)
102104

103105
- Streams:
104106
. Fixed bug #51056: blocking fread() will block even if data is available.

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ PHP 8.3 UPGRADE NOTES
6868
. array_pad() is now only limited by the maximum number of elements an array
6969
can have. Before, it was only possible to add at most 1048576 elements at a
7070
time.
71+
. strtok() raises a warninin the case token is not provided when starting tokenization.
7172

7273
========================================
7374
6. New Functions

ext/standard/string.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ PHP_FUNCTION(strtok)
10801080

10811081
if (!BG(strtok_string)) {
10821082
/* String to tokenize not set. */
1083-
// TODO: Should this warn?
1083+
php_error_docref(NULL, E_WARNING, "Both arguments must be provided when starting tokenization");
10841084
RETURN_FALSE;
10851085
}
10861086

ext/standard/tests/strings/strtok_variation3.phpt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ foreach($heredoc_strings as $string) {
6262

6363
echo "Done\n";
6464
?>
65-
--EXPECT--
65+
--EXPECTF--
6666
*** Testing strtok() : with heredoc strings ***
6767

6868
--- Iteration 1 ---
@@ -80,15 +80,35 @@ bool(false)
8080

8181
--- Iteration 2 ---
8282
bool(false)
83+
84+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
8385
bool(false)
86+
87+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
8488
bool(false)
89+
90+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
8591
bool(false)
92+
93+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
8694
bool(false)
95+
96+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
8797
bool(false)
98+
99+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
88100
bool(false)
101+
102+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
89103
bool(false)
104+
105+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
90106
bool(false)
107+
108+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
91109
bool(false)
110+
111+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
92112
bool(false)
93113

94114
--- Iteration 3 ---
@@ -137,9 +157,19 @@ string(3) "rld"
137157
string(4) "hell"
138158
string(4) "hell"
139159
bool(false)
160+
161+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
140162
bool(false)
163+
164+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
141165
bool(false)
166+
167+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
142168
bool(false)
169+
170+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
143171
bool(false)
172+
173+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
144174
bool(false)
145175
Done

ext/standard/tests/strings/strtok_variation4.phpt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,25 @@ foreach( $strings_with_nulls as $string ) {
3636

3737
echo "Done\n";
3838
?>
39-
--EXPECT--
39+
--EXPECTF--
4040
*** Testing strtok() : with embedded nulls in the strings ***
4141

4242
--- Iteration 1 ---
4343
bool(false)
44+
45+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
4446
bool(false)
47+
48+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
4549
bool(false)
50+
51+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
4652
bool(false)
53+
54+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
4755
bool(false)
56+
57+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
4858
bool(false)
4959

5060
--- Iteration 2 ---
@@ -82,9 +92,17 @@ bool(false)
8292
--- Iteration 6 ---
8393
string(11) "hello world"
8494
bool(false)
95+
96+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
8597
bool(false)
98+
99+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
86100
bool(false)
101+
102+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
87103
bool(false)
104+
105+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
88106
bool(false)
89107

90108
--- Iteration 7 ---

ext/standard/tests/strings/strtok_variation5.phpt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ foreach( $string_array as $string ) {
5252

5353
echo "Done\n";
5454
?>
55-
--EXPECT--
55+
--EXPECTF--
5656
*** Testing strtok() : with miscellaneous inputs ***
5757

5858
--- Iteration 1 ---
@@ -65,10 +65,20 @@ bool(false)
6565

6666
--- Iteration 2 ---
6767
bool(false)
68+
69+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
6870
bool(false)
71+
72+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
6973
bool(false)
74+
75+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
7076
bool(false)
77+
78+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
7179
bool(false)
80+
81+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
7282
bool(false)
7383

7484
--- Iteration 3 ---
@@ -113,18 +123,38 @@ bool(false)
113123

114124
--- Iteration 8 ---
115125
bool(false)
126+
127+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
116128
bool(false)
129+
130+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
117131
bool(false)
132+
133+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
118134
bool(false)
135+
136+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
119137
bool(false)
138+
139+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
120140
bool(false)
121141

122142
--- Iteration 9 ---
123143
bool(false)
144+
145+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
124146
bool(false)
147+
148+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
125149
bool(false)
150+
151+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
126152
bool(false)
153+
154+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
127155
bool(false)
156+
157+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
128158
bool(false)
129159

130160
--- Iteration 10 ---

ext/standard/tests/strings/strtok_variation6.phpt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ foreach( $string_array as $string ) {
4242

4343
echo "Done\n";
4444
?>
45-
--EXPECT--
45+
--EXPECTF--
4646
*** Testing strtok() : with invalid escape sequences in token ***
4747

4848
--- Iteration 1 ---
@@ -69,6 +69,8 @@ bool(false)
6969
string(1) " "
7070
string(1) "r"
7171
bool(false)
72+
73+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
7274
bool(false)
7375

7476

@@ -91,11 +93,15 @@ bool(false)
9193
string(5) "hello"
9294
string(6) " world"
9395
bool(false)
96+
97+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
9498
bool(false)
9599

96100
string(1) " "
97101
string(1) "r"
98102
bool(false)
103+
104+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
99105
bool(false)
100106

101107

@@ -113,6 +119,8 @@ bool(false)
113119
string(6) "hello\"
114120
string(6) " world"
115121
bool(false)
122+
123+
Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d
116124
bool(false)
117125

118126
string(1) "/"

0 commit comments

Comments
 (0)