Fix bounds check in buffer_append.

We're about to append one character to the buffer _and_ put a \0 after
it, so we need the buffer to be at least _two_ characters longer than
where the current position is.

I think this bug would have had a hard time showing up in normal use,
but I managed to trigger it by completely messing up a prototype
Emscripten preferences implementation, and a good thing too.
This commit is contained in:
Simon Tatham
2023-04-24 09:56:35 +01:00
parent bf453043db
commit 12b2608b24

View File

@ -3020,7 +3020,7 @@ struct buffer {
static void buffer_append(struct buffer *buf, char c) static void buffer_append(struct buffer *buf, char c)
{ {
if (buf->len + 1 > buf->size) { if (buf->len + 2 > buf->size) {
size_t new_size = buf->size + buf->size / 4 + 128; size_t new_size = buf->size + buf->size / 4 + 128;
assert(new_size > buf->size); assert(new_size > buf->size);
buf->data = sresize(buf->data, new_size, char); buf->data = sresize(buf->data, new_size, char);