From 945ba795dc69638d9c2ee8ce1d527022c779f66d Mon Sep 17 00:00:00 2001 From: desrosj Date: Mon, 17 Dec 2018 18:39:52 +0000 Subject: [PATCH] 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 preceding query have failed. Props spacedmonkey, desrosj, pento. Merges [43934] into trunk. See #45299. Built from https://develop.svn.wordpress.org/trunk@44272 git-svn-id: http://core.svn.wordpress.org/trunk@44102 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/version.php | 2 +- wp-includes/wp-db.php | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/wp-includes/version.php b/wp-includes/version.php index 94b2044f54..7056244a85 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.1-alpha-44271'; +$wp_version = '5.1-alpha-44272'; /** * 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 ae08158f64..af42aaf841 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -2544,8 +2544,10 @@ class wpdb { $new_array = array(); // Extract the column values - for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) { - $new_array[ $i ] = $this->get_var( null, $x, $i ); + if ( $this->last_result ) { + for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) { + $new_array[ $i ] = $this->get_var( null, $x, $i ); + } } return $new_array; } @@ -2585,11 +2587,13 @@ class wpdb { } elseif ( $output == OBJECT_K ) { // Return an array of row objects with keys from column 1 // (Duplicates are discarded) - foreach ( $this->last_result as $row ) { - $var_by_ref = get_object_vars( $row ); - $key = array_shift( $var_by_ref ); - if ( ! isset( $new_array[ $key ] ) ) { - $new_array[ $key ] = $row; + if ( $this->last_result ) { + foreach ( $this->last_result as $row ) { + $var_by_ref = get_object_vars( $row ); + $key = array_shift( $var_by_ref ); + if ( ! isset( $new_array[ $key ] ) ) { + $new_array[ $key ] = $row; + } } } return $new_array;