}
// Rounds up length to the nearest chunk size
-static size_t pakfire_buffer_align(size_t length) {
+static size_t pakfire_buffer_align(struct pakfire_buffer* self, size_t length) {
// Always have at least one chunk
if (!length)
return CHUNK_SIZE;
- return ((length + CHUNK_SIZE - 1) / CHUNK_SIZE) * CHUNK_SIZE;
+ // Round up to the nearest chunk size
+ length = ((length + CHUNK_SIZE - 1) / CHUNK_SIZE) * CHUNK_SIZE;
+
+ // Don't ever grow bigger than the maximum length
+ if (self->max_length && length > self->max_length)
+ length = self->max_length;
+
+ return length;
}
static int pakfire_buffer_resize(struct pakfire_buffer* self, size_t length) {
return -ENOBUFS;
// Round up to the nearest chunk size
- length = pakfire_buffer_align(length);
+ length = pakfire_buffer_align(self, length);
// Re-allocate the buffer
self->data = pakfire_realloc(self->data, length);