Skip to content

Commit 24e8aeb

Browse files
committed
PYTHON-2188 Always raise an error when bson encoding exceeds 2GiB
Remove unused buffer_write_at_position.
1 parent c96220d commit 24e8aeb

File tree

2 files changed

+8
-22
lines changed

2 files changed

+8
-22
lines changed

bson/buffer.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,16 @@ static int buffer_grow(buffer_t buffer, int min_length) {
8989
/* Assure that `buffer` has at least `size` free bytes (and grow if needed).
9090
* Return non-zero on allocation failure. */
9191
static int buffer_assure_space(buffer_t buffer, int size) {
92-
if (buffer->position + size <= buffer->size) {
92+
int new_size = buffer->position + size;
93+
/* Check for overflow. */
94+
if (new_size < buffer->position) {
95+
return 1;
96+
}
97+
98+
if (new_size <= buffer->size) {
9399
return 0;
94100
}
95-
return buffer_grow(buffer, buffer->position + size);
101+
return buffer_grow(buffer, new_size);
96102
}
97103

98104
/* Save `size` bytes from the current position in `buffer` (and grow if needed).
@@ -118,21 +124,6 @@ int buffer_write(buffer_t buffer, const char* data, int size) {
118124
return 0;
119125
}
120126

121-
/* Write `size` bytes from `data` to `buffer` at position `position`.
122-
* Does not change the internal position of `buffer`.
123-
* Return non-zero if buffer isn't large enough for write. */
124-
int buffer_write_at_position(buffer_t buffer, buffer_position position,
125-
const char* data, int size) {
126-
if (position + size > buffer->size) {
127-
buffer_free(buffer);
128-
return 1;
129-
}
130-
131-
memcpy(buffer->buffer + position, data, size);
132-
return 0;
133-
}
134-
135-
136127
int buffer_get_position(buffer_t buffer) {
137128
return buffer->position;
138129
}

bson/buffer.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ buffer_position buffer_save_space(buffer_t buffer, int size);
4141
* Return non-zero on allocation failure. */
4242
int buffer_write(buffer_t buffer, const char* data, int size);
4343

44-
/* Write `size` bytes from `data` to `buffer` at position `position`.
45-
* Does not change the internal position of `buffer`.
46-
* Return non-zero if buffer isn't large enough for write. */
47-
int buffer_write_at_position(buffer_t buffer, buffer_position position, const char* data, int size);
48-
4944
/* Getters for the internals of a buffer_t.
5045
* Should try to avoid using these as much as possible
5146
* since they break the abstraction. */

0 commit comments

Comments
 (0)