Skip to content

Commit b286fef

Browse files
committed
Use bool, check mongoc_*_append return values, and return upon weird unreachable exception
1 parent c9a2cf8 commit b286fef

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

php_phongo.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,14 @@ mongoc_bulk_operation_t *phongo_bulkwrite_init(zend_bool ordered) { /* {{{ */
451451
#define PHONGO_WRITECONCERN_ALLOWED 0x01
452452
#define PHONGO_READPREFERENCE_ALLOWED 0x02
453453

454-
static int process_read_concern(zval *option, bson_t *mongoc_opts TSRMLS_DC)
454+
static bool process_read_concern(zval *option, bson_t *mongoc_opts TSRMLS_DC)
455455
{
456456
if (Z_TYPE_P(option) == IS_OBJECT && instanceof_function(Z_OBJCE_P(option), php_phongo_readconcern_ce TSRMLS_CC)) {
457457
const mongoc_read_concern_t *read_concern = phongo_read_concern_from_zval(option TSRMLS_CC);
458458

459-
mongoc_read_concern_append((mongoc_read_concern_t*)read_concern, mongoc_opts);
459+
if (!mongoc_read_concern_append((mongoc_read_concern_t*)read_concern, mongoc_opts)) {
460+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error appending \"%s\" option", "ReadPreference");
461+
}
460462
} else {
461463
phongo_throw_exception(
462464
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
@@ -497,7 +499,7 @@ static int phongo_do_select_server(mongoc_client_t *client, bson_t *opts, zval *
497499
return selected_server_id;
498500
}
499501

500-
static int process_read_preference(zval *option, bson_t *mongoc_opts, zval **zreadPreference, mongoc_client_t *client, int server_id TSRMLS_DC)
502+
static bool process_read_preference(zval *option, bson_t *mongoc_opts, zval **zreadPreference, mongoc_client_t *client, int server_id TSRMLS_DC)
501503
{
502504
if (Z_TYPE_P(option) == IS_OBJECT && instanceof_function(Z_OBJCE_P(option), php_phongo_readpreference_ce TSRMLS_CC)) {
503505
int selected_server_id;
@@ -519,14 +521,16 @@ static int process_read_preference(zval *option, bson_t *mongoc_opts, zval **zre
519521
return true;
520522
}
521523

522-
static int process_write_concern(zval *option, bson_t *mongoc_opts, zval **zwriteConcern TSRMLS_DC)
524+
static bool process_write_concern(zval *option, bson_t *mongoc_opts, zval **zwriteConcern TSRMLS_DC)
523525
{
524526
if (Z_TYPE_P(option) == IS_OBJECT && instanceof_function(Z_OBJCE_P(option), php_phongo_writeconcern_ce TSRMLS_CC)) {
525527
const mongoc_write_concern_t *write_concern = phongo_write_concern_from_zval(option TSRMLS_CC);
526528

527529
*zwriteConcern = option;
528530

529-
mongoc_write_concern_append((mongoc_write_concern_t*) write_concern, mongoc_opts);
531+
if (!mongoc_write_concern_append((mongoc_write_concern_t*) write_concern, mongoc_opts)) {
532+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error appending \"%s\" option", "WriteConcern");
533+
}
530534
} else {
531535
phongo_throw_exception(
532536
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
@@ -830,6 +834,8 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
830834
default:
831835
/* Should never happen, but if it does: exception */
832836
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);
837+
bson_free(opts);
838+
return false;
833839
}
834840
if (!result) {
835841
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);

0 commit comments

Comments
 (0)