Skip to content

Commit 6fc49ab

Browse files
authored
ext/pdo: Convert FETCH_INTO zval to a zend_object pointer (#17525)
1 parent 88bab6e commit 6fc49ab

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

ext/pdo/pdo_stmt.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -836,14 +836,14 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h
836836

837837
case PDO_FETCH_INTO:
838838
/* TODO: Make this an assertion and ensure this is true higher up? */
839-
if (Z_ISUNDEF(stmt->fetch.into)) {
839+
if (stmt->fetch.into == NULL) {
840840
/* TODO ArgumentCountError? */
841841
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "No fetch-into object specified.");
842842
return 0;
843843
break;
844844
}
845845

846-
ZVAL_COPY(return_value, &stmt->fetch.into);
846+
ZVAL_OBJ_COPY(return_value, stmt->fetch.into);
847847

848848
if (Z_OBJ_P(return_value)->ce == ZEND_STANDARD_CLASS_DEF_PTR) {
849849
how = PDO_FETCH_OBJ;
@@ -1655,9 +1655,9 @@ bool pdo_stmt_setup_fetch_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode_a
16551655

16561656
switch (stmt->default_fetch_type) {
16571657
case PDO_FETCH_INTO:
1658-
if (!Z_ISUNDEF(stmt->fetch.into)) {
1659-
zval_ptr_dtor(&stmt->fetch.into);
1660-
ZVAL_UNDEF(&stmt->fetch.into);
1658+
if (stmt->fetch.into) {
1659+
OBJ_RELEASE(stmt->fetch.into);
1660+
stmt->fetch.into = NULL;
16611661
}
16621662
break;
16631663
default:
@@ -1786,7 +1786,8 @@ bool pdo_stmt_setup_fetch_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode_a
17861786
return false;
17871787
}
17881788

1789-
ZVAL_COPY(&stmt->fetch.into, &args[0]);
1789+
GC_ADDREF(Z_OBJ(args[0]));
1790+
stmt->fetch.into = Z_OBJ(args[0]);
17901791
break;
17911792
default:
17921793
zend_argument_value_error(mode_arg_num, "must be one of the PDO::FETCH_* constants");
@@ -2027,10 +2028,15 @@ static zend_function *dbstmt_method_get(zend_object **object_pp, zend_string *me
20272028
static HashTable *dbstmt_get_gc(zend_object *object, zval **gc_data, int *gc_count)
20282029
{
20292030
pdo_stmt_t *stmt = php_pdo_stmt_fetch_object(object);
2031+
enum pdo_fetch_type default_fetch_mode = stmt->default_fetch_type & ~PDO_FETCH_FLAGS;
20302032

20312033
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
20322034
zend_get_gc_buffer_add_zval(gc_buffer, &stmt->database_object_handle);
2033-
zend_get_gc_buffer_add_zval(gc_buffer, &stmt->fetch.into);
2035+
if (default_fetch_mode == PDO_FETCH_INTO) {
2036+
zend_get_gc_buffer_add_obj(gc_buffer, stmt->fetch.into);
2037+
} else if (default_fetch_mode == PDO_FETCH_CLASS) {
2038+
zend_get_gc_buffer_add_zval(gc_buffer, &stmt->fetch.cls.ctor_args);
2039+
}
20342040
zend_get_gc_buffer_use(gc_buffer, gc_data, gc_count);
20352041

20362042
/**
@@ -2077,9 +2083,9 @@ PDO_API void php_pdo_free_statement(pdo_stmt_t *stmt)
20772083

20782084
pdo_stmt_reset_columns(stmt);
20792085

2080-
if (!Z_ISUNDEF(stmt->fetch.into) && stmt->default_fetch_type == PDO_FETCH_INTO) {
2081-
zval_ptr_dtor(&stmt->fetch.into);
2082-
ZVAL_UNDEF(&stmt->fetch.into);
2086+
if (stmt->fetch.into && stmt->default_fetch_type == PDO_FETCH_INTO) {
2087+
OBJ_RELEASE(stmt->fetch.into);
2088+
stmt->fetch.into = NULL;
20832089
}
20842090

20852091
do_fetch_opt_finish(stmt, 1);
@@ -2168,7 +2174,6 @@ static void pdo_stmt_iter_move_forwards(zend_object_iterator *iter)
21682174

21692175
if (!do_fetch(stmt, &I->fetch_ahead, PDO_FETCH_USE_DEFAULT,
21702176
PDO_FETCH_ORI_NEXT, /* offset */ 0, NULL)) {
2171-
21722177
PDO_HANDLE_STMT_ERR();
21732178
I->key = (zend_ulong)-1;
21742179
ZVAL_UNDEF(&I->fetch_ahead);

ext/pdo/php_pdo_driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ struct _pdo_stmt_t {
621621
zval dummy; /* This exists due to alignment reasons with fetch.into and fetch.cls.ctor_args */
622622
zend_fcall_info_cache fcc;
623623
} func;
624-
zval into;
624+
zend_object *into;
625625
} fetch;
626626

627627
/* used by the query parser for driver specific

0 commit comments

Comments
 (0)