Skip to content

ext/pdo: Convert database_object_handle zval to zend_object* #17629

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

Merged
merged 1 commit into from
Jan 30, 2025
Merged
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
16 changes: 9 additions & 7 deletions ext/pdo/pdo_dbh.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_e

PDO_API bool php_pdo_stmt_valid_db_obj_handle(const pdo_stmt_t *stmt)
{
return !Z_ISUNDEF(stmt->database_object_handle)
&& IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE(stmt->database_object_handle)])
&& !(OBJ_FLAGS(Z_OBJ(stmt->database_object_handle)) & IS_OBJ_FREE_CALLED);
return stmt->database_object_handle != NULL
&& IS_OBJ_VALID(EG(objects_store).object_buckets[stmt->database_object_handle->handle])
&& !(OBJ_FLAGS(stmt->database_object_handle) & IS_OBJ_FREE_CALLED);
}

void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, pdo_error_type sqlstate, const char *supp) /* {{{ */
Expand Down Expand Up @@ -657,7 +657,8 @@ PHP_METHOD(PDO, prepare)
stmt->default_fetch_type = dbh->default_fetch_type;
stmt->dbh = dbh;
/* give it a reference to me */
ZVAL_OBJ_COPY(&stmt->database_object_handle, &dbh_obj->std);
GC_ADDREF(&dbh_obj->std);
stmt->database_object_handle = &dbh_obj->std;
/* we haven't created a lazy object yet */
ZVAL_UNDEF(&stmt->lazy_object_ref);

Expand Down Expand Up @@ -1222,7 +1223,8 @@ PHP_METHOD(PDO, query)
stmt->default_fetch_type = dbh->default_fetch_type;
stmt->dbh = dbh;
/* give it a reference to me */
ZVAL_OBJ_COPY(&stmt->database_object_handle, &dbh_obj->std);
GC_ADDREF(&dbh_obj->std);
stmt->database_object_handle = &dbh_obj->std;
/* we haven't created a lazy object yet */
ZVAL_UNDEF(&stmt->lazy_object_ref);

Expand Down Expand Up @@ -1252,8 +1254,8 @@ PHP_METHOD(PDO, query)
/* something broke */
dbh->query_stmt = stmt;
ZVAL_OBJ(&dbh->query_stmt_zval, Z_OBJ_P(return_value));
Z_DELREF(stmt->database_object_handle);
ZVAL_UNDEF(&stmt->database_object_handle);
GC_DELREF(stmt->database_object_handle);
stmt->database_object_handle = NULL;
PDO_HANDLE_STMT_ERR();
} else {
PDO_HANDLE_DBH_ERR();
Expand Down
9 changes: 5 additions & 4 deletions ext/pdo/pdo_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,7 @@ static zend_function *dbstmt_method_get(zend_object **object_pp, zend_string *me
/* not a pre-defined method, nor a user-defined method; check
* the driver specific methods */
if (!stmt->dbh->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_STMT]) {
if (!pdo_hash_methods(Z_PDO_OBJECT_P(&stmt->database_object_handle),
if (!pdo_hash_methods(php_pdo_dbh_fetch_object(stmt->database_object_handle),
PDO_DBH_DRIVER_METHOD_KIND_STMT)
|| !stmt->dbh->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_STMT]) {
goto out;
Expand All @@ -2048,7 +2048,7 @@ static HashTable *dbstmt_get_gc(zend_object *object, zval **gc_data, int *gc_cou
enum pdo_fetch_type default_fetch_mode = stmt->default_fetch_type & ~PDO_FETCH_FLAGS;

zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
zend_get_gc_buffer_add_zval(gc_buffer, &stmt->database_object_handle);
zend_get_gc_buffer_add_obj(gc_buffer, stmt->database_object_handle);
if (default_fetch_mode == PDO_FETCH_INTO) {
zend_get_gc_buffer_add_obj(gc_buffer, stmt->fetch.into);
} else if (default_fetch_mode == PDO_FETCH_CLASS) {
Expand Down Expand Up @@ -2107,8 +2107,9 @@ PDO_API void php_pdo_free_statement(pdo_stmt_t *stmt)

do_fetch_opt_finish(stmt, 1);

if (!Z_ISUNDEF(stmt->database_object_handle)) {
zval_ptr_dtor(&stmt->database_object_handle);
if (stmt->database_object_handle != NULL) {
OBJ_RELEASE(stmt->database_object_handle);
stmt->database_object_handle = NULL;
}
zend_object_std_dtor(&stmt->std);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/pdo/php_pdo_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ struct _pdo_stmt_t {
struct pdo_column_data *columns;

/* we want to keep the dbh alive while we live, so we own a reference */
zval database_object_handle;
zend_object *database_object_handle;
pdo_dbh_t *dbh;

/* keep track of bound input parameters. Some drivers support
Expand Down
10 changes: 5 additions & 5 deletions ext/pdo_pgsql/pgsql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,21 @@ const php_stream_ops pdo_pgsql_lob_stream_ops = {
NULL
};

php_stream *pdo_pgsql_create_lob_stream(zval *dbh, int lfd, Oid oid)
php_stream *pdo_pgsql_create_lob_stream(zend_object *dbh, int lfd, Oid oid)
{
php_stream *stm;
struct pdo_pgsql_lob_self *self = ecalloc(1, sizeof(*self));
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)(Z_PDO_DBH_P(dbh))->driver_data;
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)(php_pdo_dbh_fetch_inner(dbh))->driver_data;

ZVAL_COPY_VALUE(&self->dbh, dbh);
ZVAL_OBJ(&self->dbh, dbh);
self->lfd = lfd;
self->oid = oid;
self->conn = H->server;

stm = php_stream_alloc(&pdo_pgsql_lob_stream_ops, self, 0, "r+b");

if (stm) {
Z_ADDREF_P(dbh);
GC_ADDREF(dbh);
zend_hash_index_add_ptr(H->lob_streams, php_stream_get_resource_id(stm), stm->res);
return stm;
}
Expand Down Expand Up @@ -1116,7 +1116,7 @@ void pgsqlLOBOpen_internal(INTERNAL_FUNCTION_PARAMETERS)
lfd = lo_open(H->server, oid, mode);

if (lfd >= 0) {
php_stream *stream = pdo_pgsql_create_lob_stream(ZEND_THIS, lfd, oid);
php_stream *stream = pdo_pgsql_create_lob_stream(Z_OBJ_P(ZEND_THIS), lfd, oid);
if (stream) {
php_stream_to_zval(stream, return_value);
return;
Expand Down
2 changes: 1 addition & 1 deletion ext/pdo_pgsql/pgsql_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, zval *result, enum pd
/* If column was bound as LOB, return a stream. */
int loid = lo_open(S->H->server, oid, INV_READ);
if (loid >= 0) {
php_stream *stream = pdo_pgsql_create_lob_stream(&stmt->database_object_handle, loid, oid);
php_stream *stream = pdo_pgsql_create_lob_stream(stmt->database_object_handle, loid, oid);
if (stream) {
php_stream_to_zval(stream, result);
return 1;
Expand Down
2 changes: 1 addition & 1 deletion ext/pdo_pgsql/php_pdo_pgsql_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ enum pdo_pgsql_specific_constants {
PGSQL_TRANSACTION_UNKNOWN = PQTRANS_UNKNOWN
};

php_stream *pdo_pgsql_create_lob_stream(zval *pdh, int lfd, Oid oid);
php_stream *pdo_pgsql_create_lob_stream(zend_object *pdh, int lfd, Oid oid);
extern const php_stream_ops pdo_pgsql_lob_stream_ops;

void pdo_pgsql_cleanup_notice_callback(pdo_pgsql_db_handle *H);
Expand Down