Skip to content

Commit 8a85033

Browse files
authored
CDRIVER-5681 Address potential buffer overflow when writing null terminator after decimal128 significand digits (#1719)
* Address integer overflow when writing null terminator for decimal128 string * Add test case for truncated significand * BSON_ASSERT + strcmp -> ASSERT_CMPSTR * Add test case for untruncated significand * Extend significand truncation tests to assert inexact rounding errors * Assert return value of bson_decimal128_from_string * BSON_ASSERT -> ASSERT
1 parent 865ff9b commit 8a85033

File tree

2 files changed

+348
-297
lines changed

2 files changed

+348
-297
lines changed

src/libbson/src/bson/bson-decimal128.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,11 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
282282
} else {
283283
int32_t radix_position = significand_digits + exponent;
284284

285+
// Reserve space for null terminator.
286+
const int available_bytes = BSON_DECIMAL128_STRING - 1;
287+
285288
if (radix_position > 0) { /* non-zero digits before radix */
286-
for (int32_t i = 0; i < radix_position && (str_out - str) < BSON_DECIMAL128_STRING; i++) {
289+
for (int32_t i = 0; i < radix_position && (str_out - str) < available_bytes; i++) {
287290
*(str_out++) = *(significand_read++) + '0';
288291
}
289292
} else { /* leading zero before radix point */
@@ -296,7 +299,7 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
296299
}
297300

298301
for (uint32_t i = 0; bson_cmp_greater_us (significand_digits - i, BSON_MAX (radix_position - 1, 0)) &&
299-
(str_out - str) < BSON_DECIMAL128_STRING;
302+
(str_out - str) < available_bytes;
300303
i++) {
301304
*(str_out++) = *(significand_read++) + '0';
302305
}

0 commit comments

Comments
 (0)