aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/template.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/src/template.c b/src/template.c
index 0529c62..7aa3a75 100644
--- a/src/template.c
+++ b/src/template.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <err.h>
#include "vendor/mpc.h"
#include "template.h"
@@ -26,9 +27,16 @@ struct buffer {
};
void buffer_reserve(struct buffer *buf, int l) {
- if (buf->size + l >= buf->cap) {
- buf->cap *= 2;
- buf->string = realloc(buf->string, buf->size + l);
+ int req_size = buf->size + l;
+ if (req_size >= buf->cap) {
+ while (req_size >= buf->cap) {
+ buf->cap *= 2;
+ }
+
+ buf->string = realloc(buf->string, buf->cap * sizeof *buf->string);
+ if (!buf->string) {
+ err(EXIT_FAILURE, "out of memory");
+ }
}
}