Skip to content

Commit 407fa91

Browse files
committed
Throw exception on failure
This prevents a broken Query object that can lead to segfaults
1 parent a3ee5d6 commit 407fa91

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

php_phongo.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,12 @@ int phongo_execute_query(mongoc_client_t *client, char *namespace, php_phongo_qu
601601
cursor = mongoc_collection_find(collection, query->flags, query->skip, query->limit, query->batch_size, query->query, query->selector, read_preference);
602602
mongoc_collection_destroy(collection);
603603

604+
/* mongoc issues a warning we need to catch somehow */
605+
if (!cursor) {
606+
phongo_throw_exception(PHONGO_ERROR_MONGOC_FAILED TSRMLS_CC, "%s", "FIXME: Couldn't create cursor...");
607+
return false;
608+
}
609+
604610
if (!mongoc_cursor_next(cursor, &doc)) {
605611
bson_error_t error;
606612

@@ -910,7 +916,7 @@ bool phongo_query_init(php_phongo_query_t *query, zval *filter, zval *options TS
910916
zval *zquery = NULL;
911917

912918
if (filter && !(Z_TYPE_P(filter) == IS_ARRAY || Z_TYPE_P(filter) == IS_OBJECT)) {
913-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected filter to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(filter)));
919+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected filter to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(filter)));
914920
return false;
915921
}
916922

@@ -929,7 +935,7 @@ bool phongo_query_init(php_phongo_query_t *query, zval *filter, zval *options TS
929935
zval *modifiers = php_array_fetchc(options, "modifiers");
930936

931937
if (modifiers && !(Z_TYPE_P(modifiers) == IS_ARRAY || Z_TYPE_P(modifiers) == IS_OBJECT)) {
932-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected modifiers to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(modifiers)));
938+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected modifiers to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(modifiers)));
933939
zval_ptr_dtor(&zquery);
934940
return false;
935941
}
@@ -942,7 +948,7 @@ bool phongo_query_init(php_phongo_query_t *query, zval *filter, zval *options TS
942948
zval *projection = php_array_fetchc(options, "projection");
943949

944950
if (projection && !(Z_TYPE_P(projection) == IS_ARRAY || Z_TYPE_P(projection) == IS_OBJECT)) {
945-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected projection to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(projection)));
951+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected projection to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(projection)));
946952
zval_ptr_dtor(&zquery);
947953
return false;
948954
}
@@ -956,7 +962,7 @@ bool phongo_query_init(php_phongo_query_t *query, zval *filter, zval *options TS
956962
zval *sort = php_array_fetchc(options, "sort");
957963

958964
if (sort && !(Z_TYPE_P(sort) == IS_ARRAY || Z_TYPE_P(sort) == IS_OBJECT)) {
959-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected sort to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(sort)));
965+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected sort to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(sort)));
960966
zval_ptr_dtor(&zquery);
961967
return false;
962968
}

0 commit comments

Comments
 (0)