From eef2e5c85c2e5e1cd7cce1671bce1b2e312afe9d Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 16 Sep 2020 02:29:06 +0000 Subject: [PATCH] Code Modernization: Return an empty string from `wpdb::prepare()` if there are not enough arguments to match the placeholders. This avoids a fatal error on PHP 8 caused by passing mismatched arguments to `vsprintf()`, and maintains the current behaviour. Follow-up to [48979], [48980]. See #50913, #50639. Built from https://develop.svn.wordpress.org/trunk@48981 git-svn-id: http://core.svn.wordpress.org/trunk@48743 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/version.php | 2 +- wp-includes/wp-db.php | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/wp-includes/version.php b/wp-includes/version.php index 7289f20059..9cc4371360 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.6-alpha-48980'; +$wp_version = '5.6-alpha-48981'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index 9761de99a8..ebcadb6ef0 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -1369,7 +1369,9 @@ class wpdb { // Count the number of valid placeholders in the query. $placeholders = preg_match_all( "/(^|[^%]|(%%)+)%($allowed_format)?[sdF]/", $query, $matches ); - if ( count( $args ) !== $placeholders ) { + $args_count = count( $args ); + + if ( $args_count !== $placeholders ) { if ( 1 === $placeholders && $passed_as_array ) { // If the passed query only expected one argument, but the wrong number of arguments were sent as an array, bail. wp_load_translations_early(); @@ -1392,10 +1394,22 @@ class wpdb { /* translators: 1: Number of placeholders, 2: Number of arguments passed. */ __( 'The query does not contain the correct number of placeholders (%1$d) for the number of arguments passed (%2$d).' ), $placeholders, - count( $args ) + $args_count ), '4.8.3' ); + + /* + * If we don't have enough arguments to match the placeholders, + * return an empty string to avoid a fatal error on PHP 8. + */ + if ( $args_count < $placeholders ) { + $max_numbered_placeholder = ! empty( $matches[3] ) ? max( array_map( 'intval', $matches[3] ) ) : 0; + + if ( ! $max_numbered_placeholder || $args_count < $max_numbered_placeholder ) { + return ''; + } + } } }