php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50728 All PDOExceptions hardcode 'code' property to 0
Submitted: 2010-01-12 03:36 UTC Modified: 2010-01-12 12:47 UTC
From: joey@php.net Assigned:
Status: Closed Package: PDO related
PHP Version: 5.3.2RC1 OS: All
Private report: No CVE-ID: None
 [2010-01-12 03:36 UTC] joey@php.net
Description:
------------
From user report in ##PHP/freenode:

Each of the PDO drivers explicitly sets the 'code' property of their PDOException to 0, instead of passing along the driver-specific error code. I will attach is a set of proposed patches (against HEAD and 5.3.2 branch as of 2009-01-11) that I believe would fix this for each driver (although I'm guessing on the dblib - it used a significantly different model by storing the error state in DBLIB_G).

The example is from the sqlite driver - you can see that an error #14 gets stored in the 'message', but doesn't get passed to the 'code'.

Reproduce code:
---------------
try { 
    $a = new PDO("sqlite:/this/path/should/not/exist.db"); 
} catch (PDOException $e) {
    var_dump($e->getCode());
}

Expected result:
----------------
int(14)


Actual result:
--------------
int(0)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-12 03:37 UTC] joey@php.net
Index: ext/pdo_oci/oci_driver.c
===================================================================
--- ext/pdo_oci/oci_driver.c  (revision 292727)
+++ ext/pdo_oci/oci_driver.c  (working copy)
@@ -173,7 +173,7 @@

   /* little mini hack so that we can use this code from the dbh ctor */
   if (!dbh->methods) {
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s]: %s", *pdo_err, einfo->errmsg);
+     zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode TSRMLS_CC, "SQLSTATE[%s]: %s", *pdo_err, einfo->errmsg);
   }

   return einfo->errcode;
Index: ext/pdo_dblib/dblib_driver.c
===================================================================
--- ext/pdo_dblib/dblib_driver.c (revision 292727)
+++ ext/pdo_dblib/dblib_driver.c (working copy)
@@ -255,7 +255,7 @@
   dbh->driver_data = H;

   if (!ret) {
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC,
+     zend_throw_exception_ex(php_pdo_get_exception(), DBLIB_G(err).dberr TSRMLS_CC,
         "SQLSTATE[%s] %s (severity %d)",
         DBLIB_G(err).sqlstate,
         DBLIB_G(err).dberrstr,
Index: ext/pdo_sqlite/sqlite_driver.c
===================================================================
--- ext/pdo_sqlite/sqlite_driver.c  (revision 292727)
+++ ext/pdo_sqlite/sqlite_driver.c  (working copy)
@@ -78,7 +78,7 @@
   }

   if (!dbh->methods) {
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
+     zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
            *pdo_err, einfo->errcode, einfo->errmsg);
   }

Index: ext/pdo_mysql/mysql_driver.c
===================================================================
--- ext/pdo_mysql/mysql_driver.c (revision 292727)
+++ ext/pdo_mysql/mysql_driver.c (working copy)
@@ -127,7 +127,7 @@

   if (!dbh->methods) {
      PDO_DBG_INF("Throwing exception");
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
+     zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
            *pdo_err, einfo->errcode, einfo->errmsg);
   }

Index: ext/pdo_firebird/firebird_driver.c
===================================================================
--- ext/pdo_firebird/firebird_driver.c (revision 292727)
+++ ext/pdo_firebird/firebird_driver.c (working copy)
@@ -691,7 +691,7 @@
      char errmsg[512];
      ISC_STATUS *s = H->isc_status;
      isc_interprete(errmsg, &s);
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
+     zend_throw_exception_ex(php_pdo_get_exception(), H->isc_status[1] TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
            "HY000", H->isc_status[1], errmsg);
   }

Index: ext/pdo_pgsql/pgsql_driver.c
===================================================================
--- ext/pdo_pgsql/pgsql_driver.c (revision 292727)
+++ ext/pdo_pgsql/pgsql_driver.c (working copy)
@@ -87,7 +87,7 @@
   }

   if (!dbh->methods) {
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
+     zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
            *pdo_err, einfo->errcode, einfo->errmsg);
   }

Index: ext/pdo_odbc/odbc_driver.c
===================================================================
--- ext/pdo_odbc/odbc_driver.c   (revision 292727)
+++ ext/pdo_odbc/odbc_driver.c   (working copy)
@@ -104,7 +104,7 @@
   strcpy(*pdo_err, einfo->last_state);
 /* printf("@@ SQLSTATE[%s] %s\n", *pdo_err, einfo->last_err_msg); */
   if (!dbh->methods) {
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] %s: %d %s",
+     zend_throw_exception_ex(php_pdo_get_exception(), einfo->last_error TSRMLS_CC, "SQLSTATE[%s] %s: %d %s",
            *pdo_err, what, einfo->last_error, einfo->last_err_msg);
   }


 [2010-01-12 03:40 UTC] joey@php.net
Patch for HEAD (previous was for tip of 5.3.2 [svn 292727]):

Index: ext/pdo_oci/oci_driver.c
===================================================================
--- ext/pdo_oci/oci_driver.c  (revision 293440)
+++ ext/pdo_oci/oci_driver.c  (working copy)
@@ -173,7 +173,7 @@

   /* little mini hack so that we can use this code from the dbh ctor */
   if (!dbh->methods) {
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s]: %s", *pdo_err, einfo->errmsg);
+     zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode TSRMLS_CC, "SQLSTATE[%s]: %s", *pdo_err, einfo->errmsg);
   }

   return einfo->errcode;
Index: ext/pdo_dblib/dblib_driver.c
===================================================================
--- ext/pdo_dblib/dblib_driver.c (revision 293440)
+++ ext/pdo_dblib/dblib_driver.c (working copy)
@@ -255,7 +255,7 @@
   dbh->driver_data = H;

   if (!ret) {
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC,
+     zend_throw_exception_ex(php_pdo_get_exception(), DBLIB_G(err).dberr TSRMLS_CC,
         "SQLSTATE[%s] %s (severity %d)",
         DBLIB_G(err).sqlstate,
         DBLIB_G(err).dberrstr,
Index: ext/pdo_sqlite/sqlite_driver.c
===================================================================
--- ext/pdo_sqlite/sqlite_driver.c  (revision 293440)
+++ ext/pdo_sqlite/sqlite_driver.c  (working copy)
@@ -102,7 +102,7 @@
   }

   if (!dbh->methods) {
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
+     zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
            *pdo_err, einfo->errcode, einfo->errmsg);
   }

Index: ext/pdo_mysql/mysql_driver.c
===================================================================
--- ext/pdo_mysql/mysql_driver.c (revision 293440)
+++ ext/pdo_mysql/mysql_driver.c (working copy)
@@ -127,7 +127,7 @@

   if (!dbh->methods) {
      PDO_DBG_INF("Throwing exception");
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
+     zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
            *pdo_err, einfo->errcode, einfo->errmsg);
   }

Index: ext/pdo_firebird/firebird_driver.c
===================================================================
--- ext/pdo_firebird/firebird_driver.c (revision 293440)
+++ ext/pdo_firebird/firebird_driver.c (working copy)
@@ -686,7 +686,7 @@
      char errmsg[512];
      ISC_STATUS *s = H->isc_status;
      isc_interprete(errmsg, &s);
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
+     zend_throw_exception_ex(php_pdo_get_exception(), H->isc_status[1] TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
            "HY000", H->isc_status[1], errmsg);
   }

Index: ext/pdo_pgsql/pgsql_driver.c
===================================================================
--- ext/pdo_pgsql/pgsql_driver.c (revision 293440)
+++ ext/pdo_pgsql/pgsql_driver.c (working copy)
@@ -87,7 +87,7 @@
   }

   if (!dbh->methods) {
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
+     zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
            *pdo_err, einfo->errcode, einfo->errmsg);
   }

Index: ext/pdo_odbc/odbc_driver.c
===================================================================
--- ext/pdo_odbc/odbc_driver.c   (revision 293440)
+++ ext/pdo_odbc/odbc_driver.c   (working copy)
@@ -88,10 +88,10 @@
 /* printf("@@ SQLSTATE[%s] %s\n", *pdo_err, einfo->last_err_msg); */
   if (!dbh->methods) {
 #if PHP_VERSION_ID > 50200
-     zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] %s: %d %s",
+     zend_throw_exception_ex(php_pdo_get_exception(), einfo->last_error TSRMLS_CC, "SQLSTATE[%s] %s: %d %s",
            *pdo_err, what, einfo->last_error, einfo->last_err_msg);
 #else
-     zend_throw_exception_ex(php_pdo_get_exception(TSRMLS_C), 0 TSRMLS_CC, "SQLSTATE[%s] %s: %d %s",
+     zend_throw_exception_ex(php_pdo_get_exception(TSRMLS_C), einfo->last_error TSRMLS_CC, "SQLSTATE[%s] %s: %d %s",
            *pdo_err, what, einfo->last_error, einfo->last_err_msg);
 #endif
   }

 [2010-01-12 03:56 UTC] joey@php.net
Proposed ext/pdo_sqlite/tests/bug50728.phpt:
--TEST--
Bug #50728 (All PDOExceptions hardcode 'code' property to 0) (crash on PDO::FETCH_CLASS + __set())
--SKIPIF--
<?php
if (!extension_loaded('pdo_sqlite')) print 'skip not loaded';
?>
--FILE--
<?php
 try {
     $a = new PDO("sqlite:/this/path/should/not/exist.db");
 } catch (PDOException $e) {
     var_dump($e->getCode());
 }

?>
--EXPECTF--
int(14)

 [2010-01-12 12:46 UTC] svn@php.net
Automatic comment from SVN on behalf of iliaa
Revision: http://svn.php.net/viewvc/?view=revision&revision=293447
Log: Fixed bug #50728 (All PDOExceptions hardcode 'code' property to 0)
 [2010-01-12 12:47 UTC] iliaa@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2010-02-03 19:48 UTC] svn@php.net
Automatic comment from SVN on behalf of pajoye
Revision: http://svn.php.net/viewvc/?view=revision&revision=294444
Log: - Fixed bug #50728 (All PDOExceptions hardcode 'code' property to 0)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 11:01:29 2024 UTC