Skip to content

Commit

Permalink
PHPC-2513: Check for returned _id before appending insert
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikola committed Feb 18, 2025
1 parent 65bbd1b commit 977469b
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/MongoDB/BulkWrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,36 +381,33 @@ static PHP_METHOD(MongoDB_Driver_BulkWrite, insert)
php_phongo_bulkwrite_t* intern;
zval* zdocument;
bson_t bdocument = BSON_INITIALIZER, boptions = BSON_INITIALIZER;
bson_t* bson_out = NULL;
int bson_flags = PHONGO_BSON_ADD_ID;
bson_error_t error = { 0 };
bson_t* bson_out = NULL;
bson_error_t error = { 0 };

intern = Z_BULKWRITE_OBJ_P(getThis());

PHONGO_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY_OR_OBJECT(zdocument)
PHONGO_PARSE_PARAMETERS_END();

bson_flags |= PHONGO_BSON_RETURN_ID;

php_phongo_zval_to_bson(zdocument, bson_flags, &bdocument, &bson_out);
php_phongo_zval_to_bson(zdocument, (PHONGO_BSON_ADD_ID | PHONGO_BSON_RETURN_ID), &bdocument, &bson_out);

if (EG(exception)) {
goto cleanup;
}

if (!bson_out) {
phongo_throw_exception(PHONGO_ERROR_LOGIC, "php_phongo_zval_to_bson() did not return an _id. Please file a bug report.");
goto cleanup;
}

if (!mongoc_bulk_operation_insert_with_opts(intern->bulk, &bdocument, &boptions, &error)) {
phongo_throw_exception_from_bson_error_t(&error);
goto cleanup;
}

intern->num_ops++;

if (!bson_out) {
phongo_throw_exception(PHONGO_ERROR_LOGIC, "Did not receive result from bulk write. Please file a bug report.");
goto cleanup;
}

php_phongo_bulkwrite_extract_id(bson_out, &return_value);

cleanup:
Expand Down

0 comments on commit 977469b

Please sign in to comment.