Skip to content

Commit ad6332f

Browse files
authored
CDRIVER-5878 Free child on failed call to bson_append_array_builder_begin (#1849)
* add failing test * free `child` on failure in `bson_append_array_builder_begin`
1 parent 0f7fcad commit ad6332f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/libbson/src/bson/bson.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -2971,7 +2971,12 @@ bson_append_array_builder_begin (bson_t *bson, const char *key, int key_length,
29712971
BSON_ASSERT_PARAM (key);
29722972
BSON_ASSERT_PARAM (child);
29732973
*child = bson_array_builder_new ();
2974-
return bson_append_array_begin (bson, key, key_length, &(*child)->bson);
2974+
bool ok = bson_append_array_begin (bson, key, key_length, &(*child)->bson);
2975+
if (!ok) {
2976+
bson_array_builder_destroy (*child);
2977+
*child = NULL;
2978+
}
2979+
return ok;
29752980
}
29762981

29772982
bool

src/libbson/tests/test-bson.c

+10
Original file line numberDiff line numberDiff line change
@@ -3196,6 +3196,16 @@ test_bson_array_builder (void)
31963196
bson_array_builder_destroy (bab);
31973197
}
31983198
}
3199+
3200+
// A failure in bson_append_array_builder_begin does not allocate.
3201+
{
3202+
bson_t b = BSON_INITIALIZER;
3203+
bson_array_builder_t *child;
3204+
bool ok = bson_append_array_builder_begin (&b, "has_embedded_null\0", strlen ("has_embedded_null") + 1, &child);
3205+
ASSERT (!ok);
3206+
bson_destroy (&b);
3207+
// Not necessary to free `child`.
3208+
}
31993209
}
32003210

32013211
void

0 commit comments

Comments
 (0)