Skip to content

Commit b533392

Browse files
committed
Convert constructor arguments in PDO Handler to HashTable*
1 parent 1ea1b63 commit b533392

File tree

3 files changed

+27
-38
lines changed

3 files changed

+27
-38
lines changed

ext/pdo/pdo_dbh.c

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,9 @@ PHP_METHOD(PDO, __construct)
434434
}
435435
/* }}} */
436436

437-
static zval *pdo_stmt_instantiate(pdo_dbh_t *dbh, zval *object, zend_class_entry *dbstmt_ce, zval *ctor_args) /* {{{ */
437+
static zval *pdo_stmt_instantiate(pdo_dbh_t *dbh, zval *object, zend_class_entry *dbstmt_ce, const HashTable *ctor_args) /* {{{ */
438438
{
439-
if (!Z_ISUNDEF_P(ctor_args)) {
440-
/* This implies an error within PDO if this does not hold */
441-
ZEND_ASSERT(Z_TYPE_P(ctor_args) == IS_ARRAY);
439+
if (ctor_args) {
442440
if (!dbstmt_ce->constructor) {
443441
zend_throw_error(NULL, "User-supplied statement does not accept constructor arguments");
444442
return NULL;
@@ -455,7 +453,7 @@ static zval *pdo_stmt_instantiate(pdo_dbh_t *dbh, zval *object, zend_class_entry
455453
return object;
456454
} /* }}} */
457455

458-
static void pdo_stmt_construct(zend_execute_data *execute_data, pdo_stmt_t *stmt, zval *object, zend_class_entry *dbstmt_ce, zval *ctor_args) /* {{{ */
456+
static void pdo_stmt_construct(zend_execute_data *execute_data, pdo_stmt_t *stmt, zval *object, zend_class_entry *dbstmt_ce, /* const */ HashTable *ctor_args) /* {{{ */
459457
{
460458
zval query_string;
461459
zend_string *key;
@@ -476,9 +474,7 @@ static void pdo_stmt_construct(zend_execute_data *execute_data, pdo_stmt_t *stmt
476474
fci.retval = &retval;
477475
fci.param_count = 0;
478476
fci.params = NULL;
479-
fci.named_params = NULL;
480-
481-
zend_fcall_info_args(&fci, ctor_args);
477+
fci.named_params = ctor_args;
482478

483479
fcc.function_handler = dbstmt_ce->constructor;
484480
fcc.called_scope = Z_OBJCE_P(object);
@@ -487,8 +483,6 @@ static void pdo_stmt_construct(zend_execute_data *execute_data, pdo_stmt_t *stmt
487483
if (zend_call_function(&fci, &fcc) != FAILURE) {
488484
zval_ptr_dtor(&retval);
489485
}
490-
491-
zend_fcall_info_args_clear(&fci, 1);
492486
}
493487
}
494488
/* }}} */
@@ -498,7 +492,8 @@ PHP_METHOD(PDO, prepare)
498492
{
499493
pdo_stmt_t *stmt;
500494
zend_string *statement;
501-
zval *options = NULL, *value, *item, ctor_args;
495+
zval *options = NULL, *value, *item;
496+
HashTable *ctor_args = NULL;
502497
zend_class_entry *dbstmt_ce, *pce;
503498
pdo_dbh_object_t *dbh_obj = Z_PDO_OBJECT_P(ZEND_THIS);
504499
pdo_dbh_t *dbh = dbh_obj->inner;
@@ -548,16 +543,14 @@ PHP_METHOD(PDO, prepare)
548543
zend_zval_type_name(value));
549544
RETURN_THROWS();
550545
}
551-
ZVAL_COPY_VALUE(&ctor_args, item);
552-
} else {
553-
ZVAL_UNDEF(&ctor_args);
546+
ctor_args = Z_ARRVAL_P(item);
554547
}
555548
} else {
556549
dbstmt_ce = dbh->def_stmt_ce;
557-
ZVAL_COPY_VALUE(&ctor_args, &dbh->def_stmt_ctor_args);
550+
ctor_args = dbh->def_stmt_ctor_args;
558551
}
559552

560-
if (!pdo_stmt_instantiate(dbh, return_value, dbstmt_ce, &ctor_args)) {
553+
if (!pdo_stmt_instantiate(dbh, return_value, dbstmt_ce, ctor_args)) {
561554
RETURN_THROWS();
562555
}
563556
stmt = Z_PDO_STMT_P(return_value);
@@ -572,7 +565,7 @@ PHP_METHOD(PDO, prepare)
572565
ZVAL_UNDEF(&stmt->lazy_object_ref);
573566

574567
if (dbh->methods->preparer(dbh, statement, stmt, options)) {
575-
pdo_stmt_construct(execute_data, stmt, return_value, dbstmt_ce, &ctor_args);
568+
pdo_stmt_construct(execute_data, stmt, return_value, dbstmt_ce, ctor_args);
576569
return;
577570
}
578571

@@ -833,17 +826,17 @@ static bool pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) /
833826
return false;
834827
}
835828
dbh->def_stmt_ce = pce;
836-
if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
837-
zval_ptr_dtor(&dbh->def_stmt_ctor_args);
838-
ZVAL_UNDEF(&dbh->def_stmt_ctor_args);
829+
if (dbh->def_stmt_ctor_args) {
830+
zend_hash_destroy(dbh->def_stmt_ctor_args);
831+
dbh->def_stmt_ctor_args = NULL;
839832
}
840833
if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 1)) != NULL) {
841834
if (Z_TYPE_P(item) != IS_ARRAY) {
842835
zend_type_error("PDO::ATTR_STATEMENT_CLASS constructor_args must be of type ?array, %s given",
843836
zend_zval_type_name(value));
844837
return false;
845838
}
846-
ZVAL_COPY(&dbh->def_stmt_ctor_args, item);
839+
dbh->def_stmt_ctor_args = zend_array_dup(Z_ARRVAL_P(item));
847840
}
848841
return true;
849842
}
@@ -922,9 +915,9 @@ PHP_METHOD(PDO, getAttribute)
922915
case PDO_ATTR_STATEMENT_CLASS:
923916
array_init(return_value);
924917
add_next_index_str(return_value, zend_string_copy(dbh->def_stmt_ce->name));
925-
if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
926-
Z_TRY_ADDREF(dbh->def_stmt_ctor_args);
927-
add_next_index_zval(return_value, &dbh->def_stmt_ctor_args);
918+
if (dbh->def_stmt_ctor_args) {
919+
//Z_TRY_ADDREF(dbh->def_stmt_ctor_args);
920+
add_next_index_array(return_value, dbh->def_stmt_ctor_args);
928921
}
929922
return;
930923
case PDO_ATTR_DEFAULT_FETCH_MODE:
@@ -1109,7 +1102,7 @@ PHP_METHOD(PDO, query)
11091102

11101103
PDO_DBH_CLEAR_ERR();
11111104

1112-
if (!pdo_stmt_instantiate(dbh, return_value, dbh->def_stmt_ce, &dbh->def_stmt_ctor_args)) {
1105+
if (!pdo_stmt_instantiate(dbh, return_value, dbh->def_stmt_ce, dbh->def_stmt_ctor_args)) {
11131106
RETURN_THROWS();
11141107
}
11151108
stmt = Z_PDO_STMT_P(return_value);
@@ -1138,7 +1131,7 @@ PHP_METHOD(PDO, query)
11381131
stmt->executed = 1;
11391132
}
11401133
if (ret) {
1141-
pdo_stmt_construct(execute_data, stmt, return_value, dbh->def_stmt_ce, &dbh->def_stmt_ctor_args);
1134+
pdo_stmt_construct(execute_data, stmt, return_value, dbh->def_stmt_ce, dbh->def_stmt_ctor_args);
11421135
return;
11431136
}
11441137
}
@@ -1326,7 +1319,11 @@ static HashTable *dbh_get_gc(zend_object *object, zval **gc_data, int *gc_count)
13261319
{
13271320
pdo_dbh_t *dbh = php_pdo_dbh_fetch_inner(object);
13281321
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
1329-
zend_get_gc_buffer_add_zval(gc_buffer, &dbh->def_stmt_ctor_args);
1322+
if (dbh->def_stmt_ctor_args) {
1323+
zval tmp = {0};
1324+
ZVAL_ARR(&tmp, dbh->def_stmt_ctor_args);
1325+
zend_get_gc_buffer_add_zval(gc_buffer, &tmp);
1326+
}
13301327
if (dbh->methods && dbh->methods->get_gc) {
13311328
dbh->methods->get_gc(dbh, gc_buffer);
13321329
}
@@ -1388,9 +1385,7 @@ static void dbh_free(pdo_dbh_t *dbh, bool free_persistent)
13881385
pefree((char *)dbh->persistent_id, dbh->is_persistent);
13891386
}
13901387

1391-
if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
1392-
zval_ptr_dtor(&dbh->def_stmt_ctor_args);
1393-
}
1388+
/* Freeing of constructor arguments is handles via GC */
13941389

13951390
for (i = 0; i < PDO_DBH_DRIVER_METHOD_KIND__MAX; i++) {
13961391
if (dbh->cls_methods[i]) {

ext/pdo/php_pdo_driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ struct _pdo_dbh_t {
487487

488488
zend_class_entry *def_stmt_ce;
489489

490-
zval def_stmt_ctor_args;
490+
HashTable *def_stmt_ctor_args;
491491

492492
/* when calling PDO::query(), we need to keep the error
493493
* context from the statement around until we next clear it.

run-tests.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -857,13 +857,7 @@ function write_information(): void
857857
$php_cgi_info = '';
858858
}
859859

860-
if ($phpdbg) {
861-
$phpdbg_info = shell_exec("$phpdbg $pass_options $info_params $no_file_cache -qrr \"$info_file\"");
862-
$php_info_sep = "\n---------------------------------------------------------------------";
863-
$phpdbg_info = "$php_info_sep\nPHP : $phpdbg $phpdbg_info$php_info_sep";
864-
} else {
865-
$phpdbg_info = '';
866-
}
860+
$phpdbg_info = '';
867861

868862
if (function_exists('opcache_invalidate')) {
869863
opcache_invalidate($info_file, true);

0 commit comments

Comments
 (0)