Skip to content

PHPC-2513: Check for returned _id before appending insert #1788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is consistent with the newer code going into BulkWriteCommand.c. There was really no need for a variable here.


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
Loading