Skip to content

Commit d3cdb62

Browse files
rcsanchez97kevinAlbseramongodb
committed
CDRIVER-5601 more robust bson append (#1648)
Co-authored-by: Kevin Albertson <[email protected]> Co-authored-by: Ezra Chung <[email protected]>
1 parent 3012f26 commit d3cdb62

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/libbson/src/bson/bson.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,18 @@ _bson_append_va (bson_t *bson, /* IN */
324324

325325
buf = _bson_data (bson) + bson->len - 1;
326326

327+
/* Track running sum of bytes written in a uint64_t to detect possible overflow of `n_bytes`. */
328+
uint64_t n_bytes_sum = 0;
327329
do {
330+
// Size of any individual data being appended should not exceed the total byte limit.
331+
if (BSON_UNLIKELY (bson_cmp_less_uu (n_bytes, data_len))) {
332+
return false;
333+
}
334+
// Total size of data being appended should not exceed the total byte limit.
335+
if (BSON_UNLIKELY (bson_cmp_greater_uu (n_bytes_sum, n_bytes - data_len))) {
336+
return false;
337+
}
338+
n_bytes_sum += data_len;
328339
n_pairs--;
329340
/* data may be NULL if data_len is 0. memcpy is not safe to call with
330341
* NULL. */

0 commit comments

Comments
 (0)