Skip to content

CDRIVER-5681 Address potential buffer overflow when writing null terminator after decimal128 significand digits #1719

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 8 commits into from
Sep 10, 2024
7 changes: 5 additions & 2 deletions src/libbson/src/bson/bson-decimal128.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,11 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
} else {
int32_t radix_position = significand_digits + exponent;

// Reserve space for null terminator.
const int available_bytes = BSON_DECIMAL128_STRING - 1;

if (radix_position > 0) { /* non-zero digits before radix */
for (int32_t i = 0; i < radix_position && (str_out - str) < BSON_DECIMAL128_STRING; i++) {
for (int32_t i = 0; i < radix_position && (str_out - str) < available_bytes; i++) {
*(str_out++) = *(significand_read++) + '0';
}
} else { /* leading zero before radix point */
Expand All @@ -296,7 +299,7 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
}

for (uint32_t i = 0; bson_cmp_greater_us (significand_digits - i, BSON_MAX (radix_position - 1, 0)) &&
(str_out - str) < BSON_DECIMAL128_STRING;
(str_out - str) < available_bytes;
i++) {
*(str_out++) = *(significand_read++) + '0';
}
Expand Down
Loading