Skip to content

Commit 9d79ad6

Browse files
committed
Introduce ValueError for empty string arguments
1 parent 07fd1df commit 9d79ad6

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

ext/pdo/pdo_dbh.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ PHP_METHOD(PDO, __construct)
242242
Z_PARAM_ARRAY_OR_NULL(options)
243243
ZEND_PARSE_PARAMETERS_END();
244244

245+
if (username && usernamelen == 0) {
246+
zend_argument_value_error(2, "cannot be empty");
247+
RETURN_THROWS();
248+
}
249+
245250
/* parse the data source name */
246251
colon = strchr(data_source, ':');
247252

@@ -497,9 +502,15 @@ PHP_METHOD(PDO, prepare)
497502
Z_PARAM_ARRAY(options)
498503
ZEND_PARSE_PARAMETERS_END();
499504

500-
PDO_DBH_CLEAR_ERR();
501505
PDO_CONSTRUCT_CHECK;
502506

507+
if (statement_len == 0) {
508+
zend_argument_value_error(1, "cannot be empty");
509+
RETURN_THROWS();
510+
}
511+
512+
PDO_DBH_CLEAR_ERR();
513+
503514
if (options && (value = zend_hash_index_find(Z_ARRVAL_P(options), PDO_ATTR_STATEMENT_CLASS)) != NULL) {
504515
if (Z_TYPE_P(value) != IS_ARRAY) {
505516
zend_type_error("PDO::ATTR_STATEMENT_CLASS's value must be of type array, %s given",
@@ -950,8 +961,15 @@ PHP_METHOD(PDO, lastInsertId)
950961
Z_PARAM_STRING_OR_NULL(name, namelen)
951962
ZEND_PARSE_PARAMETERS_END();
952963

953-
PDO_DBH_CLEAR_ERR();
954964
PDO_CONSTRUCT_CHECK;
965+
966+
if (name && namelen == 0) {
967+
zend_argument_value_error(1, "cannot be empty");
968+
RETURN_THROWS();
969+
}
970+
971+
PDO_DBH_CLEAR_ERR();
972+
955973
if (!dbh->methods->last_id) {
956974
pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support lastInsertId()");
957975
RETURN_FALSE;
@@ -1060,9 +1078,15 @@ PHP_METHOD(PDO, query)
10601078
RETURN_THROWS();
10611079
}
10621080

1063-
PDO_DBH_CLEAR_ERR();
10641081
PDO_CONSTRUCT_CHECK;
10651082

1083+
if (statement_len == 0) {
1084+
zend_argument_value_error(1, "cannot be empty");
1085+
RETURN_THROWS();
1086+
}
1087+
1088+
PDO_DBH_CLEAR_ERR();
1089+
10661090
if (!pdo_stmt_instantiate(dbh, return_value, dbh->def_stmt_ce, &dbh->def_stmt_ctor_args)) {
10671091
if (EXPECTED(!EG(exception))) {
10681092
pdo_raise_impl_error(dbh, NULL, "HY000", "failed to instantiate user supplied statement class");
@@ -1135,8 +1159,14 @@ PHP_METHOD(PDO, quote)
11351159
Z_PARAM_LONG(paramtype)
11361160
ZEND_PARSE_PARAMETERS_END();
11371161

1138-
PDO_DBH_CLEAR_ERR();
11391162
PDO_CONSTRUCT_CHECK;
1163+
1164+
if (str_len == 0) {
1165+
zend_argument_value_error(1, "cannot be empty");
1166+
RETURN_THROWS();
1167+
}
1168+
1169+
PDO_DBH_CLEAR_ERR();
11401170
if (!dbh->methods->quoter) {
11411171
pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support quoting");
11421172
RETURN_FALSE;

ext/pdo/pdo_stmt_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 395813e5ab7b565718430dd17b8da3fc54889eeb */
2+
* Stub hash: 12ba83581b1019e2e0c4565e20682b5e83389cae */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_bindColumn, 0, 0, 2)
55
ZEND_ARG_TYPE_MASK(0, column, MAY_BE_STRING|MAY_BE_LONG, NULL)

ext/pdo_mysql/tests/pdo_mysql_prepare_emulated.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ $db = MySQLPDOTest::factory();
8888
if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
8989
printf("[002] Unable to switch to emulated prepared statements, test will fail\n");
9090

91-
// TODO - that's PDO - you can prepare empty statements!
92-
prepex(3, $db, '',
93-
array(), array('execute' => array('sqlstate' => '42000')));
91+
try {
92+
prepex(3, $db, '', [], ['execute' => ['sqlstate' => '42000']]);
93+
} catch (\ValueError $e) {
94+
echo $e->getMessage(), \PHP_EOL;
95+
}
9496

9597
// lets be fair and do the most simple SELECT first
9698
$stmt = prepex(4, $db, 'SELECT 1 as "one"');
@@ -328,6 +330,7 @@ $db->exec('DROP TABLE IF EXISTS test');
328330
PDO's PS parser has some problems with invalid SQL and crashes from time to time
329331
(check with valgrind...)
330332
--EXPECT--
333+
PDO::prepare(): Argument #1 ($statement) cannot be empty
331334
array(1) {
332335
["one"]=>
333336
string(1) "1"

ext/pdo_mysql/tests/pdo_mysql_prepare_native.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ $db = MySQLPDOTest::factory();
9999
if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
100100
printf("[002] Unable to turn off emulated prepared statements\n");
101101

102-
// TODO - that's PDO - you can prepare empty statements!
103-
prepex(3, $db, '',
104-
array(), array('prepare' => array('sqlstate' => '42000')));
102+
try {
103+
prepex(3, $db, '', [], ['prepare' => ['sqlstate' => '42000']]);
104+
} catch (\ValueError $e) {
105+
echo $e->getMessage(), \PHP_EOL;
106+
}
105107

106108
// lets be fair and do the most simple SELECT first
107109
$stmt = prepex(4, $db, 'SELECT 1 as "one"');
@@ -342,6 +344,7 @@ $db = MySQLPDOTest::factory();
342344
$db->exec('DROP TABLE IF EXISTS test');
343345
?>
344346
--EXPECT--
347+
PDO::prepare(): Argument #1 ($statement) cannot be empty
345348
array(1) {
346349
[0]=>
347350
array(1) {

0 commit comments

Comments
 (0)