Skip to content

Allow mongoc_buffer_t larger than INT_MAX #1467

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 3 commits into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 13 additions & 25 deletions src/libmongoc/src/mongoc/mongoc-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@
#endif


#define SPACE_FOR(_b, _sz) \
(((ssize_t) (_b)->datalen - (ssize_t) (_b)->len) >= (ssize_t) (_sz))
static void
make_space_for (mongoc_buffer_t *buffer, size_t data_size)
{
if (buffer->len + data_size > buffer->datalen) {
buffer->datalen = bson_next_power_of_two (buffer->len + data_size);
buffer->data = (uint8_t *) buffer->realloc_func (
Comment on lines +38 to +39
Copy link
Contributor

@eramongodb eramongodb Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a size_t overflow check.

const size_t new_datalen = bson_next_power_of_two (buffer->len + data_size);
BSON_ASSERT (new_datalen > buffer->datalen); // Overflow check.
buffer->datalen = bson_next_power_of_two (new_datalen);

buffer->data, buffer->datalen, buffer->realloc_data);
}
}


/**
Expand Down Expand Up @@ -135,12 +142,7 @@ _mongoc_buffer_append (mongoc_buffer_t *buffer,

BSON_ASSERT (buffer->datalen);

if (!SPACE_FOR (buffer, data_size)) {
BSON_ASSERT ((buffer->datalen + data_size) < INT_MAX);
buffer->datalen = bson_next_power_of_two (data_size + buffer->len);
buffer->data =
(uint8_t *) buffer->realloc_func (buffer->data, buffer->datalen, NULL);
}
make_space_for (buffer, data_size);

buf = &buffer->data[buffer->len];

Expand Down Expand Up @@ -186,12 +188,7 @@ _mongoc_buffer_append_from_stream (mongoc_buffer_t *buffer,

BSON_ASSERT (buffer->datalen);

if (!SPACE_FOR (buffer, size)) {
BSON_ASSERT ((buffer->datalen + size) < INT_MAX);
buffer->datalen = bson_next_power_of_two (size + buffer->len);
buffer->data =
(uint8_t *) buffer->realloc_func (buffer->data, buffer->datalen, NULL);
}
make_space_for (buffer, size);

buf = &buffer->data[buffer->len];

Expand Down Expand Up @@ -260,11 +257,7 @@ _mongoc_buffer_fill (mongoc_buffer_t *buffer,

min_bytes -= buffer->len;

if (!SPACE_FOR (buffer, min_bytes)) {
buffer->datalen = bson_next_power_of_two (buffer->len + min_bytes);
buffer->data = (uint8_t *) buffer->realloc_func (
buffer->data, buffer->datalen, buffer->realloc_data);
}
make_space_for (buffer, min_bytes);

avail_bytes = buffer->datalen - buffer->len;

Expand Down Expand Up @@ -341,12 +334,7 @@ _mongoc_buffer_try_append_from_stream (mongoc_buffer_t *buffer,

BSON_ASSERT (buffer->datalen);

if (!SPACE_FOR (buffer, size)) {
BSON_ASSERT ((buffer->datalen + size) < INT_MAX);
buffer->datalen = bson_next_power_of_two (size + buffer->len);
buffer->data =
(uint8_t *) buffer->realloc_func (buffer->data, buffer->datalen, NULL);
}
make_space_for (buffer, size);

buf = &buffer->data[buffer->len];

Expand Down