From cf1e2f1b6308e7ae83b52411309435d0f9e52080 Mon Sep 17 00:00:00 2001 From: Ahmed Abdou Date: Sun, 17 Feb 2019 22:59:00 +0100 Subject: [PATCH 1/5] Fix #64705 errorInfo property of PDOException is null when PDO::__construct() fails Pdo drivers constructors are throwing PdoException wtihout setting errorInfo, so creating new reusable function that throw exceptions for pdo and will set the errorInfo also, and using this function with pdo_mysql,pdo_sqlite,pdo_pgsql. Author: Ahmed Abdou --- ext/pdo/pdo_dbh.c | 24 ++++++++++++++++++++++++ ext/pdo/php_pdo_driver.h | 1 + ext/pdo_mysql/mysql_driver.c | 3 +-- ext/pdo_mysql/tests/bug_64705.phpt | 17 +++++++++++++++++ ext/pdo_pgsql/pgsql_driver.c | 3 +-- ext/pdo_pgsql/tests/bug_64705.phpt | 17 +++++++++++++++++ ext/pdo_sqlite/sqlite_driver.c | 3 +-- ext/pdo_sqlite/tests/bug_64705.phpt | 17 +++++++++++++++++ 8 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 ext/pdo_mysql/tests/bug_64705.phpt create mode 100644 ext/pdo_pgsql/tests/bug_64705.phpt create mode 100644 ext/pdo_sqlite/tests/bug_64705.phpt diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index 314c845ac43a8..da2a846e6813d 100644 --- a/ext/pdo/pdo_dbh.c +++ b/ext/pdo/pdo_dbh.c @@ -38,6 +38,30 @@ static int pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value); +void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_error_type *pdo_error) +{ + zval error_info,pdo_exception; + char *pdo_exception_message; + object_init_ex(&pdo_exception, php_pdo_get_exception()); + array_init(&error_info); + + add_next_index_string(&error_info, *pdo_error); + add_next_index_long(&error_info, driver_errcode); + add_next_index_string(&error_info, driver_errmsg); + + spprintf(&pdo_exception_message, 0,"SQLSTATE[%s] [%d] %s",*pdo_error, driver_errcode, driver_errmsg); + zend_update_property(php_pdo_get_exception(), &pdo_exception, "errorInfo", sizeof("errorInfo")-1, &error_info); + zend_update_property_long(php_pdo_get_exception(), &pdo_exception, "code", sizeof("code")-1, driver_errcode); + zend_update_property_string( + php_pdo_get_exception(), + &pdo_exception, + "message", + sizeof("message")-1, + pdo_exception_message + ); + zend_throw_exception_object(&pdo_exception); +} + void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *sqlstate, const char *supp) /* {{{ */ { pdo_error_type *pdo_err = &dbh->error_code; diff --git a/ext/pdo/php_pdo_driver.h b/ext/pdo/php_pdo_driver.h index 5aec24de03d41..a4a96364ebfaa 100644 --- a/ext/pdo/php_pdo_driver.h +++ b/ext/pdo/php_pdo_driver.h @@ -693,6 +693,7 @@ PDO_API void php_pdo_dbh_delref(pdo_dbh_t *dbh); PDO_API void php_pdo_free_statement(pdo_stmt_t *stmt); +PDO_API void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_error_type *pdo_error); #endif /* PHP_PDO_DRIVER_H */ /* * Local variables: diff --git a/ext/pdo_mysql/mysql_driver.c b/ext/pdo_mysql/mysql_driver.c index 07aaa6f6c1fb3..912d531edb6b4 100644 --- a/ext/pdo_mysql/mysql_driver.c +++ b/ext/pdo_mysql/mysql_driver.c @@ -105,8 +105,7 @@ int _pdo_mysql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int lin if (!dbh->methods) { PDO_DBG_INF("Throwing exception"); - zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s] [%d] %s", - *pdo_err, einfo->errcode, einfo->errmsg); + pdo_throw_exception(einfo->errcode, einfo->errmsg, pdo_err); } PDO_DBG_RETURN(einfo->errcode); diff --git a/ext/pdo_mysql/tests/bug_64705.phpt b/ext/pdo_mysql/tests/bug_64705.phpt new file mode 100644 index 0000000000000..53e79757df20c --- /dev/null +++ b/ext/pdo_mysql/tests/bug_64705.phpt @@ -0,0 +1,17 @@ +--TEST-- +Bug #64705 errorInfo property of PDOException is null when PDO::__construct() fails +--SKIPIF-- + +--FILE-- +errorInfo) && is_array($e->errorInfo)); +} +?> +--EXPECTF-- +bool(true) \ No newline at end of file diff --git a/ext/pdo_pgsql/pgsql_driver.c b/ext/pdo_pgsql/pgsql_driver.c index ebe83140a3709..6603bdef9afbf 100644 --- a/ext/pdo_pgsql/pgsql_driver.c +++ b/ext/pdo_pgsql/pgsql_driver.c @@ -105,8 +105,7 @@ int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char * } if (!dbh->methods) { - zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s] [%d] %s", - *pdo_err, einfo->errcode, einfo->errmsg); + pdo_throw_exception(einfo->errcode, einfo->errmsg, pdo_err); } return errcode; diff --git a/ext/pdo_pgsql/tests/bug_64705.phpt b/ext/pdo_pgsql/tests/bug_64705.phpt new file mode 100644 index 0000000000000..464b4d0769a22 --- /dev/null +++ b/ext/pdo_pgsql/tests/bug_64705.phpt @@ -0,0 +1,17 @@ +--TEST-- +Bug #64705 errorInfo property of PDOException is null when PDO::__construct() fails +--SKIPIF-- + +--FILE-- +errorInfo) && is_array($e->errorInfo)); +} +?> +--EXPECTF-- +bool(true) \ No newline at end of file diff --git a/ext/pdo_sqlite/sqlite_driver.c b/ext/pdo_sqlite/sqlite_driver.c index 2bf452a88a819..81aa16f039754 100644 --- a/ext/pdo_sqlite/sqlite_driver.c +++ b/ext/pdo_sqlite/sqlite_driver.c @@ -78,8 +78,7 @@ int _pdo_sqlite_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int li } if (!dbh->methods) { - zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s] [%d] %s", - *pdo_err, einfo->errcode, einfo->errmsg); + pdo_throw_exception(einfo->errcode, einfo->errmsg, pdo_err); } return einfo->errcode; diff --git a/ext/pdo_sqlite/tests/bug_64705.phpt b/ext/pdo_sqlite/tests/bug_64705.phpt new file mode 100644 index 0000000000000..bf3babf4f95aa --- /dev/null +++ b/ext/pdo_sqlite/tests/bug_64705.phpt @@ -0,0 +1,17 @@ +--TEST-- +Bug #64705 errorInfo property of PDOException is null when PDO::__construct() fails +--SKIPIF-- + +--FILE-- +errorInfo) && is_array($e->errorInfo)); +} +?> +--EXPECTF-- +bool(true) \ No newline at end of file From 9d5d575d53ebc58154cebb5e48e45b2ea0238d14 Mon Sep 17 00:00:00 2001 From: Ahmed Abdou Date: Fri, 22 Feb 2019 02:02:39 +0100 Subject: [PATCH 2/5] free message --- ext/pdo/pdo_dbh.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index da2a846e6813d..285fac64ce8ef 100644 --- a/ext/pdo/pdo_dbh.c +++ b/ext/pdo/pdo_dbh.c @@ -59,6 +59,7 @@ void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_e sizeof("message")-1, pdo_exception_message ); + efree(pdo_exception_message); zend_throw_exception_object(&pdo_exception); } From 33cd06df02c1278b1487e87f0ddf4c3c043ee871 Mon Sep 17 00:00:00 2001 From: Ahmed Abdou Date: Fri, 22 Feb 2019 02:19:12 +0100 Subject: [PATCH 3/5] Free errorInfo --- ext/pdo/pdo_dbh.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index 285fac64ce8ef..3eedb410a91d6 100644 --- a/ext/pdo/pdo_dbh.c +++ b/ext/pdo/pdo_dbh.c @@ -60,6 +60,7 @@ void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_e pdo_exception_message ); efree(pdo_exception_message); + zval_ptr_dtor(&error_info); zend_throw_exception_object(&pdo_exception); } From e88f9368fb476dd8074df696f84a407cc9dcd4f4 Mon Sep 17 00:00:00 2001 From: Ahmed Abdou Date: Fri, 22 Feb 2019 02:19:39 +0100 Subject: [PATCH 4/5] fix sqlite test --- ext/pdo_sqlite/tests/bug_64705.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/pdo_sqlite/tests/bug_64705.phpt b/ext/pdo_sqlite/tests/bug_64705.phpt index bf3babf4f95aa..dccf2f605a21b 100644 --- a/ext/pdo_sqlite/tests/bug_64705.phpt +++ b/ext/pdo_sqlite/tests/bug_64705.phpt @@ -6,7 +6,7 @@ if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?> --FILE-- Date: Fri, 22 Feb 2019 03:23:50 +0100 Subject: [PATCH 5/5] fix sqlite test --- ext/pdo/pdo_dbh.c | 1 + ext/pdo_sqlite/tests/bug_64705.phpt | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index 3eedb410a91d6..51ecdaba38a4a 100644 --- a/ext/pdo/pdo_dbh.c +++ b/ext/pdo/pdo_dbh.c @@ -42,6 +42,7 @@ void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_e { zval error_info,pdo_exception; char *pdo_exception_message; + object_init_ex(&pdo_exception, php_pdo_get_exception()); array_init(&error_info); diff --git a/ext/pdo_sqlite/tests/bug_64705.phpt b/ext/pdo_sqlite/tests/bug_64705.phpt index dccf2f605a21b..861a99fbd9b28 100644 --- a/ext/pdo_sqlite/tests/bug_64705.phpt +++ b/ext/pdo_sqlite/tests/bug_64705.phpt @@ -6,7 +6,7 @@ if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?> --FILE--