Skip to content

Commit 3624e5a

Browse files
committed
PHPC-1272: phongo_execute_query() should not alter state of Query opts
1 parent 0022839 commit 3624e5a

File tree

5 files changed

+10
-27
lines changed

5 files changed

+10
-27
lines changed

php_phongo.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ bool phongo_cursor_advance_and_check_for_error(mongoc_cursor_t* cursor TSRMLS_DC
760760
bool phongo_execute_query(mongoc_client_t* client, const char* namespace, zval* zquery, zval* options, uint32_t server_id, zval* return_value, int return_value_used TSRMLS_DC) /* {{{ */
761761
{
762762
const php_phongo_query_t* query;
763+
bson_t opts = BSON_INITIALIZER;
763764
mongoc_cursor_t* cursor;
764765
char* dbname;
765766
char* collname;
@@ -777,30 +778,36 @@ bool phongo_execute_query(mongoc_client_t* client, const char* namespace, zval*
777778

778779
query = Z_QUERY_OBJ_P(zquery);
779780

781+
bson_copy_to(query->opts, &opts);
782+
780783
if (query->read_concern) {
781784
mongoc_collection_set_read_concern(collection, query->read_concern);
782785
}
783786

784787
if (!phongo_parse_read_preference(options, &zreadPreference TSRMLS_CC)) {
785788
/* Exception should already have been thrown */
786789
mongoc_collection_destroy(collection);
790+
bson_destroy(&opts);
787791
return false;
788792
}
789793

790-
if (!phongo_parse_session(options, client, query->opts, &zsession TSRMLS_CC)) {
794+
if (!phongo_parse_session(options, client, &opts, &zsession TSRMLS_CC)) {
791795
/* Exception should already have been thrown */
792796
mongoc_collection_destroy(collection);
797+
bson_destroy(&opts);
793798
return false;
794799
}
795800

796-
if (!BSON_APPEND_INT32(query->opts, "serverId", server_id)) {
801+
if (!BSON_APPEND_INT32(&opts, "serverId", server_id)) {
797802
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error appending \"serverId\" option");
798803
mongoc_collection_destroy(collection);
804+
bson_destroy(&opts);
799805
return false;
800806
}
801807

802-
cursor = mongoc_collection_find_with_opts(collection, query->filter, query->opts, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC));
808+
cursor = mongoc_collection_find_with_opts(collection, query->filter, &opts, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC));
803809
mongoc_collection_destroy(collection);
810+
bson_destroy(&opts);
804811

805812
/* maxAwaitTimeMS must be set before the cursor is sent */
806813
if (query->max_await_time_ms) {

tests/manager/manager-executeQuery-001.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
5656
["y"]=>
5757
int(1)
5858
}
59-
["serverId"]=>
60-
int(%d)
6159
}
6260
["readConcern"]=>
6361
NULL

tests/manager/manager-executeQuery-002.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
5555
["y"]=>
5656
int(1)
5757
}
58-
["serverId"]=>
59-
int(%d)
6058
}
6159
["readConcern"]=>
6260
NULL

tests/readPreference/bug0146-001.phpt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
4646
}
4747
["options"]=>
4848
object(stdClass)#%d (%d) {
49-
["serverId"]=>
50-
int(%d)
5149
}
5250
["readConcern"]=>
5351
NULL
@@ -86,8 +84,6 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
8684
}
8785
["options"]=>
8886
object(stdClass)#%d (%d) {
89-
["serverId"]=>
90-
int(%d)
9187
}
9288
["readConcern"]=>
9389
NULL
@@ -126,8 +122,6 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
126122
}
127123
["options"]=>
128124
object(stdClass)#%d (%d) {
129-
["serverId"]=>
130-
int(%d)
131125
}
132126
["readConcern"]=>
133127
NULL
@@ -166,8 +160,6 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
166160
}
167161
["options"]=>
168162
object(stdClass)#%d (%d) {
169-
["serverId"]=>
170-
int(%d)
171163
}
172164
["readConcern"]=>
173165
NULL
@@ -206,8 +198,6 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
206198
}
207199
["options"]=>
208200
object(stdClass)#%d (%d) {
209-
["serverId"]=>
210-
int(%d)
211201
}
212202
["readConcern"]=>
213203
NULL

tests/readPreference/bug0146-002.phpt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
4646
}
4747
["options"]=>
4848
object(stdClass)#%d (%d) {
49-
["serverId"]=>
50-
int(%d)
5149
}
5250
["readConcern"]=>
5351
NULL
@@ -86,8 +84,6 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
8684
}
8785
["options"]=>
8886
object(stdClass)#%d (%d) {
89-
["serverId"]=>
90-
int(%d)
9187
}
9288
["readConcern"]=>
9389
NULL
@@ -126,8 +122,6 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
126122
}
127123
["options"]=>
128124
object(stdClass)#%d (%d) {
129-
["serverId"]=>
130-
int(%d)
131125
}
132126
["readConcern"]=>
133127
NULL
@@ -166,8 +160,6 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
166160
}
167161
["options"]=>
168162
object(stdClass)#%d (%d) {
169-
["serverId"]=>
170-
int(%d)
171163
}
172164
["readConcern"]=>
173165
NULL
@@ -206,8 +198,6 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
206198
}
207199
["options"]=>
208200
object(stdClass)#%d (%d) {
209-
["serverId"]=>
210-
int(%d)
211201
}
212202
["readConcern"]=>
213203
NULL

0 commit comments

Comments
 (0)