-
Type:
Task
-
Resolution: Won't Do
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Not Needed
-
None
-
C Drivers
-
None
-
None
-
None
-
None
-
None
-
None
Proposal
Add an inline variant of bson_append_array_builder_t
Details
CDRIVER-6180 proposes deprecating bson_append_array_begin for the safer alternative of bson_append_array_builder_t. However, bson_append_array_buider_t has slightly different API expectations.
bson_append_array_begin does not require a call to bson_append_array_end to free memory:
bool append_array (bson_t *out) { bson_t array; if (!BSON_APPEND_ARRAY_BEGIN(out, "foo", &array)) { return false; } if (!something()) { // OK. No leak. return false; } return bson_append_array_end(&doc); }
Naively migrating this to bson_array_builder_t may result in a leak:
bool append_array_builder(bson_t *out) { bson_array_builder_t *array_builder; if (!BSON_APPEND_ARRAY_BUILDER_BEGIN(out, "foo", &array_builder)) { return false; } if (!something()) { // Leaks `*array_builder`! Missing call to `bson_append_array_builder_end` or `bson_array_builder_destroy`. return false; } return bson_append_array_builder_end(out, array_builder); }
This ticket proposes an inline API to ease migration (and avoid an extra allocation):
bool append_inline_array_builder(bson_t *out) { bson_array_builder_t inline_array_builder = BSON_ARRAY_BUILDER_INITIALIZER; if (!BSON_APPEND_ARRAY_BUILDER_INLINE_BEGIN(out, "foo", &inline_array_builder)) { return false; } if (!something()) { // OK. No leak. return false; } return bson_append_array_builder_end(out, &inline_array_builder); }
Further motivation:
GitHub code search shows examples that do not call bson_append_array_end on some failures:
BSON_APPEND_ARRAY_BEGIN(ret, "values", &subarray); // ... else { ERROR("write_mongodb plugin: Unknown ds_type %d for index %" PRIsz, ds->ds[i].type, i); bson_destroy(ret); return NULL; } // ... bson_append_array_end(ret, &subarray);
BSON_APPEND_ARRAY_BEGIN(doc, k, &child);
if (json_to_bson_append_array(&child, v) < 0) {
LM_ERR("Failed to append array to bson_t\n");
return -1;
}
bson_append_array_end(doc, &child);
If these examples were migrated to bson_array_builder_t and a call to bson_append_array_builder_end or bson_array_builder_destroy was not added on early returns, that could risk leaks.
- is depended on by
-
CDRIVER-6180 Deprecate bson_append_array_begin
-
- In Code Review
-
- is related to
-
CDRIVER-504 Convenient API to create BSON arrays
-
- Closed
-