Skip to content

CXX-3098 Throw from to_json when bson_init_static returns false #1204

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
Sep 5, 2024
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
8 changes: 6 additions & 2 deletions src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ void bson_free_deleter(std::uint8_t* ptr) {

std::string to_json_helper(document::view view, decltype(bson_as_json) converter) {
bson_t bson;
bson_init_static(&bson, view.data(), view.length());

if (!bson_init_static(&bson, view.data(), view.length())) {
throw exception(error_code::k_failed_converting_bson_to_json);
}

size_t size;
auto result = converter(&bson, &size);

if (!result)
if (!result) {
throw exception(error_code::k_failed_converting_bson_to_json);
}

const auto deleter = [](char* result) { bson_free(result); };
const std::unique_ptr<char[], decltype(deleter)> cleanup(result, deleter);
Expand Down