summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAhmed Abdou <email@ahmed.ro>2019-02-17 22:59:00 +0100
committerNikita Popov <nikita.ppv@gmail.com>2020-08-11 17:12:48 +0200
commit2fe2e5b48f8cf9a45ac2530dd0e2dca8f4287e01 (patch)
tree994e5f20b990ce660cb4208b6f3edf48d9fcfb73
parent2fa4ca95db262267aa6bd46c5e2f74aa42542fc3 (diff)
downloadphp-git-2fe2e5b48f8cf9a45ac2530dd0e2dca8f4287e01.tar.gz
Fix #64705 errorInfo property of PDOException is null when PDO::__construct() fails
PDO driver constructors are throwing PdoException without setting errorInfo, so create a new reusable function that throws exceptions for PDO and will also set the errorInfo. Use this function in pdo_mysql, pdo_sqlite, and pdo_pgsql.
-rw-r--r--NEWS4
-rw-r--r--ext/pdo/pdo_dbh.c27
-rw-r--r--ext/pdo/php_pdo_driver.h1
-rw-r--r--ext/pdo_mysql/mysql_driver.c3
-rw-r--r--ext/pdo_mysql/tests/bug_64705.phpt17
-rw-r--r--ext/pdo_pgsql/pgsql_driver.c3
-rw-r--r--ext/pdo_pgsql/tests/bug_64705.phpt17
-rw-r--r--ext/pdo_sqlite/sqlite_driver.c3
-rw-r--r--ext/pdo_sqlite/tests/bug_64705.phpt17
9 files changed, 86 insertions, 6 deletions
diff --git a/NEWS b/NEWS
index d79a06a2bb..178785e884 100644
--- a/NEWS
+++ b/NEWS
@@ -31,6 +31,10 @@ PHP NEWS
. Fixed bug #73060 (php failed with error after temp folder cleaned up).
(cmb)
+- PDO:
+ . Fixed bug #64705 (errorInfo property of PDOException is null when
+ PDO::__construct() fails). (Ahmed Abdou)
+
- Standard:
. Fixed bug #79930 (array_merge_recursive() crashes when called with array
with single reference). (Nikita)
diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c
index 36bb2a17ac..4df9db11de 100644
--- a/ext/pdo/pdo_dbh.c
+++ b/ext/pdo/pdo_dbh.c
@@ -36,6 +36,33 @@
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
+ );
+ efree(pdo_exception_message);
+ zval_ptr_dtor(&error_info);
+ 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 3350211a5e..b002c3e5f3 100644
--- a/ext/pdo/php_pdo_driver.h
+++ b/ext/pdo/php_pdo_driver.h
@@ -691,6 +691,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 6431ccf4e1..6efa0fc620 100644
--- a/ext/pdo_mysql/mysql_driver.c
+++ b/ext/pdo_mysql/mysql_driver.c
@@ -103,8 +103,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 0000000000..53e79757df
--- /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--
+<?php
+if (!extension_loaded('pdo_mysql')) print 'skip not loaded';
+?>
+--FILE--
+<?php
+$dsn = 'mysql:host=DonotExistsHost;dbname=test;user=foo;password=wrongpass';
+try {
+ $pdo = new \PDO($dsn, null, null);
+} catch (\PDOException $e) {
+ var_dump(!empty($e->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 89dd55167e..794730a225 100644
--- a/ext/pdo_pgsql/pgsql_driver.c
+++ b/ext/pdo_pgsql/pgsql_driver.c
@@ -101,8 +101,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 0000000000..464b4d0769
--- /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--
+<?php
+if (!extension_loaded('pdo_pgsql')) print 'skip not loaded';
+?>
+--FILE--
+<?php
+$dsn = 'pgsql:host=DonotExistsHost;dbname=test;user=foo;password=wrongpass';
+try {
+ $pdo = new \PDO($dsn, null, null);
+} catch (\PDOException $e) {
+ var_dump(!empty($e->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 2cc7f72475..3d85a6db10 100644
--- a/ext/pdo_sqlite/sqlite_driver.c
+++ b/ext/pdo_sqlite/sqlite_driver.c
@@ -76,8 +76,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 0000000000..861a99fbd9
--- /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--
+<?php
+if (!extension_loaded('pdo_sqlite')) print 'skip not loaded';
+?>
+--FILE--
+<?php
+$dsn = 'sqlite:./bug64705NonExistingDir/bug64705NonExistingDb';
+try {
+ $pdo = new \PDO($dsn, null, null);
+} catch (\PDOException $e) {
+ var_dump(!empty($e->errorInfo) && is_array($e->errorInfo));
+}
+?>
+--EXPECTF--
+bool(true) \ No newline at end of file