1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

Address #616 Buffer extra overflow bug.

We should have a normal error instead of undefined behavior, wrap
around, or wait for realloc to fail.
This commit is contained in:
Calvin Rose 2021-01-29 18:32:54 -06:00
parent 77c5279296
commit 9aed578466

View File

@ -91,7 +91,9 @@ void janet_buffer_extra(JanetBuffer *buffer, int32_t n) {
} }
int32_t new_size = buffer->count + n; int32_t new_size = buffer->count + n;
if (new_size > buffer->capacity) { if (new_size > buffer->capacity) {
int32_t new_capacity = new_size * 2; size_t new_capacity_sizet = (size_t) (new_size) * 2;
if (new_capacity_sizet > INT32_MAX) new_capacity_sizet = INT32_MAX;
int32_t new_capacity = (int32_t) new_capacity_sizet;
uint8_t *new_data = realloc(buffer->data, new_capacity * sizeof(uint8_t)); uint8_t *new_data = realloc(buffer->data, new_capacity * sizeof(uint8_t));
janet_gcpressure(new_capacity - buffer->capacity); janet_gcpressure(new_capacity - buffer->capacity);
if (NULL == new_data) { if (NULL == new_data) {