Make WordPress Core


Ignore:
Timestamp:
11/22/2018 03:58:19 AM (8 years ago)
Author:
pento
Message:

WPDB: Check that $wpdb->last_result is countable before counting with it.

wpdb::get_col() iterates over $wpdb->last_result, which can be a non-countable value, should the preceeding query have failed.

Props spacedmonkey, desrosj.
See #45299.

File:
1 edited

Legend:

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

    r42550 r43934  
    24572457                $new_array = array();
    24582458                // Extract the column values
    2459                 for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
    2460                         $new_array[$i] = $this->get_var( null, $x, $i );
     2459                if ( $this->last_result ) {
     2460                        for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
     2461                                $new_array[ $i ] = $this->get_var( null, $x, $i );
     2462                        }
    24612463                }
    24622464                return $new_array;
     
    24982500                        // Return an array of row objects with keys from column 1
    24992501                        // (Duplicates are discarded)
    2500                         foreach ( $this->last_result as $row ) {
    2501                                 $var_by_ref = get_object_vars( $row );
    2502                                 $key = array_shift( $var_by_ref );
    2503                                 if ( ! isset( $new_array[ $key ] ) )
    2504                                         $new_array[ $key ] = $row;
     2502                        if ( $this->last_result ) {
     2503                                foreach ( $this->last_result as $row ) {
     2504                                        $var_by_ref = get_object_vars( $row );
     2505                                        $key = array_shift( $var_by_ref );
     2506                                        if ( ! isset( $new_array[ $key ] ) ) {
     2507                                                $new_array[ $key ] = $row;
     2508                                        }
     2509                                }
    25052510                        }
    25062511                        return $new_array;
Note: See TracChangeset for help on using the changeset viewer.