(bug 39287) Fixed error checking in DatabaseMysql::fetchObject.
authorTyler Anthony Romeo <tylerromeo@gmail.com>
Tue, 4 Sep 2012 03:55:18 +0000 (23:55 -0400)
committerAaron <aschulz@wikimedia.org>
Tue, 4 Sep 2012 18:41:58 +0000 (11:41 -0700)
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

index faa09ad..7f389da 100644 (file)
@@ -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;