Skip to content

Commit 59d323b

Browse files
committed
PHPC-1879: Session::getTransactionOptions() leaks mongoc_transaction_opt_t
1 parent 9890e61 commit 59d323b

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/MongoDB/Session.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,21 @@ static const char* php_phongo_get_transaction_state_string(mongoc_transaction_st
9898
}
9999
}
100100

101-
static void php_phongo_transaction_options_to_zval(mongoc_transaction_opt_t* opts, zval* retval)
101+
static void php_phongo_transaction_options_to_zval(mongoc_client_session_t* cs, zval* retval)
102102
{
103+
mongoc_transaction_opt_t* opts;
103104
int64_t max_commit_time_ms;
104105
const mongoc_read_concern_t* read_concern;
105106
const mongoc_read_prefs_t* read_preference;
106107
const mongoc_write_concern_t* write_concern;
107108

109+
if (!cs) {
110+
ZVAL_NULL(retval);
111+
return;
112+
}
113+
114+
opts = mongoc_session_opts_get_transaction_opts(cs);
115+
108116
if (!opts) {
109117
ZVAL_NULL(retval);
110118
return;
@@ -141,6 +149,8 @@ static void php_phongo_transaction_options_to_zval(mongoc_transaction_opt_t* opt
141149
phongo_writeconcern_init(&zwrite_concern, write_concern);
142150
ADD_ASSOC_ZVAL_EX(retval, "writeConcern", &zwrite_concern);
143151
}
152+
153+
mongoc_transaction_opts_destroy(opts);
144154
}
145155

146156
/* {{{ proto void MongoDB\Driver\Session::advanceClusterTime(array|object $clusterTime)
@@ -332,8 +342,8 @@ static PHP_METHOD(Session, getServer)
332342
Returns options for the currently running transaction */
333343
static PHP_METHOD(Session, getTransactionOptions)
334344
{
335-
zend_error_handling error_handling;
336-
php_phongo_session_t* intern;
345+
zend_error_handling error_handling;
346+
php_phongo_session_t* intern;
337347

338348
intern = Z_SESSION_OBJ_P(getThis());
339349
SESSION_CHECK_LIVELINESS(intern, "getTransactionOptions")
@@ -345,7 +355,7 @@ static PHP_METHOD(Session, getTransactionOptions)
345355
}
346356
zend_restore_error_handling(&error_handling);
347357

348-
php_phongo_transaction_options_to_zval(mongoc_session_opts_get_transaction_opts(intern->client_session), return_value);
358+
php_phongo_transaction_options_to_zval(intern->client_session, return_value);
349359
} /* }}} */
350360

351361
/* {{{ proto string MongoDB\Driver\Session::getTransactionState()
@@ -728,8 +738,7 @@ static HashTable* php_phongo_session_get_debug_info(phongo_compat_object_handler
728738
}
729739

730740
if (intern->client_session) {
731-
const mongoc_session_opt_t* cs_opts;
732-
cs_opts = mongoc_client_session_get_opts(intern->client_session);
741+
const mongoc_session_opt_t* cs_opts = mongoc_client_session_get_opts(intern->client_session);
733742
ADD_ASSOC_BOOL_EX(&retval, "causalConsistency", mongoc_session_opts_get_causal_consistency(cs_opts));
734743
} else {
735744
ADD_ASSOC_NULL_EX(&retval, "causalConsistency");
@@ -776,10 +785,10 @@ static HashTable* php_phongo_session_get_debug_info(phongo_compat_object_handler
776785

777786
if (intern->client_session) {
778787
const char* state = php_phongo_get_transaction_state_string(mongoc_client_session_get_transaction_state(intern->client_session));
779-
788+
780789
if (!state) {
781790
/* Exception should already have been thrown */
782-
goto done;
791+
goto done;
783792
}
784793

785794
ADD_ASSOC_STRING(&retval, "transactionState", state);
@@ -788,17 +797,9 @@ static HashTable* php_phongo_session_get_debug_info(phongo_compat_object_handler
788797
}
789798

790799
if (intern->client_session) {
791-
mongoc_transaction_opt_t* txn_opts = mongoc_session_opts_get_transaction_opts(intern->client_session);
792-
793-
if (txn_opts) {
794-
795-
zval transaction;
796-
797-
php_phongo_transaction_options_to_zval(txn_opts, &transaction);
798-
ADD_ASSOC_ZVAL_EX(&retval, "transactionOptions", &transaction);
799-
} else {
800-
ADD_ASSOC_NULL_EX(&retval, "transactionOptions");
801-
}
800+
zval txn_opts;
801+
php_phongo_transaction_options_to_zval(intern->client_session, &txn_opts);
802+
ADD_ASSOC_ZVAL_EX(&retval, "transactionOptions", &txn_opts);
802803
} else {
803804
ADD_ASSOC_NULL_EX(&retval, "transactionOptions");
804805
}

0 commit comments

Comments
 (0)