Skip to content

Commit

Permalink
PHP 8.1 | wpdb::_real_escape(): fix "passing null to non-nullable" de…
Browse files Browse the repository at this point in the history
…precation notices

The PHP native `mysqli_real_escape_string()` function expects to be passed a string as the second parameter and this is not a nullable parameter.
Passing `null` to it will result in a `mysqli_real_escape_string(): Passing null to parameter #2 ($string) of type string is deprecated` notice on PHP 8.1.

Previously, an input type check was put in place to prevent fatal errors on PHP 8.0 when an array, object or resource was passed. Changeset [48980].

A `null` value was explicitly excluded from that check, even though a `null` value being passed would only ever result in an empty string anyway.

This commit changes the previous input type check to also bow out early for `null` values and to automatically return an empty string for those.

Refs:
* https://www.php.net/manual/en/mysqli.real-escape-string.php
* https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg
  • Loading branch information
jrfnl committed Aug 27, 2021
1 parent 46f57cf commit 8cf0fd4
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/wp-includes/wp-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ function _weak_escape( $string ) {
* @return string Escaped string.
*/
function _real_escape( $string ) {
if ( ! is_scalar( $string ) && ! is_null( $string ) ) {
if ( ! is_scalar( $string ) ) {
return '';
}

Expand Down

0 comments on commit 8cf0fd4

Please sign in to comment.