From b69cc58b7fda191872ebbc315ea635983d710c48 Mon Sep 17 00:00:00 2001 From: Tyler Anthony Romeo Date: Mon, 3 Sep 2012 23:55:18 -0400 Subject: [PATCH] (bug 39287) Fixed error checking in DatabaseMysql::fetchObject. According to the libmysql API documentation, mysql_fetch_row, upon which the PHP functions mysql_fetch_object, etc. are based on, does not reset the error number or message upon success. So calling mysql_errno() after a successful fetch may return the error of the previous function call. This commit changes DatabaseMysql::fetchObject and similar functions to only check for specific error numbers that are explicitly documented to be expected from mysql_fetch_*. Change-Id: Iacb78828885a8a0d4e499a681d938a6adf651582 --- includes/db/DatabaseMysql.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/includes/db/DatabaseMysql.php b/includes/db/DatabaseMysql.php index faa09ada7b..7f389da904 100644 --- a/includes/db/DatabaseMysql.php +++ b/includes/db/DatabaseMysql.php @@ -203,7 +203,13 @@ class DatabaseMysql extends DatabaseBase { wfSuppressWarnings(); $row = mysql_fetch_object( $res ); wfRestoreWarnings(); - if( $this->lastErrno() ) { + + $errno = $this->lastErrno(); + // Unfortunately, mysql_fetch_object does not reset the last errno. + // Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as + // these are the only errors mysql_fetch_object can cause. + // See http://dev.mysql.com/doc/refman/5.0/es/mysql-fetch-row.html. + if( $errno == 2000 || $errno == 2013 ) { throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() ) ); } return $row; @@ -221,7 +227,13 @@ class DatabaseMysql extends DatabaseBase { wfSuppressWarnings(); $row = mysql_fetch_array( $res ); wfRestoreWarnings(); - if ( $this->lastErrno() ) { + + $errno = $this->lastErrno(); + // Unfortunately, mysql_fetch_array does not reset the last errno. + // Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as + // these are the only errors mysql_fetch_object can cause. + // See http://dev.mysql.com/doc/refman/5.0/es/mysql-fetch-row.html. + if( $errno == 2000 || $errno == 2013 ) { throw new DBUnexpectedError( $this, 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() ) ); } return $row; -- 2.20.1