Skip to content

Commit 87c88c8

Browse files
committed
Type Error for invalid options in PDO::prepare()
1 parent 2aeb53e commit 87c88c8

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

ext/pdo/pdo_dbh.c

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,8 @@ PHP_METHOD(PDO, __construct)
425425
static zval *pdo_stmt_instantiate(pdo_dbh_t *dbh, zval *object, zend_class_entry *dbstmt_ce, zval *ctor_args) /* {{{ */
426426
{
427427
if (!Z_ISUNDEF_P(ctor_args)) {
428-
if (Z_TYPE_P(ctor_args) != IS_ARRAY) {
429-
/* TODO Always TypeError */
430-
pdo_raise_impl_error(dbh, NULL, "HY000", "constructor arguments must be passed as an array");
431-
return NULL;
432-
}
428+
/* This implies an error within PDO if this does not hold */
429+
ZEND_ASSERT(Z_TYPE_P(ctor_args) == IS_ARRAY);
433430
if (!dbstmt_ce->constructor) {
434431
/* TODO Error? */
435432
pdo_raise_impl_error(dbh, NULL, "HY000", "user-supplied statement does not accept constructor arguments");
@@ -489,7 +486,7 @@ PHP_METHOD(PDO, prepare)
489486
pdo_stmt_t *stmt;
490487
char *statement;
491488
size_t statement_len;
492-
zval *options = NULL, *opt, *item, ctor_args;
489+
zval *options = NULL, *value, *item, ctor_args;
493490
zend_class_entry *dbstmt_ce, *pce;
494491
pdo_dbh_object_t *dbh_obj = Z_PDO_OBJECT_P(ZEND_THIS);
495492
pdo_dbh_t *dbh = dbh_obj->inner;
@@ -503,43 +500,38 @@ PHP_METHOD(PDO, prepare)
503500
PDO_DBH_CLEAR_ERR();
504501
PDO_CONSTRUCT_CHECK;
505502

506-
if (ZEND_NUM_ARGS() > 1 && (opt = zend_hash_index_find(Z_ARRVAL_P(options), PDO_ATTR_STATEMENT_CLASS)) != NULL) {
507-
if (Z_TYPE_P(opt) != IS_ARRAY || (item = zend_hash_index_find(Z_ARRVAL_P(opt), 0)) == NULL
508-
|| Z_TYPE_P(item) != IS_STRING
509-
|| (pce = zend_lookup_class(Z_STR_P(item))) == NULL
510-
) {
511-
/* TODO Always TypeError */
512-
pdo_raise_impl_error(dbh, NULL, "HY000",
513-
"PDO::ATTR_STATEMENT_CLASS requires format array(classname, array(ctor_args)); "
514-
"the classname must be a string specifying an existing class"
515-
);
516-
PDO_HANDLE_DBH_ERR();
517-
RETURN_FALSE;
503+
if (options && (value = zend_hash_index_find(Z_ARRVAL_P(options), PDO_ATTR_STATEMENT_CLASS)) != NULL) {
504+
if (Z_TYPE_P(value) != IS_ARRAY) {
505+
zend_type_error("PDO::ATTR_STATEMENT_CLASS's value must be of type array, %s given",
506+
zend_zval_type_name(value));
507+
RETURN_THROWS();
508+
}
509+
if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 0)) == NULL) {
510+
zend_value_error("PDO::ATTR_STATEMENT_CLASS's value must be an array with the following "
511+
"format array(classname, array(ctor_args))");
512+
RETURN_THROWS();
513+
}
514+
if (Z_TYPE_P(item) != IS_STRING || (pce = zend_lookup_class(Z_STR_P(item))) == NULL) {
515+
zend_type_error("PDO::ATTR_STATEMENT_CLASS's class must be a valid class");
516+
RETURN_THROWS();
518517
}
519518
dbstmt_ce = pce;
520519
if (!instanceof_function(dbstmt_ce, pdo_dbstmt_ce)) {
521-
/* TODO Always TypeError */
522-
pdo_raise_impl_error(dbh, NULL, "HY000",
523-
"user-supplied statement class must be derived from PDOStatement");
524-
PDO_HANDLE_DBH_ERR();
525-
RETURN_FALSE;
520+
zend_type_error("PDO::ATTR_STATEMENT_CLASS's class must be derived from PDOStatement");
521+
RETURN_THROWS();
526522
}
527523
if (dbstmt_ce->constructor && !(dbstmt_ce->constructor->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED))) {
528-
/* TODO Always TError */
524+
/* TODO Always Error? */
529525
pdo_raise_impl_error(dbh, NULL, "HY000",
530526
"user-supplied statement class cannot have a public constructor");
531527
PDO_HANDLE_DBH_ERR();
532528
RETURN_FALSE;
533529
}
534-
if ((item = zend_hash_index_find(Z_ARRVAL_P(opt), 1)) != NULL) {
530+
if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 1)) != NULL) {
535531
if (Z_TYPE_P(item) != IS_ARRAY) {
536-
/* TODO Always TypeError */
537-
pdo_raise_impl_error(dbh, NULL, "HY000",
538-
"PDO::ATTR_STATEMENT_CLASS requires format array(classname, ctor_args); "
539-
"ctor_args must be an array"
540-
);
541-
PDO_HANDLE_DBH_ERR();
542-
RETURN_FALSE;
532+
zend_type_error("PDO::ATTR_STATEMENT_CLASS's ctor_args must be ?array, %s given",
533+
zend_zval_type_name(value));
534+
RETURN_THROWS();
543535
}
544536
ZVAL_COPY_VALUE(&ctor_args, item);
545537
} else {

0 commit comments

Comments
 (0)