@@ -749,7 +749,7 @@ bool phongo_cursor_advance_and_check_for_error(mongoc_cursor_t *cursor TSRMLS_DC
749
749
return true;
750
750
} /* }}} */
751
751
752
- int 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 ) /* {{{ */
752
+ 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 ) /* {{{ */
753
753
{
754
754
const php_phongo_query_t * query ;
755
755
mongoc_cursor_t * cursor ;
@@ -827,7 +827,7 @@ static bson_t *create_wrapped_command_envelope(const char *db, bson_t *reply)
827
827
return tmp ;
828
828
}
829
829
830
- int phongo_execute_command (mongoc_client_t * client , php_phongo_command_type_t type , const char * db , zval * zcommand , zval * options , uint32_t server_id , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
830
+ bool phongo_execute_command (mongoc_client_t * client , php_phongo_command_type_t type , const char * db , zval * zcommand , zval * options , uint32_t server_id , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
831
831
{
832
832
const php_phongo_command_t * command ;
833
833
bson_iter_t iter ;
@@ -837,38 +837,34 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
837
837
mongoc_cursor_t * cmd_cursor ;
838
838
zval * zreadPreference = NULL ;
839
839
zval * zsession = NULL ;
840
- int result ;
840
+ bool result = false;
841
+ bool free_reply = false;
841
842
842
843
command = Z_COMMAND_OBJ_P (zcommand );
843
844
844
845
if ((type & PHONGO_OPTION_READ_CONCERN ) && !phongo_parse_read_concern (options , & opts TSRMLS_CC )) {
845
846
/* Exception should already have been thrown */
846
- bson_destroy (& opts );
847
- return false;
847
+ goto cleanup ;
848
848
}
849
849
850
850
if ((type & PHONGO_OPTION_READ_PREFERENCE ) && !phongo_parse_read_preference (options , & zreadPreference TSRMLS_CC )) {
851
851
/* Exception should already have been thrown */
852
- bson_destroy (& opts );
853
- return false;
852
+ goto cleanup ;
854
853
}
855
854
856
855
if (!phongo_parse_session (options , client , & opts , & zsession TSRMLS_CC )) {
857
856
/* Exception should already have been thrown */
858
- bson_destroy (& opts );
859
- return false;
857
+ goto cleanup ;
860
858
}
861
859
862
860
if ((type & PHONGO_OPTION_WRITE_CONCERN ) && !phongo_parse_write_concern (options , & opts , NULL TSRMLS_CC )) {
863
861
/* Exception should already have been thrown */
864
- bson_destroy (& opts );
865
- return false;
862
+ goto cleanup ;
866
863
}
867
864
868
865
if (!BSON_APPEND_INT32 (& opts , "serverId" , server_id )) {
869
866
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Error appending \"serverId\" option" );
870
- bson_destroy (& opts );
871
- return false;
867
+ goto cleanup ;
872
868
}
873
869
874
870
/* Although "opts" already always includes the serverId option, the read
@@ -891,21 +887,18 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
891
887
default :
892
888
/* Should never happen, but if it does: exception */
893
889
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 );
894
- bson_destroy (& opts );
895
- return false;
890
+ goto cleanup ;
896
891
}
892
+
893
+ free_reply = true;
894
+
897
895
if (!result ) {
898
896
phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
899
- bson_destroy (& reply );
900
- bson_destroy (& opts );
901
- return false;
897
+ goto cleanup ;
902
898
}
903
899
904
- bson_destroy (& opts );
905
-
906
900
if (!return_value_used ) {
907
- bson_destroy (& reply );
908
- return true;
901
+ goto cleanup ;
909
902
}
910
903
911
904
/* According to mongoc_cursor_new_from_command_reply(), the reply bson_t
@@ -926,16 +919,22 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
926
919
}
927
920
928
921
cmd_cursor = mongoc_cursor_new_from_command_reply (client , & initial_reply , server_id );
929
- bson_destroy (& reply );
930
922
} else {
931
923
bson_t * wrapped_reply = create_wrapped_command_envelope (db , & reply );
932
924
933
925
cmd_cursor = mongoc_cursor_new_from_command_reply (client , wrapped_reply , server_id );
934
- bson_destroy (& reply );
935
926
}
936
927
937
928
phongo_cursor_init_for_command (return_value , client , cmd_cursor , db , zcommand , zreadPreference , zsession TSRMLS_CC );
938
- return true;
929
+
930
+ cleanup :
931
+ bson_destroy (& opts );
932
+
933
+ if (free_reply ) {
934
+ bson_destroy (& reply );
935
+ }
936
+
937
+ return result ;
939
938
} /* }}} */
940
939
/* }}} */
941
940
0 commit comments