Skip to content

Add ValueError for invalid mode in count() #5106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't realize this is not pre-existing. Still fine though :)


switch (Z_TYPE_P(array)) {
case IS_NULL:
/* Intentionally not converted to an exception */
Expand Down
37 changes: 37 additions & 0 deletions ext/standard/tests/array/count_invalid_mode.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Test count() function : invalid modes in weak type mode
--FILE--
<?php

$modes = [
COUNT_NORMAL,
COUNT_RECURSIVE,
0,
1,
-1,
-1.45,
2,
TRUE,
FALSE,
NULL,
];

foreach ($modes as $mode) {
try {
var_dump(count([], $mode));
} catch (\ValueError $error) {
echo $error->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)
28 changes: 8 additions & 20 deletions ext/standard/tests/array/count_recursive.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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)