From c77124056a79834f1c7cb0ae831d41101f56d9de Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Wed, 22 Jan 2020 01:55:12 +0100 Subject: [PATCH] Add ValueError for invalid mode in count() --- ext/standard/array.c | 5 +++ .../tests/array/count_invalid_mode.phpt | 37 +++++++++++++++++++ ext/standard/tests/array/count_recursive.phpt | 28 ++++---------- 3 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 ext/standard/tests/array/count_invalid_mode.phpt diff --git a/ext/standard/array.c b/ext/standard/array.c index 3624881c4c7a..9e0af6f07c00 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -764,6 +764,11 @@ PHP_FUNCTION(count) Z_PARAM_LONG(mode) ZEND_PARSE_PARAMETERS_END(); + if (mode != COUNT_NORMAL && mode != COUNT_RECURSIVE) { + zend_value_error("Mode value is invalid"); + RETURN_THROWS(); + } + switch (Z_TYPE_P(array)) { case IS_NULL: /* Intentionally not converted to an exception */ diff --git a/ext/standard/tests/array/count_invalid_mode.phpt b/ext/standard/tests/array/count_invalid_mode.phpt new file mode 100644 index 000000000000..6e302fd0ce9a --- /dev/null +++ b/ext/standard/tests/array/count_invalid_mode.phpt @@ -0,0 +1,37 @@ +--TEST-- +Test count() function : invalid modes in weak type mode +--FILE-- +getMessage() . \PHP_EOL; + } +} +?> +--EXPECT-- +int(0) +int(0) +int(0) +int(0) +Mode value is invalid +Mode value is invalid +Mode value is invalid +int(0) +int(0) +int(0) diff --git a/ext/standard/tests/array/count_recursive.phpt b/ext/standard/tests/array/count_recursive.phpt index 501b2cf598d0..dd5c2342fca9 100644 --- a/ext/standard/tests/array/count_recursive.phpt +++ b/ext/standard/tests/array/count_recursive.phpt @@ -104,14 +104,10 @@ echo "\n-- Testing count() on arrays containing references --\n"; $arr = array(1, array("a", "b", "c")); $arr[2] = &$arr[1]; -$mode_arr = array( COUNT_NORMAL, COUNT_RECURSIVE, 0, 1, -1, -1.45, 2, TRUE, - FALSE, NULL); -for( $i =0; $i < count( $mode_arr ); $i++) { - echo "For mode '$mode_arr[$i]' count is => "; - var_dump(count($arr, $mode_arr[$i])); -} - -echo "\nDone"; +echo "Count normal" . \PHP_EOL; +var_dump(count($arr, COUNT_NORMAL)); +echo "Count recursive" . \PHP_EOL; +var_dump(count($arr, COUNT_RECURSIVE)); /* closing the resource handles */ fclose( $resource1 ); @@ -209,15 +205,7 @@ COUNT_NORMAL: should be 3, is 3 int(2) -- Testing count() on arrays containing references -- -For mode '0' count is => int(3) -For mode '1' count is => int(9) -For mode '0' count is => int(3) -For mode '1' count is => int(9) -For mode '-1' count is => int(3) -For mode '-1.45' count is => int(3) -For mode '2' count is => int(3) -For mode '1' count is => int(9) -For mode '' count is => int(3) -For mode '' count is => int(3) - -Done +Count normal +int(3) +Count recursive +int(9)