Hello Cerber Team,
Bug Report: Issue with crb_array_diff_keys
in WP Cerber and User Role Editor
Error Encountered:
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, bool given in /wp-content/plugins/wp-cerber/cerber-common.php on line 1124
#### Environment:
* **WordPress Version:** 6.7.1
* **WP Cerber Version:** 9.6.5
* **User Role Editor Version:** 4.64.4
* **PHP Version:** 8.1
#### Details:
The error seems to originate from the `crb_array_diff_keys` function in the file `/wp-content/plugins/wp-cerber/cerber-common.php` . Here is the original code:
/**
-
Compare two arrays by using keys: check if two arrays have a different set of keys
-
@param $arr1 array
-
@param $arr2 array
-
@return bool true if arrays have different set of keys
*/
function crb_array_diff_keys( &$arr1, &$arr2 ) {
if ( count( $arr1 ) != count( $arr2 ) ) {
return true;
}
if ( array_diff_key( $arr1, $arr2 ) ) {
return true;
}
if ( array_diff_key( $arr2, $arr1 ) ) {
return true;
}return false;
}
The issue occurs when the function receives a variable `$arr1` or `$arr2` that is neither an array nor an instance of `Countable` . In such cases, the call to `count()` triggers a fatal error.
#### Suggested Fix:
To resolve this issue, I modified the `crb_array_diff_keys` function to include validation of the arguments before using `count()` . Here is the updated function:
/**
-
Compare two arrays by using keys: check if two arrays have a different set of keys
-
@param mixed $arr1 Array or Countable
-
@param mixed $arr2 Array or Countable
-
@return bool True if arrays have a different set of keys
*/
function crb_array_diff_keys( &$arr1, &$arr2 ) {
if ( !is_array( $arr1 ) || !is_array( $arr2 ) ) {
return true; // Return true if either parameter is not an array
}if ( count( $arr1 ) != count( $arr2 ) ) {
return true;
}if ( array_diff_key( $arr1, $arr2 ) ) {
return true;
}if ( array_diff_key( $arr2, $arr1 ) ) {
return true;
}return false;
}
#### Result:
* This change ensures the function does not attempt to count a variable that is not an array or an instance of `Countable` .
* It prevents the fatal error while maintaining the intended functionality.
#### Feedback and Inquiry:
While this fix works for my environment, Iâm unsure if it is universally applicable to other parts of WP Cerberâs codebase. Iâm submitting this bug report and solution here in case it is helpful for future updates or for others encountering a similar issue.
If you need further details or testing, Iâm happy to assist.
Thank you for your time and support!
Best regards,
Ovnis đ¸đ