Skip to content

Commit bb2adac

Browse files
committed
Use stack-based BSON instead of heap-allocated
1 parent 4c5f9a6 commit bb2adac

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

php_phongo.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, php_ph
621621
php_phongo_writeresult_t *writeresult;
622622
zval *zwriteConcern = NULL;
623623
const mongoc_write_concern_t *write_concern;
624-
bson_t *opts;
624+
bson_t opts = BSON_INITIALIZER;
625625

626626
if (bulk_write->executed) {
627627
phongo_throw_exception(PHONGO_ERROR_WRITE_FAILED TSRMLS_CC, "BulkWrite objects may only be executed once and this instance has already been executed");
@@ -633,15 +633,16 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, php_ph
633633
return false;
634634
}
635635

636-
opts = bson_new();
637-
638636
/* FIXME: Legacy way of specifying the writeConcern option into this function */
639637
if (options && Z_TYPE_P(options) == IS_OBJECT && instanceof_function(Z_OBJCE_P(options), php_phongo_writeconcern_ce TSRMLS_CC)) {
640638
zwriteConcern = options;
641-
} else if (!phongo_execute_parse_options(client, server_id, options, PHONGO_COMMAND_WRITE, opts, NULL, &zwriteConcern TSRMLS_CC)) {
639+
} else if (!phongo_execute_parse_options(client, server_id, options, PHONGO_COMMAND_WRITE, &opts, NULL, &zwriteConcern TSRMLS_CC)) {
640+
bson_destroy(&opts);
642641
return false;
643642
}
644643

644+
bson_destroy(&opts);
645+
645646
mongoc_bulk_operation_set_database(bulk, bulk_write->database);
646647
mongoc_bulk_operation_set_collection(bulk, bulk_write->collection);
647648
mongoc_bulk_operation_set_client(bulk, client);
@@ -727,7 +728,7 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
727728
char *collname;
728729
mongoc_collection_t *collection;
729730
zval *zreadPreference = NULL;
730-
bson_t *opts;
731+
bson_t opts = BSON_INITIALIZER;
731732

732733
if (!phongo_split_namespace(namespace, &dbname, &collname)) {
733734
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s: %s", "Invalid namespace provided", namespace);
@@ -742,16 +743,17 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
742743
if (query->read_concern) {
743744
mongoc_collection_set_read_concern(collection, query->read_concern);
744745
}
745-
746-
opts = bson_new();
747746

748747
/* FIXME: Legacy way of specifying the readPreference option into this function */
749748
if (options && Z_TYPE_P(options) == IS_OBJECT && instanceof_function(Z_OBJCE_P(options), php_phongo_readpreference_ce TSRMLS_CC)) {
750749
zreadPreference = options;
751-
} else if (!phongo_execute_parse_options(client, server_id, options, PHONGO_COMMAND_READ, opts, &zreadPreference, NULL TSRMLS_CC)) {
750+
} else if (!phongo_execute_parse_options(client, server_id, options, PHONGO_COMMAND_READ, &opts, &zreadPreference, NULL TSRMLS_CC)) {
751+
bson_destroy(&opts);
752752
return false;
753753
}
754754

755+
bson_destroy(&opts);
756+
755757
cursor = mongoc_collection_find_with_opts(collection, query->filter, query->opts, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC));
756758
mongoc_collection_destroy(collection);
757759

@@ -797,26 +799,24 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
797799
bson_iter_t iter;
798800
bson_t reply;
799801
bson_error_t error;
800-
bson_t *opts;
802+
bson_t opts = BSON_INITIALIZER;
801803
mongoc_cursor_t *cmd_cursor;
802804
uint32_t selected_server_id;
803805
zval *zreadPreference = NULL;
804806
int result;
805807

806808
command = Z_COMMAND_OBJ_P(zcommand);
807809

808-
opts = bson_new();
809-
810810
/* FIXME: Legacy way of specifying the readPreference option into this function */
811811
if (options && Z_TYPE_P(options) == IS_OBJECT && instanceof_function(Z_OBJCE_P(options), php_phongo_readpreference_ce TSRMLS_CC)) {
812812
zreadPreference = options;
813-
} else if (!phongo_execute_parse_options(client, server_id, options, type, opts, &zreadPreference, NULL TSRMLS_CC)) {
813+
} else if (!phongo_execute_parse_options(client, server_id, options, type, &opts, &zreadPreference, NULL TSRMLS_CC)) {
814814
return false;
815815
}
816816

817-
selected_server_id = phongo_do_select_server(client, opts, zreadPreference, server_id TSRMLS_CC);
817+
selected_server_id = phongo_do_select_server(client, &opts, zreadPreference, server_id TSRMLS_CC);
818818
if (!selected_server_id) {
819-
bson_free(opts);
819+
bson_destroy(&opts);
820820
return false;
821821
}
822822

@@ -825,31 +825,31 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
825825
* command construction. */
826826
switch (type) {
827827
case PHONGO_COMMAND_RAW:
828-
result = mongoc_client_command_with_opts(client, db, command->bson, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC), opts, &reply, &error);
828+
result = mongoc_client_command_with_opts(client, db, command->bson, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC), &opts, &reply, &error);
829829
break;
830830
case PHONGO_COMMAND_READ:
831-
result = mongoc_client_read_command_with_opts(client, db, command->bson, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC), opts, &reply, &error);
831+
result = mongoc_client_read_command_with_opts(client, db, command->bson, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC), &opts, &reply, &error);
832832
break;
833833
case PHONGO_COMMAND_WRITE:
834-
result = mongoc_client_write_command_with_opts(client, db, command->bson, opts, &reply, &error);
834+
result = mongoc_client_write_command_with_opts(client, db, command->bson, &opts, &reply, &error);
835835
break;
836836
case PHONGO_COMMAND_READ_WRITE:
837837
/* We can pass NULL as readPreference, as this argument was added historically, but has no function */
838-
result = mongoc_client_read_write_command_with_opts(client, db, command->bson, NULL, opts, &reply, &error);
838+
result = mongoc_client_read_write_command_with_opts(client, db, command->bson, NULL, &opts, &reply, &error);
839839
break;
840840
default:
841841
/* Should never happen, but if it does: exception */
842842
phongo_throw_exception(PHONGO_ERROR_LOGIC TSRMLS_CC, "Type '%d' should never have been passed to phongo_execute_command, please file a bug report", type);
843-
bson_free(opts);
843+
bson_destroy(&opts);
844844
return false;
845845
}
846846
if (!result) {
847847
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
848-
bson_free(opts);
848+
bson_destroy(&opts);
849849
return false;
850850
}
851851

852-
bson_free(opts);
852+
bson_destroy(&opts);
853853

854854
if (!return_value_used) {
855855
bson_destroy(&reply);

0 commit comments

Comments
 (0)