Make WordPress Core

Changeset 48981


Ignore:
Timestamp:
09/16/2020 02:27:42 AM (4 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/wp-db.php

    r48980 r48981  
    13701370        $placeholders = preg_match_all( "/(^|[^%]|(%%)+)%($allowed_format)?[sdF]/", $query, $matches );
    13711371
    1372         if ( count( $args ) !== $placeholders ) {
     1372        $args_count = count( $args );
     1373
     1374        if ( $args_count !== $placeholders ) {
    13731375            if ( 1 === $placeholders && $passed_as_array ) {
    13741376                // If the passed query only expected one argument, but the wrong number of arguments were sent as an array, bail.
     
    13931395                        __( 'The query does not contain the correct number of placeholders (%1$d) for the number of arguments passed (%2$d).' ),
    13941396                        $placeholders,
    1395                         count( $args )
     1397                        $args_count
    13961398                    ),
    13971399                    '4.8.3'
    13981400                );
     1401
     1402                /*
     1403                 * If we don't have enough arguments to match the placeholders,
     1404                 * return an empty string to avoid a fatal error on PHP 8.
     1405                 */
     1406                if ( $args_count < $placeholders ) {
     1407                    $max_numbered_placeholder = ! empty( $matches[3] ) ? max( array_map( 'intval', $matches[3] ) ) : 0;
     1408
     1409                    if ( ! $max_numbered_placeholder || $args_count < $max_numbered_placeholder ) {
     1410                        return '';
     1411                    }
     1412                }
    13991413            }
    14001414        }
Note: See TracChangeset for help on using the changeset viewer.