Skip to content

Commit 310609b

Browse files
author
Christian Hergert
committed
buffer: ensure realloc size includes current offset and length.
1 parent 47da505 commit 310609b

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/mongoc/mongoc-buffer.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,26 +155,30 @@ _mongoc_buffer_append_from_stream (mongoc_buffer_t *buffer,
155155
bson_return_val_if_fail (size, false);
156156

157157
BSON_ASSERT (buffer->datalen);
158+
BSON_ASSERT ((buffer->datalen + size) < INT_MAX);
158159

159160
if (!SPACE_FOR (buffer, size)) {
160161
if (buffer->len) {
161162
memmove(&buffer->data[0], &buffer->data[buffer->off], buffer->len);
162163
}
163164
buffer->off = 0;
164165
if (!SPACE_FOR (buffer, size)) {
165-
buffer->datalen = bson_next_power_of_two ((uint32_t)size);
166+
buffer->datalen = bson_next_power_of_two ((uint32_t)(size + buffer->len + buffer->off));
166167
buffer->data = buffer->realloc_func (buffer->data, buffer->datalen, NULL);
167168
}
168169
}
169170

170171
buf = &buffer->data[buffer->off + buffer->len];
172+
173+
BSON_ASSERT ((buffer->off + buffer->len + size) <= buffer->datalen);
174+
171175
ret = mongoc_stream_read (stream, buf, size, size, timeout_msec);
172176
if (ret != size) {
173177
bson_set_error (error,
174178
MONGOC_ERROR_STREAM,
175179
MONGOC_ERROR_STREAM_SOCKET,
176-
"Failed to read %u bytes from socket.",
177-
(unsigned)size);
180+
"Failed to read %"PRIu64" bytes from socket.",
181+
(uint64_t)size);
178182
RETURN (false);
179183
}
180184

0 commit comments

Comments
 (0)