diff --git a/mongoose.c b/mongoose.c index ec0b1acd..fe8ff5b2 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1533,10 +1533,13 @@ int mg_iobuf_init(struct mg_iobuf *io, size_t size) { size_t mg_iobuf_append(struct mg_iobuf *io, const void *buf, size_t len, size_t chunk_size) { - size_t new_size = io->len + len + chunk_size; - new_size -= new_size % chunk_size; - if (new_size != io->size) mg_iobuf_resize(io, new_size); - if (new_size != io->size) len = 0; // Realloc failure, append nothing + size_t new_size = io->len + len; + if (new_size > io->size) { + new_size += chunk_size; + new_size -= new_size % chunk_size; + if (new_size != io->size) mg_iobuf_resize(io, new_size); + if (new_size != io->size) len = 0; // Realloc failure, append nothing + } if (buf != NULL) memmove(io->buf + io->len, buf, len); io->len += len; return len; diff --git a/mongoose.h b/mongoose.h index 4fe646a1..5da8b471 100644 --- a/mongoose.h +++ b/mongoose.h @@ -127,7 +127,16 @@ #include #include #include + +#if defined(__GNUC__) #include +#else +typedef long suseconds_t; +struct timeval { + time_t tv_sec; + suseconds_t tv_usec; +}; +#endif #if MG_ENABLE_FS #include diff --git a/src/iobuf.c b/src/iobuf.c index 0d0826a3..3275f20b 100644 --- a/src/iobuf.c +++ b/src/iobuf.c @@ -44,10 +44,13 @@ int mg_iobuf_init(struct mg_iobuf *io, size_t size) { size_t mg_iobuf_append(struct mg_iobuf *io, const void *buf, size_t len, size_t chunk_size) { - size_t new_size = io->len + len + chunk_size; - new_size -= new_size % chunk_size; - if (new_size != io->size) mg_iobuf_resize(io, new_size); - if (new_size != io->size) len = 0; // Realloc failure, append nothing + size_t new_size = io->len + len; + if (new_size > io->size) { + new_size += chunk_size; + new_size -= new_size % chunk_size; + if (new_size != io->size) mg_iobuf_resize(io, new_size); + if (new_size != io->size) len = 0; // Realloc failure, append nothing + } if (buf != NULL) memmove(io->buf + io->len, buf, len); io->len += len; return len;