From 256a34ed1598698a2a971cf72968ce3349523f5d Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 28 Nov 2022 20:40:32 +0000 Subject: [PATCH] strtok warns in case the string to split was not set. Close GH-10016. --- NEWS | 2 ++ UPGRADING | 1 + ext/standard/string.c | 2 +- .../tests/strings/strtok_variation3.phpt | 32 ++++++++++++++++++- .../tests/strings/strtok_variation4.phpt | 20 +++++++++++- .../tests/strings/strtok_variation5.phpt | 32 ++++++++++++++++++- .../tests/strings/strtok_variation6.phpt | 10 +++++- 7 files changed, 94 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index eb6c171a676..4a2204c53cb 100644 --- a/NEWS +++ b/NEWS @@ -99,6 +99,8 @@ PHP NEWS - Standard: . E_NOTICEs emitted by unserialize() have been promoted to E_WARNING. (timwolla) . Make array_pad's $length warning less confusing. (nielsdos) + . E_WARNING emitted by strtok in the caase both arguments are not provided when + starting tokenisation. (David Carlier) - Streams: . Fixed bug #51056: blocking fread() will block even if data is available. diff --git a/UPGRADING b/UPGRADING index fbcca037489..c27fa861581 100644 --- a/UPGRADING +++ b/UPGRADING @@ -68,6 +68,7 @@ PHP 8.3 UPGRADE NOTES . array_pad() is now only limited by the maximum number of elements an array can have. Before, it was only possible to add at most 1048576 elements at a time. + . strtok() raises a warninin the case token is not provided when starting tokenization. ======================================== 6. New Functions diff --git a/ext/standard/string.c b/ext/standard/string.c index feaf568745a..bd8ea063d9a 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1080,7 +1080,7 @@ PHP_FUNCTION(strtok) if (!BG(strtok_string)) { /* String to tokenize not set. */ - // TODO: Should this warn? + php_error_docref(NULL, E_WARNING, "Both arguments must be provided when starting tokenization"); RETURN_FALSE; } diff --git a/ext/standard/tests/strings/strtok_variation3.phpt b/ext/standard/tests/strings/strtok_variation3.phpt index 8ae1e03e7db..d5cf1ad91db 100644 --- a/ext/standard/tests/strings/strtok_variation3.phpt +++ b/ext/standard/tests/strings/strtok_variation3.phpt @@ -62,7 +62,7 @@ foreach($heredoc_strings as $string) { echo "Done\n"; ?> ---EXPECT-- +--EXPECTF-- *** Testing strtok() : with heredoc strings *** --- Iteration 1 --- @@ -80,15 +80,35 @@ bool(false) --- Iteration 2 --- bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) --- Iteration 3 --- @@ -137,9 +157,19 @@ string(3) "rld" string(4) "hell" string(4) "hell" bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) Done diff --git a/ext/standard/tests/strings/strtok_variation4.phpt b/ext/standard/tests/strings/strtok_variation4.phpt index 63090497097..671cf74b031 100644 --- a/ext/standard/tests/strings/strtok_variation4.phpt +++ b/ext/standard/tests/strings/strtok_variation4.phpt @@ -36,15 +36,25 @@ foreach( $strings_with_nulls as $string ) { echo "Done\n"; ?> ---EXPECT-- +--EXPECTF-- *** Testing strtok() : with embedded nulls in the strings *** --- Iteration 1 --- bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) --- Iteration 2 --- @@ -82,9 +92,17 @@ bool(false) --- Iteration 6 --- string(11) "hello world" bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) --- Iteration 7 --- diff --git a/ext/standard/tests/strings/strtok_variation5.phpt b/ext/standard/tests/strings/strtok_variation5.phpt index 006cf7a9f96..bc33315b960 100644 --- a/ext/standard/tests/strings/strtok_variation5.phpt +++ b/ext/standard/tests/strings/strtok_variation5.phpt @@ -52,7 +52,7 @@ foreach( $string_array as $string ) { echo "Done\n"; ?> ---EXPECT-- +--EXPECTF-- *** Testing strtok() : with miscellaneous inputs *** --- Iteration 1 --- @@ -65,10 +65,20 @@ bool(false) --- Iteration 2 --- bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) --- Iteration 3 --- @@ -113,18 +123,38 @@ bool(false) --- Iteration 8 --- bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) --- Iteration 9 --- bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) --- Iteration 10 --- diff --git a/ext/standard/tests/strings/strtok_variation6.phpt b/ext/standard/tests/strings/strtok_variation6.phpt index 9b3f1d244d8..255c100a0b1 100644 --- a/ext/standard/tests/strings/strtok_variation6.phpt +++ b/ext/standard/tests/strings/strtok_variation6.phpt @@ -42,7 +42,7 @@ foreach( $string_array as $string ) { echo "Done\n"; ?> ---EXPECT-- +--EXPECTF-- *** Testing strtok() : with invalid escape sequences in token *** --- Iteration 1 --- @@ -69,6 +69,8 @@ bool(false) string(1) " " string(1) "r" bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) @@ -91,11 +93,15 @@ bool(false) string(5) "hello" string(6) " world" bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) string(1) " " string(1) "r" bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) @@ -113,6 +119,8 @@ bool(false) string(6) "hello\" string(6) " world" bool(false) + +Warning: strtok(): Both arguments must be provided when starting tokenization in %s on line %d bool(false) string(1) "/"