From 469981bf177838b733a55f6a2549c67513230c45 Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Wed, 15 Nov 2023 19:37:56 -0300 Subject: [PATCH 01/14] Fixes #12682 --- ext/standard/string.c | 2 +- ext/standard/tests/strings/join_error1.phpt | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 ext/standard/tests/strings/join_error1.phpt diff --git a/ext/standard/string.c b/ext/standard/string.c index ae8deace66da3..22eddfbe6e653 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1035,7 +1035,7 @@ PHP_FUNCTION(implode) if (pieces == NULL) { if (arg1_array == NULL) { - zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name()); + zend_type_error("%s(): Argument #1 ($array) must be of type array, null given", get_active_function_name()); RETURN_THROWS(); } diff --git a/ext/standard/tests/strings/join_error1.phpt b/ext/standard/tests/strings/join_error1.phpt new file mode 100644 index 0000000000000..91b176b90881e --- /dev/null +++ b/ext/standard/tests/strings/join_error1.phpt @@ -0,0 +1,12 @@ +--TEST-- +join() function - error when passing null as the array parameter +--FILE-- +getMessage(); +} +?> +--EXPECT-- +join(): Argument #2 ($array) must be of type array, null given From af47cdfccf1f652be77d985e663f59cf2aa0658f Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Wed, 15 Nov 2023 19:49:19 -0300 Subject: [PATCH 02/14] Adding correct check and error message --- ext/standard/string.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index 22eddfbe6e653..51df21b7e5dc3 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1033,9 +1033,14 @@ PHP_FUNCTION(implode) Z_PARAM_ARRAY_HT_OR_NULL(pieces) ZEND_PARSE_PARAMETERS_END(); + if (arg1_str != NULL && pieces == NULL) { + zend_type_error("%s(): Argument #2 ($array) must be of type array, null given", get_active_function_name()); + RETURN_THROWS(); + } + if (pieces == NULL) { if (arg1_array == NULL) { - zend_type_error("%s(): Argument #1 ($array) must be of type array, null given", get_active_function_name()); + zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name()); RETURN_THROWS(); } From b2494f1d1b2dbc0a4c6160da88b23c231d52bd29 Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Wed, 15 Nov 2023 20:29:35 -0300 Subject: [PATCH 03/14] Splitting paths as suggested by @iluuu1994 --- ext/standard/string.c | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index 51df21b7e5dc3..8f4998820d07b 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1024,33 +1024,21 @@ PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return PHP_FUNCTION(implode) { zend_string *arg1_str = NULL; - HashTable *arg1_array = NULL; zend_array *pieces = NULL; - ZEND_PARSE_PARAMETERS_START(1, 2) - Z_PARAM_ARRAY_HT_OR_STR(arg1_array, arg1_str) - Z_PARAM_OPTIONAL - Z_PARAM_ARRAY_HT_OR_NULL(pieces) - ZEND_PARSE_PARAMETERS_END(); + if (ZEND_NUM_ARGS() == 1) { + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_ARRAY_HT(pieces) + ZEND_PARSE_PARAMETERS_END(); - if (arg1_str != NULL && pieces == NULL) { - zend_type_error("%s(): Argument #2 ($array) must be of type array, null given", get_active_function_name()); - RETURN_THROWS(); + arg1_str = ZSTR_EMPTY_ALLOC(); } - if (pieces == NULL) { - if (arg1_array == NULL) { - zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name()); - RETURN_THROWS(); - } - - arg1_str = ZSTR_EMPTY_ALLOC(); - pieces = arg1_array; - } else { - if (arg1_str == NULL) { - zend_argument_type_error(1, "must be of type string, array given"); - RETURN_THROWS(); - } + if (ZEND_NUM_ARGS() == 2) { + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_STR(arg1_str) + Z_PARAM_ARRAY_HT(pieces) + ZEND_PARSE_PARAMETERS_END(); } php_implode(arg1_str, pieces, return_value); From 0581bf18ffaacd9a3d160d1f9b921bf138b0714b Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Wed, 15 Nov 2023 21:46:56 -0300 Subject: [PATCH 04/14] Fixing parameters parsing --- ext/standard/string.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index 8f4998820d07b..d6d4097fc2e5d 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1026,7 +1026,7 @@ PHP_FUNCTION(implode) zend_string *arg1_str = NULL; zend_array *pieces = NULL; - if (ZEND_NUM_ARGS() == 1) { + if (ZEND_NUM_ARGS() <= 1) { ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_ARRAY_HT(pieces) ZEND_PARSE_PARAMETERS_END(); @@ -1034,8 +1034,8 @@ PHP_FUNCTION(implode) arg1_str = ZSTR_EMPTY_ALLOC(); } - if (ZEND_NUM_ARGS() == 2) { - ZEND_PARSE_PARAMETERS_START(1, 2) + if (ZEND_NUM_ARGS() >= 2) { + ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_STR(arg1_str) Z_PARAM_ARRAY_HT(pieces) ZEND_PARSE_PARAMETERS_END(); From d3524bdb4ec8d07ad3309e1225ece75649067405 Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Wed, 15 Nov 2023 21:53:17 -0300 Subject: [PATCH 05/14] Simplifying the parameter check conditions --- ext/standard/string.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index d6d4097fc2e5d..880644fb2ef95 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1026,15 +1026,13 @@ PHP_FUNCTION(implode) zend_string *arg1_str = NULL; zend_array *pieces = NULL; - if (ZEND_NUM_ARGS() <= 1) { + if (ZEND_NUM_ARGS() == 1) { ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_ARRAY_HT(pieces) ZEND_PARSE_PARAMETERS_END(); arg1_str = ZSTR_EMPTY_ALLOC(); - } - - if (ZEND_NUM_ARGS() >= 2) { + } else { ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_STR(arg1_str) Z_PARAM_ARRAY_HT(pieces) From 737d829911a9363dd47fb1273d3935e3d47cdc96 Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Wed, 15 Nov 2023 22:36:16 -0300 Subject: [PATCH 06/14] Tests fixed and code approach reverted to the original one --- ext/standard/string.c | 30 +++++++--- ext/standard/tests/strings/implode1.phpt | 56 +------------------ ext/standard/tests/strings/implode_error.phpt | 54 ++++++++++++++++++ ext/standard/tests/strings/join_error.phpt | 2 +- ...{join_variation2.phpt => join_error2.phpt} | 46 +++++++-------- 5 files changed, 101 insertions(+), 87 deletions(-) create mode 100644 ext/standard/tests/strings/implode_error.phpt rename ext/standard/tests/strings/{join_variation2.phpt => join_error2.phpt} (59%) diff --git a/ext/standard/string.c b/ext/standard/string.c index 880644fb2ef95..6e9d8d9626c61 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1024,19 +1024,33 @@ PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return PHP_FUNCTION(implode) { zend_string *arg1_str = NULL; + HashTable *arg1_array = NULL; zend_array *pieces = NULL; - if (ZEND_NUM_ARGS() == 1) { - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_ARRAY_HT(pieces) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_ARRAY_HT_OR_STR(arg1_array, arg1_str) + Z_PARAM_OPTIONAL + Z_PARAM_ARRAY_HT(pieces) + ZEND_PARSE_PARAMETERS_END(); + + if (arg1_str != NULL && pieces == NULL) { + zend_type_error("%s(): Argument #2 ($array) must be of type array, null given", get_active_function_name()); + RETURN_THROWS(); + } + + if (pieces == NULL) { + if (arg1_array == NULL) { + zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name()); + RETURN_THROWS(); + } arg1_str = ZSTR_EMPTY_ALLOC(); + pieces = arg1_array; } else { - ZEND_PARSE_PARAMETERS_START(2, 2) - Z_PARAM_STR(arg1_str) - Z_PARAM_ARRAY_HT(pieces) - ZEND_PARSE_PARAMETERS_END(); + if (arg1_str == NULL) { + zend_argument_type_error(1, "must be of type string, array given"); + RETURN_THROWS(); + } } php_implode(arg1_str, pieces, return_value); diff --git a/ext/standard/tests/strings/implode1.phpt b/ext/standard/tests/strings/implode1.phpt index d467a37dd3175..ee5a48a8e5052 100644 --- a/ext/standard/tests/strings/implode1.phpt +++ b/ext/standard/tests/strings/implode1.phpt @@ -106,50 +106,6 @@ $resources = array($file_handle, $dir_handle); var_dump( implode("::", $resources) ); -echo "\n*** Testing error conditions ***\n"; - -/* only glue */ -try { - var_dump( implode("glue") ); -} catch (TypeError $e) { - echo $e->getMessage(), "\n"; -} - -/* int as pieces */ -try { - var_dump( implode("glue",1234) ); -} catch (TypeError $e) { - echo $e->getMessage(), "\n"; -} - -/* NULL as pieces */ -try { - var_dump( implode("glue", NULL) ); -} catch (TypeError $e) { - echo $e->getMessage(), "\n"; -} - -/* pieces as NULL array */ -try { - var_dump( implode(",", array(NULL)) ); -} catch (TypeError $e) { - echo $e->getMessage(), "\n"; -} - -/* integer as glue */ -try { - var_dump( implode(12, "pieces") ); -} catch (TypeError $e) { - echo $e->getMessage(), "\n"; -} - -/* NULL as glue */ -try { - var_dump( implode(NULL, "abcd") ); -} catch (TypeError $e) { - echo $e->getMessage(), "\n"; -} - /* closing resource handles */ fclose($file_handle); closedir($dir_handle); @@ -236,7 +192,7 @@ string(35) "2000-639010PHP000 0string%0with%0...%0" string(43) "2\00\0-639\01\0PHP\0\0\0 \0string%0with%0...%0" *** Testing implode() on empty string *** -implode(): Argument #1 ($array) must be of type array, string given +implode(): Argument #2 ($array) must be of type array, null given *** Testing implode() on sub-arrays *** @@ -264,14 +220,4 @@ array(2) { *** Testing end() on resource type *** string(%d) "Resource id #%d::Resource id #%d" - -*** Testing error conditions *** -implode(): Argument #1 ($array) must be of type array, string given -implode(): Argument #2 ($array) must be of type ?array, int given -implode(): Argument #1 ($array) must be of type array, string given -string(0) "" -implode(): Argument #2 ($array) must be of type ?array, string given - -Deprecated: implode(): Passing null to parameter #1 ($separator) of type array|string is deprecated in %s on line %d -implode(): Argument #2 ($array) must be of type ?array, string given Done diff --git a/ext/standard/tests/strings/implode_error.phpt b/ext/standard/tests/strings/implode_error.phpt new file mode 100644 index 0000000000000..af047d6c5ed28 --- /dev/null +++ b/ext/standard/tests/strings/implode_error.phpt @@ -0,0 +1,54 @@ +--TEST-- +--FILE-- +getMessage(), "\n"; +} + +/* int as pieces */ +try { + var_dump( implode("glue",1234) ); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* NULL as pieces */ +try { + var_dump( implode("glue", NULL) ); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* pieces as NULL array */ +try { + var_dump( implode(",", array(NULL)) ); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* integer as glue */ +try { + var_dump( implode(12, "pieces") ); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* NULL as glue */ +try { + var_dump( implode(NULL, "abcd") ); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} +?> +--EXPECTF-- +implode(): Argument #2 ($array) must be of type array, null given +implode(): Argument #2 ($array) must be of type array, int given +implode(): Argument #2 ($array) must be of type array, null given +string(0) "" +implode(): Argument #2 ($array) must be of type array, string given + +Deprecated: implode(): Passing null to parameter #1 ($separator) of type array|string is deprecated in %s on line %d +implode(): Argument #2 ($array) must be of type array, string given diff --git a/ext/standard/tests/strings/join_error.phpt b/ext/standard/tests/strings/join_error.phpt index c3cee28812d30..65ff0f40c72eb 100644 --- a/ext/standard/tests/strings/join_error.phpt +++ b/ext/standard/tests/strings/join_error.phpt @@ -20,5 +20,5 @@ echo "Done\n"; *** Testing join() : error conditions *** -- Testing join() with less than expected no. of arguments -- -join(): Argument #1 ($array) must be of type array, string given +join(): Argument #2 ($array) must be of type array, null given Done diff --git a/ext/standard/tests/strings/join_variation2.phpt b/ext/standard/tests/strings/join_error2.phpt similarity index 59% rename from ext/standard/tests/strings/join_variation2.phpt rename to ext/standard/tests/strings/join_error2.phpt index f71f5c31f0cbb..1bd0aa689d232 100644 --- a/ext/standard/tests/strings/join_variation2.phpt +++ b/ext/standard/tests/strings/join_error2.phpt @@ -102,49 +102,49 @@ echo "Done\n"; --- Testing join() by supplying different values for 'pieces' argument --- -- Iteration 1 -- -join(): Argument #2 ($array) must be of type ?array, int given +join(): Argument #2 ($array) must be of type array, int given -- Iteration 2 -- -join(): Argument #2 ($array) must be of type ?array, int given +join(): Argument #2 ($array) must be of type array, int given -- Iteration 3 -- -join(): Argument #2 ($array) must be of type ?array, int given +join(): Argument #2 ($array) must be of type array, int given -- Iteration 4 -- -join(): Argument #2 ($array) must be of type ?array, int given +join(): Argument #2 ($array) must be of type array, int given -- Iteration 5 -- -join(): Argument #2 ($array) must be of type ?array, float given +join(): Argument #2 ($array) must be of type array, float given -- Iteration 6 -- -join(): Argument #2 ($array) must be of type ?array, float given +join(): Argument #2 ($array) must be of type array, float given -- Iteration 7 -- -join(): Argument #2 ($array) must be of type ?array, float given +join(): Argument #2 ($array) must be of type array, float given -- Iteration 8 -- -join(): Argument #2 ($array) must be of type ?array, float given +join(): Argument #2 ($array) must be of type array, float given -- Iteration 9 -- -join(): Argument #2 ($array) must be of type ?array, float given +join(): Argument #2 ($array) must be of type array, float given -- Iteration 10 -- -join(): Argument #2 ($array) must be of type ?array, true given +join(): Argument #2 ($array) must be of type array, true given -- Iteration 11 -- -join(): Argument #2 ($array) must be of type ?array, false given +join(): Argument #2 ($array) must be of type array, false given -- Iteration 12 -- -join(): Argument #2 ($array) must be of type ?array, true given +join(): Argument #2 ($array) must be of type array, true given -- Iteration 13 -- -join(): Argument #2 ($array) must be of type ?array, false given +join(): Argument #2 ($array) must be of type array, false given -- Iteration 14 -- -join(): Argument #2 ($array) must be of type ?array, string given +join(): Argument #2 ($array) must be of type array, string given -- Iteration 15 -- -join(): Argument #2 ($array) must be of type ?array, string given +join(): Argument #2 ($array) must be of type array, string given -- Iteration 16 -- -join(): Argument #2 ($array) must be of type ?array, test given +join(): Argument #2 ($array) must be of type array, test given -- Iteration 17 -- -join(): Argument #2 ($array) must be of type ?array, string given +join(): Argument #2 ($array) must be of type array, string given -- Iteration 18 -- -join(): Argument #2 ($array) must be of type ?array, string given +join(): Argument #2 ($array) must be of type array, string given -- Iteration 19 -- -join(): Argument #1 ($array) must be of type array, string given +join(): Argument #2 ($array) must be of type array, null given -- Iteration 20 -- -join(): Argument #1 ($array) must be of type array, string given +join(): Argument #2 ($array) must be of type array, null given -- Iteration 21 -- -join(): Argument #2 ($array) must be of type ?array, resource given +join(): Argument #2 ($array) must be of type array, resource given -- Iteration 22 -- -join(): Argument #1 ($array) must be of type array, string given +join(): Argument #2 ($array) must be of type array, null given -- Iteration 23 -- -join(): Argument #1 ($array) must be of type array, string given +join(): Argument #2 ($array) must be of type array, null given Done From 63648cce5a3085c79015ca5a120bd39e8430489a Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Wed, 15 Nov 2023 22:38:26 -0300 Subject: [PATCH 07/14] Removing duplicated test --- ext/standard/tests/strings/join_error1.phpt | 148 ++++++++++++++++++- ext/standard/tests/strings/join_error2.phpt | 150 -------------------- 2 files changed, 143 insertions(+), 155 deletions(-) delete mode 100644 ext/standard/tests/strings/join_error2.phpt diff --git a/ext/standard/tests/strings/join_error1.phpt b/ext/standard/tests/strings/join_error1.phpt index 91b176b90881e..1bd0aa689d232 100644 --- a/ext/standard/tests/strings/join_error1.phpt +++ b/ext/standard/tests/strings/join_error1.phpt @@ -1,12 +1,150 @@ --TEST-- -join() function - error when passing null as the array parameter +Test join() function : usage variations - unexpected values for 'pieces' argument(Bug#42789) --FILE-- getMessage(); +/* + * test join() by passing different unexpected value for pieces argument +*/ + +echo "*** Testing join() : usage variations ***\n"; +// initialize all required variables +$glue = '::'; + +// get an unset variable +$unset_var = array(1, 2); +unset($unset_var); + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// define a class +class test +{ + var $t = 10; + var $p = 10; + function __toString() { + return "testObject"; + } } + +// array with different values +$values = array ( + + // integer values + 0, + 1, + 12345, + -2345, + + // float values + 10.5, + -10.5, + 10.5e10, + 10.6E-10, + .5, + + // boolean values + true, + false, + TRUE, + FALSE, + + // string values + "string", + 'string', + + // objects + new test(), + + // empty string + "", + '', + + // null values + NULL, + null, + + // resource variable + $fp, + + // undefined variable + @$undefined_var, + + // unset variable + @$unset_var +); + + +// loop through each element of the array and check the working of join() +// when $pieces argument is supplied with different values +echo "\n--- Testing join() by supplying different values for 'pieces' argument ---\n"; +$counter = 1; +for($index = 0; $index < count($values); $index ++) { + echo "-- Iteration $counter --\n"; + $pieces = $values [$index]; + + try { + var_dump( join($glue, $pieces) ); + } catch (TypeError $e) { + echo $e->getMessage(), "\n"; + } + + $counter ++; +} + +// close the resources used +fclose($fp); + +echo "Done\n"; ?> --EXPECT-- +*** Testing join() : usage variations *** + +--- Testing join() by supplying different values for 'pieces' argument --- +-- Iteration 1 -- +join(): Argument #2 ($array) must be of type array, int given +-- Iteration 2 -- +join(): Argument #2 ($array) must be of type array, int given +-- Iteration 3 -- +join(): Argument #2 ($array) must be of type array, int given +-- Iteration 4 -- +join(): Argument #2 ($array) must be of type array, int given +-- Iteration 5 -- +join(): Argument #2 ($array) must be of type array, float given +-- Iteration 6 -- +join(): Argument #2 ($array) must be of type array, float given +-- Iteration 7 -- +join(): Argument #2 ($array) must be of type array, float given +-- Iteration 8 -- +join(): Argument #2 ($array) must be of type array, float given +-- Iteration 9 -- +join(): Argument #2 ($array) must be of type array, float given +-- Iteration 10 -- +join(): Argument #2 ($array) must be of type array, true given +-- Iteration 11 -- +join(): Argument #2 ($array) must be of type array, false given +-- Iteration 12 -- +join(): Argument #2 ($array) must be of type array, true given +-- Iteration 13 -- +join(): Argument #2 ($array) must be of type array, false given +-- Iteration 14 -- +join(): Argument #2 ($array) must be of type array, string given +-- Iteration 15 -- +join(): Argument #2 ($array) must be of type array, string given +-- Iteration 16 -- +join(): Argument #2 ($array) must be of type array, test given +-- Iteration 17 -- +join(): Argument #2 ($array) must be of type array, string given +-- Iteration 18 -- +join(): Argument #2 ($array) must be of type array, string given +-- Iteration 19 -- +join(): Argument #2 ($array) must be of type array, null given +-- Iteration 20 -- +join(): Argument #2 ($array) must be of type array, null given +-- Iteration 21 -- +join(): Argument #2 ($array) must be of type array, resource given +-- Iteration 22 -- +join(): Argument #2 ($array) must be of type array, null given +-- Iteration 23 -- join(): Argument #2 ($array) must be of type array, null given +Done diff --git a/ext/standard/tests/strings/join_error2.phpt b/ext/standard/tests/strings/join_error2.phpt deleted file mode 100644 index 1bd0aa689d232..0000000000000 --- a/ext/standard/tests/strings/join_error2.phpt +++ /dev/null @@ -1,150 +0,0 @@ ---TEST-- -Test join() function : usage variations - unexpected values for 'pieces' argument(Bug#42789) ---FILE-- -getMessage(), "\n"; - } - - $counter ++; -} - -// close the resources used -fclose($fp); - -echo "Done\n"; -?> ---EXPECT-- -*** Testing join() : usage variations *** - ---- Testing join() by supplying different values for 'pieces' argument --- --- Iteration 1 -- -join(): Argument #2 ($array) must be of type array, int given --- Iteration 2 -- -join(): Argument #2 ($array) must be of type array, int given --- Iteration 3 -- -join(): Argument #2 ($array) must be of type array, int given --- Iteration 4 -- -join(): Argument #2 ($array) must be of type array, int given --- Iteration 5 -- -join(): Argument #2 ($array) must be of type array, float given --- Iteration 6 -- -join(): Argument #2 ($array) must be of type array, float given --- Iteration 7 -- -join(): Argument #2 ($array) must be of type array, float given --- Iteration 8 -- -join(): Argument #2 ($array) must be of type array, float given --- Iteration 9 -- -join(): Argument #2 ($array) must be of type array, float given --- Iteration 10 -- -join(): Argument #2 ($array) must be of type array, true given --- Iteration 11 -- -join(): Argument #2 ($array) must be of type array, false given --- Iteration 12 -- -join(): Argument #2 ($array) must be of type array, true given --- Iteration 13 -- -join(): Argument #2 ($array) must be of type array, false given --- Iteration 14 -- -join(): Argument #2 ($array) must be of type array, string given --- Iteration 15 -- -join(): Argument #2 ($array) must be of type array, string given --- Iteration 16 -- -join(): Argument #2 ($array) must be of type array, test given --- Iteration 17 -- -join(): Argument #2 ($array) must be of type array, string given --- Iteration 18 -- -join(): Argument #2 ($array) must be of type array, string given --- Iteration 19 -- -join(): Argument #2 ($array) must be of type array, null given --- Iteration 20 -- -join(): Argument #2 ($array) must be of type array, null given --- Iteration 21 -- -join(): Argument #2 ($array) must be of type array, resource given --- Iteration 22 -- -join(): Argument #2 ($array) must be of type array, null given --- Iteration 23 -- -join(): Argument #2 ($array) must be of type array, null given -Done From 48a080ec85b37e73bf5ac15ad863b0c482fe73f9 Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Thu, 16 Nov 2023 23:30:03 -0300 Subject: [PATCH 08/14] Rewriting the error message and moving test conditions --- ext/standard/string.c | 6 +++++- .../strings/{implode.phpt => implode_basic.phpt} | 0 ext/standard/tests/strings/implode_error.phpt | 16 ---------------- .../{implode1.phpt => implode_variation.phpt} | 8 +++++++- 4 files changed, 12 insertions(+), 18 deletions(-) rename ext/standard/tests/strings/{implode.phpt => implode_basic.phpt} (100%) rename ext/standard/tests/strings/{implode1.phpt => implode_variation.phpt} (97%) diff --git a/ext/standard/string.c b/ext/standard/string.c index 6e9d8d9626c61..96e27569d10d0 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1034,7 +1034,11 @@ PHP_FUNCTION(implode) ZEND_PARSE_PARAMETERS_END(); if (arg1_str != NULL && pieces == NULL) { - zend_type_error("%s(): Argument #2 ($array) must be of type array, null given", get_active_function_name()); + zend_type_error( + "%s(): If argument #1 ($separator) is of type string, " + "argument #2 ($array) must be of type array, null given", + get_active_function_name() + ); RETURN_THROWS(); } diff --git a/ext/standard/tests/strings/implode.phpt b/ext/standard/tests/strings/implode_basic.phpt similarity index 100% rename from ext/standard/tests/strings/implode.phpt rename to ext/standard/tests/strings/implode_basic.phpt diff --git a/ext/standard/tests/strings/implode_error.phpt b/ext/standard/tests/strings/implode_error.phpt index af047d6c5ed28..c499989aff8de 100644 --- a/ext/standard/tests/strings/implode_error.phpt +++ b/ext/standard/tests/strings/implode_error.phpt @@ -8,13 +8,6 @@ try { echo $e->getMessage(), "\n"; } -/* int as pieces */ -try { - var_dump( implode("glue",1234) ); -} catch (TypeError $e) { - echo $e->getMessage(), "\n"; -} - /* NULL as pieces */ try { var_dump( implode("glue", NULL) ); @@ -22,13 +15,6 @@ try { echo $e->getMessage(), "\n"; } -/* pieces as NULL array */ -try { - var_dump( implode(",", array(NULL)) ); -} catch (TypeError $e) { - echo $e->getMessage(), "\n"; -} - /* integer as glue */ try { var_dump( implode(12, "pieces") ); @@ -45,9 +31,7 @@ try { ?> --EXPECTF-- implode(): Argument #2 ($array) must be of type array, null given -implode(): Argument #2 ($array) must be of type array, int given implode(): Argument #2 ($array) must be of type array, null given -string(0) "" implode(): Argument #2 ($array) must be of type array, string given Deprecated: implode(): Passing null to parameter #1 ($separator) of type array|string is deprecated in %s on line %d diff --git a/ext/standard/tests/strings/implode1.phpt b/ext/standard/tests/strings/implode_variation.phpt similarity index 97% rename from ext/standard/tests/strings/implode1.phpt rename to ext/standard/tests/strings/implode_variation.phpt index ee5a48a8e5052..1c5851f8de2ec 100644 --- a/ext/standard/tests/strings/implode1.phpt +++ b/ext/standard/tests/strings/implode_variation.phpt @@ -9,7 +9,8 @@ $arrays = array ( array(array(2),array(1)), array(false,true), array(), - array("a","aaaa","b","bbbb","c","ccccccccccccccccccccc") + array("a","aaaa","b","bbbb","c","ccccccccccccccccccccc"), + array(NULL), ); /* loop to output string with ', ' as $glue, using implode() */ foreach ($arrays as $array) { @@ -170,6 +171,11 @@ array(6) { [5]=> string(21) "ccccccccccccccccccccc" } +string(0) "" +array(1) { + [0]=> + NULL +} *** Testing implode() with variations of glue *** -- Iteration 1 -- From b56c486a1797f71ea598f0e74cdeae37e2262644 Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Thu, 16 Nov 2023 23:31:22 -0300 Subject: [PATCH 09/14] Removing deprecation test --- ext/standard/tests/strings/implode_error.phpt | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/ext/standard/tests/strings/implode_error.phpt b/ext/standard/tests/strings/implode_error.phpt index c499989aff8de..c383c4f062602 100644 --- a/ext/standard/tests/strings/implode_error.phpt +++ b/ext/standard/tests/strings/implode_error.phpt @@ -21,18 +21,7 @@ try { } catch (TypeError $e) { echo $e->getMessage(), "\n"; } - -/* NULL as glue */ -try { - var_dump( implode(NULL, "abcd") ); -} catch (TypeError $e) { - echo $e->getMessage(), "\n"; -} -?> --EXPECTF-- implode(): Argument #2 ($array) must be of type array, null given implode(): Argument #2 ($array) must be of type array, null given implode(): Argument #2 ($array) must be of type array, string given - -Deprecated: implode(): Passing null to parameter #1 ($separator) of type array|string is deprecated in %s on line %d -implode(): Argument #2 ($array) must be of type array, string given From 5ac343153fcbeb95199d43d5c523b3c8b3ce244d Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Fri, 17 Nov 2023 13:00:51 -0300 Subject: [PATCH 10/14] Fixing tests with the new error message --- ext/standard/tests/strings/implode_error.phpt | 2 +- ext/standard/tests/strings/implode_variation.phpt | 2 +- ext/standard/tests/strings/join_error.phpt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/standard/tests/strings/implode_error.phpt b/ext/standard/tests/strings/implode_error.phpt index c383c4f062602..ba29150b3d7af 100644 --- a/ext/standard/tests/strings/implode_error.phpt +++ b/ext/standard/tests/strings/implode_error.phpt @@ -22,6 +22,6 @@ try { echo $e->getMessage(), "\n"; } --EXPECTF-- -implode(): Argument #2 ($array) must be of type array, null given +implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given implode(): Argument #2 ($array) must be of type array, null given implode(): Argument #2 ($array) must be of type array, string given diff --git a/ext/standard/tests/strings/implode_variation.phpt b/ext/standard/tests/strings/implode_variation.phpt index 1c5851f8de2ec..52afdbbd24010 100644 --- a/ext/standard/tests/strings/implode_variation.phpt +++ b/ext/standard/tests/strings/implode_variation.phpt @@ -198,7 +198,7 @@ string(35) "2000-639010PHP000 0string%0with%0...%0" string(43) "2\00\0-639\01\0PHP\0\0\0 \0string%0with%0...%0" *** Testing implode() on empty string *** -implode(): Argument #2 ($array) must be of type array, null given +implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given *** Testing implode() on sub-arrays *** diff --git a/ext/standard/tests/strings/join_error.phpt b/ext/standard/tests/strings/join_error.phpt index 65ff0f40c72eb..0029bcc34b08a 100644 --- a/ext/standard/tests/strings/join_error.phpt +++ b/ext/standard/tests/strings/join_error.phpt @@ -20,5 +20,5 @@ echo "Done\n"; *** Testing join() : error conditions *** -- Testing join() with less than expected no. of arguments -- -join(): Argument #2 ($array) must be of type array, null given +join(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given Done From f5b351e017d7189220b5c377f388656a120b8d40 Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Mon, 20 Nov 2023 18:43:40 -0300 Subject: [PATCH 11/14] Using the new error message always with null --- ext/standard/string.c | 2 +- ext/standard/tests/strings/implode_error.phpt | 5 +- ext/standard/tests/strings/join_error1.phpt | 46 +++++++++---------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index 96e27569d10d0..00e2c0574af0c 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1030,7 +1030,7 @@ PHP_FUNCTION(implode) ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_ARRAY_HT_OR_STR(arg1_array, arg1_str) Z_PARAM_OPTIONAL - Z_PARAM_ARRAY_HT(pieces) + Z_PARAM_ARRAY_HT_OR_NULL(pieces) ZEND_PARSE_PARAMETERS_END(); if (arg1_str != NULL && pieces == NULL) { diff --git a/ext/standard/tests/strings/implode_error.phpt b/ext/standard/tests/strings/implode_error.phpt index ba29150b3d7af..a2b92b8076454 100644 --- a/ext/standard/tests/strings/implode_error.phpt +++ b/ext/standard/tests/strings/implode_error.phpt @@ -1,4 +1,5 @@ --TEST-- +Test implode() function: error conditions --FILE-- Date: Tue, 21 Nov 2023 14:16:35 -0300 Subject: [PATCH 12/14] Adding closing tag to test and removing unreacheable statement --- ext/standard/string.c | 15 +++++---------- ext/standard/tests/strings/implode_error.phpt | 1 + 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index 00e2c0574af0c..856f25ddd0c73 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1042,19 +1042,14 @@ PHP_FUNCTION(implode) RETURN_THROWS(); } - if (pieces == NULL) { - if (arg1_array == NULL) { - zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name()); - RETURN_THROWS(); - } + if (pieces != NULL && arg1_str == NULL) { + zend_argument_type_error(1, "must be of type string, array given"); + RETURN_THROWS(); + } + if (pieces == NULL) { arg1_str = ZSTR_EMPTY_ALLOC(); pieces = arg1_array; - } else { - if (arg1_str == NULL) { - zend_argument_type_error(1, "must be of type string, array given"); - RETURN_THROWS(); - } } php_implode(arg1_str, pieces, return_value); diff --git a/ext/standard/tests/strings/implode_error.phpt b/ext/standard/tests/strings/implode_error.phpt index a2b92b8076454..d2df89a2a15d8 100644 --- a/ext/standard/tests/strings/implode_error.phpt +++ b/ext/standard/tests/strings/implode_error.phpt @@ -22,6 +22,7 @@ try { } catch (TypeError $e) { echo $e->getMessage(), "\n"; } +?> --EXPECTF-- implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given From e86f567f5816776a3822a3eeb95bced04f0ff28a Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Wed, 22 Nov 2023 13:20:34 -0300 Subject: [PATCH 13/14] Reverting checks to original structure, changing only the error messages --- ext/standard/string.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index 856f25ddd0c73..dc88e375925ec 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1033,23 +1033,23 @@ PHP_FUNCTION(implode) Z_PARAM_ARRAY_HT_OR_NULL(pieces) ZEND_PARSE_PARAMETERS_END(); - if (arg1_str != NULL && pieces == NULL) { - zend_type_error( - "%s(): If argument #1 ($separator) is of type string, " - "argument #2 ($array) must be of type array, null given", - get_active_function_name() - ); - RETURN_THROWS(); - } - - if (pieces != NULL && arg1_str == NULL) { - zend_argument_type_error(1, "must be of type string, array given"); - RETURN_THROWS(); - } - if (pieces == NULL) { + if (arg1_array == NULL) { + zend_type_error( + "%s(): If argument #1 ($separator) is of type string, " + "argument #2 ($array) must be of type array, null given", + get_active_function_name() + ); + RETURN_THROWS(); + } + arg1_str = ZSTR_EMPTY_ALLOC(); pieces = arg1_array; + } else { + if (arg1_str == NULL) { + zend_argument_type_error(1, "must be of type string, array given"); + RETURN_THROWS(); + } } php_implode(arg1_str, pieces, return_value); From 95b8db9262c19858c49fe1a0a83e8f033baf0e74 Mon Sep 17 00:00:00 2001 From: Vinicius Dias Date: Wed, 22 Nov 2023 17:26:18 -0300 Subject: [PATCH 14/14] Making tests PSR-12 compliant --- ext/standard/tests/strings/implode_error.phpt | 8 ++++---- ext/standard/tests/strings/implode_variation.phpt | 14 +++++++------- ext/standard/tests/strings/join_error1.phpt | 3 +-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/ext/standard/tests/strings/implode_error.phpt b/ext/standard/tests/strings/implode_error.phpt index d2df89a2a15d8..417b3591e0dd2 100644 --- a/ext/standard/tests/strings/implode_error.phpt +++ b/ext/standard/tests/strings/implode_error.phpt @@ -4,26 +4,26 @@ Test implode() function: error conditions getMessage(), "\n"; } /* NULL as pieces */ try { - var_dump( implode("glue", NULL) ); + var_dump(implode("glue", NULL)); } catch (TypeError $e) { echo $e->getMessage(), "\n"; } /* integer as glue */ try { - var_dump( implode(12, "pieces") ); + var_dump(implode(12, "pieces")); } catch (TypeError $e) { echo $e->getMessage(), "\n"; } ?> ---EXPECTF-- +--EXPECT-- implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given implode(): Argument #2 ($array) must be of type ?array, string given diff --git a/ext/standard/tests/strings/implode_variation.phpt b/ext/standard/tests/strings/implode_variation.phpt index 52afdbbd24010..2cc0870f39f4f 100644 --- a/ext/standard/tests/strings/implode_variation.phpt +++ b/ext/standard/tests/strings/implode_variation.phpt @@ -3,7 +3,7 @@ Test implode() function --FILE-- getMessage() . "\n"; } try { - var_dump( implode(2, $sub_array) ); + var_dump(implode(2, $sub_array)); } catch (TypeError $exception) { echo $exception->getMessage() . "\n"; } @@ -91,7 +91,7 @@ $obj = new foo(); //creating new object $arr = array(); $arr[0] = &$obj; $arr[1] = &$obj; -var_dump( implode(",", $arr) ); +var_dump(implode(",", $arr)); var_dump($arr); /* Checking on resource types */ @@ -105,7 +105,7 @@ $dir_handle = opendir( __DIR__ ); /* store resources in array for comparison */ $resources = array($file_handle, $dir_handle); -var_dump( implode("::", $resources) ); +var_dump(implode("::", $resources)); /* closing resource handles */ fclose($file_handle); diff --git a/ext/standard/tests/strings/join_error1.phpt b/ext/standard/tests/strings/join_error1.phpt index 1507af652cb31..0bb451a73d98d 100644 --- a/ext/standard/tests/strings/join_error1.phpt +++ b/ext/standard/tests/strings/join_error1.phpt @@ -29,7 +29,6 @@ class test // array with different values $values = array ( - // integer values 0, 1, @@ -84,7 +83,7 @@ for($index = 0; $index < count($values); $index ++) { $pieces = $values [$index]; try { - var_dump( join($glue, $pieces) ); + var_dump(join($glue, $pieces)); } catch (TypeError $e) { echo $e->getMessage(), "\n"; }