Skip to content

Commit 981af26

Browse files
committed
Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3: Fix #64705 errorInfo property of PDOException is null when PDO::__construct() fails
2 parents 5c55086 + 2fe2e5b commit 981af26

File tree

9 files changed

+86
-6
lines changed

9 files changed

+86
-6
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ PHP NEWS
3535
. Fixed bug #79917 (File cache segfault with a static variable in inherited
3636
method). (Nikita)
3737

38+
- PDO:
39+
. Fixed bug #64705 (errorInfo property of PDOException is null when
40+
PDO::__construct() fails). (Ahmed Abdou)
41+
3842
- Standard:
3943
. Fixed bug #79930 (array_merge_recursive() crashes when called with array
4044
with single reference). (Nikita)

ext/pdo/pdo_dbh.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,33 @@
3737

3838
static int pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value);
3939

40+
void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_error_type *pdo_error)
41+
{
42+
zval error_info,pdo_exception;
43+
char *pdo_exception_message;
44+
45+
object_init_ex(&pdo_exception, php_pdo_get_exception());
46+
array_init(&error_info);
47+
48+
add_next_index_string(&error_info, *pdo_error);
49+
add_next_index_long(&error_info, driver_errcode);
50+
add_next_index_string(&error_info, driver_errmsg);
51+
52+
spprintf(&pdo_exception_message, 0,"SQLSTATE[%s] [%d] %s",*pdo_error, driver_errcode, driver_errmsg);
53+
zend_update_property(php_pdo_get_exception(), &pdo_exception, "errorInfo", sizeof("errorInfo")-1, &error_info);
54+
zend_update_property_long(php_pdo_get_exception(), &pdo_exception, "code", sizeof("code")-1, driver_errcode);
55+
zend_update_property_string(
56+
php_pdo_get_exception(),
57+
&pdo_exception,
58+
"message",
59+
sizeof("message")-1,
60+
pdo_exception_message
61+
);
62+
efree(pdo_exception_message);
63+
zval_ptr_dtor(&error_info);
64+
zend_throw_exception_object(&pdo_exception);
65+
}
66+
4067
void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *sqlstate, const char *supp) /* {{{ */
4168
{
4269
pdo_error_type *pdo_err = &dbh->error_code;

ext/pdo/php_pdo_driver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,4 +691,5 @@ PDO_API void php_pdo_dbh_delref(pdo_dbh_t *dbh);
691691
PDO_API void php_pdo_free_statement(pdo_stmt_t *stmt);
692692

693693

694+
PDO_API void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_error_type *pdo_error);
694695
#endif /* PHP_PDO_DRIVER_H */

ext/pdo_mysql/mysql_driver.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ int _pdo_mysql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int lin
103103

104104
if (!dbh->methods) {
105105
PDO_DBG_INF("Throwing exception");
106-
zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s] [%d] %s",
107-
*pdo_err, einfo->errcode, einfo->errmsg);
106+
pdo_throw_exception(einfo->errcode, einfo->errmsg, pdo_err);
108107
}
109108

110109
PDO_DBG_RETURN(einfo->errcode);

ext/pdo_mysql/tests/bug_64705.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #64705 errorInfo property of PDOException is null when PDO::__construct() fails
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('pdo_mysql')) print 'skip not loaded';
6+
?>
7+
--FILE--
8+
<?php
9+
$dsn = 'mysql:host=DonotExistsHost;dbname=test;user=foo;password=wrongpass';
10+
try {
11+
$pdo = new \PDO($dsn, null, null);
12+
} catch (\PDOException $e) {
13+
var_dump(!empty($e->errorInfo) && is_array($e->errorInfo));
14+
}
15+
?>
16+
--EXPECTF--
17+
bool(true)

ext/pdo_pgsql/pgsql_driver.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *
9696
}
9797

9898
if (!dbh->methods) {
99-
zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s] [%d] %s",
100-
*pdo_err, einfo->errcode, einfo->errmsg);
99+
pdo_throw_exception(einfo->errcode, einfo->errmsg, pdo_err);
101100
}
102101

103102
return errcode;

ext/pdo_pgsql/tests/bug_64705.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #64705 errorInfo property of PDOException is null when PDO::__construct() fails
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('pdo_pgsql')) print 'skip not loaded';
6+
?>
7+
--FILE--
8+
<?php
9+
$dsn = 'pgsql:host=DonotExistsHost;dbname=test;user=foo;password=wrongpass';
10+
try {
11+
$pdo = new \PDO($dsn, null, null);
12+
} catch (\PDOException $e) {
13+
var_dump(!empty($e->errorInfo) && is_array($e->errorInfo));
14+
}
15+
?>
16+
--EXPECTF--
17+
bool(true)

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ int _pdo_sqlite_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int li
7676
}
7777

7878
if (!dbh->methods) {
79-
zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s] [%d] %s",
80-
*pdo_err, einfo->errcode, einfo->errmsg);
79+
pdo_throw_exception(einfo->errcode, einfo->errmsg, pdo_err);
8180
}
8281

8382
return einfo->errcode;

ext/pdo_sqlite/tests/bug_64705.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #64705 errorInfo property of PDOException is null when PDO::__construct() fails
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('pdo_sqlite')) print 'skip not loaded';
6+
?>
7+
--FILE--
8+
<?php
9+
$dsn = 'sqlite:./bug64705NonExistingDir/bug64705NonExistingDb';
10+
try {
11+
$pdo = new \PDO($dsn, null, null);
12+
} catch (\PDOException $e) {
13+
var_dump(!empty($e->errorInfo) && is_array($e->errorInfo));
14+
}
15+
?>
16+
--EXPECTF--
17+
bool(true)

0 commit comments

Comments
 (0)