Skip to content

Commit

Permalink
Fixed bug #79131
Browse files Browse the repository at this point in the history
When a driver reports an error during EVT_ALLOC (and some over EVTs),
make sure we handle it as usual, i.e. warn or throw.

This requires some adjustments in PDO PgSQL to stop manually doing
this through an impl error.

Unfortunately the PDO PgSQL error messages regress because of this,
as they now include a completely arbitrary error code. There doesn't
seem to be an ability to skip it right now.
  • Loading branch information
nikic committed Dec 10, 2020
1 parent dde5572 commit 15b51a2
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ PHP NEWS
. Fixed bug #76815 (PDOStatement cannot be GCed/closeCursor-ed when a
PROCEDURE resultset SIGNAL). (Nikita)
. Fixed bug #79872 (Can't execute query with pending result sets). (Nikita)
. Fixed bug #79131 (PDO does not throw an exception when parameter values are
missing). (Nikita)

- Phar:
. Fixed bug #73809 (Phar Zip parse crash - mmap fail). (cmb)
Expand Down
9 changes: 5 additions & 4 deletions ext/pdo/pdo_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ static bool really_register_bound_param(struct pdo_bound_param_data *param, pdo_
* a reference to param, as it resides in transient storage only
* at this time. */
if (stmt->methods->param_hook) {
if (!stmt->methods->param_hook(stmt, param, PDO_PARAM_EVT_NORMALIZE
)) {
if (!stmt->methods->param_hook(stmt, param, PDO_PARAM_EVT_NORMALIZE)) {
PDO_HANDLE_STMT_ERR();
if (param->name) {
zend_string_release_ex(param->name, 0);
param->name = NULL;
Expand All @@ -367,8 +367,8 @@ static bool really_register_bound_param(struct pdo_bound_param_data *param, pdo_

/* tell the driver we just created a parameter */
if (stmt->methods->param_hook) {
if (!stmt->methods->param_hook(stmt, pparam, PDO_PARAM_EVT_ALLOC
)) {
if (!stmt->methods->param_hook(stmt, pparam, PDO_PARAM_EVT_ALLOC)) {
PDO_HANDLE_STMT_ERR();
/* undo storage allocation; the hash will free the parameter
* name if required */
if (pparam->name) {
Expand Down Expand Up @@ -479,6 +479,7 @@ PHP_METHOD(PDOStatement, execute)
}

if (ret && !dispatch_param_event(stmt, PDO_PARAM_EVT_EXEC_POST)) {
PDO_HANDLE_STMT_ERR();
RETURN_FALSE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ require __DIR__ . '/mysql_pdo_test.inc';
$db = MySQLPDOTest::factory();
$db->exec('DROP TABLE IF EXISTS test');
?>
--EXPECT--
--EXPECTF--
array(1) {
[0]=>
array(2) {
Expand All @@ -75,6 +75,8 @@ array(1) {
}
}
now the same with native PS

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in %s on line %d
[005] Execute has failed, 'HY093' array (
0 => 'HY093',
1 => NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ require __DIR__ . '/mysql_pdo_test.inc';
$db = MySQLPDOTest::factory();
$db->exec('DROP TABLE IF EXISTS test');
?>
--EXPECT--
--EXPECTF--
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in %s on line %d
[003] Execute has failed, 'HY093' array (
0 => 'HY093',
1 => NULL,
Expand Down
6 changes: 4 additions & 2 deletions ext/pdo_pgsql/pgsql_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *
ZEND_ATOL(param->paramno, namevar + 1);
param->paramno--;
} else {
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", ZSTR_VAL(param->name));
pdo_pgsql_error_stmt_msg(
stmt, PGRES_FATAL_ERROR, "HY093", ZSTR_VAL(param->name));
return 0;
}
}
Expand All @@ -294,7 +295,8 @@ static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *
return 1;
}
if (!zend_hash_index_exists(stmt->bound_param_map, param->paramno)) {
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
pdo_pgsql_error_stmt_msg(
stmt, PGRES_FATAL_ERROR, "HY093", "parameter was not defined");
return 0;
}
case PDO_PARAM_EVT_EXEC_POST:
Expand Down
3 changes: 2 additions & 1 deletion ext/pdo_pgsql/php_pdo_pgsql_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ extern int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const
#define pdo_pgsql_error(d,e,z) _pdo_pgsql_error(d, NULL, e, z, NULL, __FILE__, __LINE__)
#define pdo_pgsql_error_msg(d,e,m) _pdo_pgsql_error(d, NULL, e, NULL, m, __FILE__, __LINE__)
#define pdo_pgsql_error_stmt(s,e,z) _pdo_pgsql_error(s->dbh, s, e, z, NULL, __FILE__, __LINE__)
#define pdo_pgsql_error_stmt_msg(s,e,m) _pdo_pgsql_error(s->dbh, s, e, NULL, m, __FILE__, __LINE__)
#define pdo_pgsql_error_stmt_msg(stmt, e, sqlstate, msg) \
_pdo_pgsql_error(stmt->dbh, stmt, e, sqlstate, msg, __FILE__, __LINE__)

extern const struct pdo_stmt_methods pgsql_stmt_methods;

Expand Down
2 changes: 1 addition & 1 deletion ext/pdo_pgsql/tests/bug36727.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ var_dump($stmt->bindValue(':test', 1, PDO::PARAM_INT));
echo "Done\n";
?>
--EXPECTF--
Warning: PDOStatement::bindValue(): SQLSTATE[HY093]: Invalid parameter number: :test in %sbug36727.php on line %d
Warning: PDOStatement::bindValue(): SQLSTATE[HY093]: Invalid parameter number: 7 :test in %s on line %d
bool(false)
Done
2 changes: 1 addition & 1 deletion ext/pdo_pgsql/tests/bug69344.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ $test();

?>
--EXPECT--
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
SQLSTATE[HY093]: Invalid parameter number: 7 parameter was not defined
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
2 changes: 1 addition & 1 deletion ext/pdo_pgsql/tests/bug71573.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ $statement->execute([ 'test', 'test', 'test' ]);

?>
--EXPECTF--
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in %sbug71573.php on line %d
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: 7 parameter was not defined in %s on line %d

0 comments on commit 15b51a2

Please sign in to comment.