Skip to content

Fix bug 77490 https://bugs.php.net/bug.php?id=77490 #4288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ext/pdo/pdo_sql_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,13 @@ PDO_API int pdo_parse_params(pdo_stmt_t *stmt, char *inquery, size_t inquery_len
/* parameter was not defined */
ret = -1;
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");

strlcpy(stmt->dbh->error_code, PDO_ERR_NONE, sizeof(PDO_ERR_NONE));
if (stmt->dbh->query_stmt) {
stmt->dbh->query_stmt = NULL;
zval_ptr_dtor(&stmt->dbh->query_stmt_zval);
}

goto clean_up;
}
if (stmt->dbh->methods->quoter) {
Expand Down
7 changes: 7 additions & 0 deletions ext/pdo/pdo_sql_parser.re
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ safe:
/* parameter was not defined */
ret = -1;
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");

strlcpy(stmt->dbh->error_code, PDO_ERR_NONE, sizeof(PDO_ERR_NONE));
if (stmt->dbh->query_stmt) {
stmt->dbh->query_stmt = NULL;
zval_ptr_dtor(&stmt->dbh->query_stmt_zval);
}

goto clean_up;
}
if (stmt->dbh->methods->quoter) {
Expand Down
15 changes: 15 additions & 0 deletions ext/pdo/pdo_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ static inline int rewrite_name_to_position(pdo_stmt_t *stmt, struct pdo_bound_pa
return 1;
}
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");

strlcpy(stmt->dbh->error_code, PDO_ERR_NONE, sizeof(PDO_ERR_NONE));
if (stmt->dbh->query_stmt) {
stmt->dbh->query_stmt = NULL;
zval_ptr_dtor(&stmt->dbh->query_stmt_zval);
}

return 0;
}

Expand All @@ -152,6 +159,13 @@ static inline int rewrite_name_to_position(pdo_stmt_t *stmt, struct pdo_bound_pa
return 1;
} ZEND_HASH_FOREACH_END();
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");

strlcpy(stmt->dbh->error_code, PDO_ERR_NONE, sizeof(PDO_ERR_NONE));
if (stmt->dbh->query_stmt) {
stmt->dbh->query_stmt = NULL;
zval_ptr_dtor(&stmt->dbh->query_stmt_zval);
}

return 0;
}
return 1;
Expand Down Expand Up @@ -466,6 +480,7 @@ static PHP_METHOD(PDOStatement, execute)
if (!Z_ISUNDEF(param.parameter)) {
zval_ptr_dtor(&param.parameter);
}
PDO_HANDLE_STMT_ERR();
RETURN_FALSE;
}
} ZEND_HASH_FOREACH_END();
Expand Down
76 changes: 76 additions & 0 deletions ext/pdo_mysql/tests/bug77490.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
--TEST--
Bug #77490 (PDO mysql does not give exception for too many params when none required)
--SKIPIF--
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
MySQLPDOTest::skip();

?>
--FILE--
<?php

include __DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc';

// Test exception mode throws
echo "Testing exception.\n";
$db = MySQLPDOTest::factory();

$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$createSql = "CREATE TABLE `bug77490` (
`label` varchar(255) NOT NULL DEFAULT '0'
)";

$db->exec('drop table if exists bug77490');
$db->exec($createSql);
$insertSql = "insert into bug77490 (label) values (label)";
$stmt = $db->prepare($insertSql);
$values = [
':label' => 'foo',
];

try {
$stmt->execute($values);
echo "Failed to throw exception\n";
}
catch (PDOException $pdoException) {
echo "Ok, exception caught: " . $pdoException->getMessage() . "\n";
}


// Test non-exception mode gives warning
echo "Testing warning.";
$db = MySQLPDOTest::factory();

$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);

$createSql = "CREATE TABLE `bug77490` (
`label` varchar(255) NOT NULL DEFAULT '0'
)";

$db->exec('drop table if exists bug77490');
$db->exec($createSql);
$insertSql = "insert into bug77490 (label) values (label)";
$stmt = $db->prepare($insertSql);
$values = [
':label' => 'foo',
];

try {
$stmt->execute($values);
}
catch (PDOException $pdoException) {
echo "fail, exception shouldn't have been thrown: " . $pdoException->getMessage() . "\n";
}

echo "done\n";

?>
--EXPECTF--
Testing exception.
Ok, exception caught: SQLSTATE[HY093]: Invalid parameter number
Testing warning.
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in %s on line %d
done
2 changes: 2 additions & 0 deletions ext/pdo_mysql/tests/pdo_mysql_prepare_emulated_anonymous.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -73,6 +73,7 @@ $db = MySQLPDOTest::factory();
$db->exec('DROP TABLE IF EXISTS test');
?>
--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