commit 301473795e1eb79511fb814674312d43e2286059
parent fb79c20e36ba4e20178a2f96bce29173b1eba677
Author: Jan Klemkow <j.klemkow@wemelug.de>
Date: Mon, 17 Feb 2020 22:20:42 +0100
implement error avoiding realloc
Diffstat:
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/scroll.c b/scroll.c
@@ -126,6 +126,25 @@ eamalloc(size_t size)
return mem;
}
+/* error avoiding remalloc */
+void *
+earealloc(void *ptr, size_t size)
+{
+ void *mem;
+
+ while ((mem = realloc(ptr, size)) == NULL) {
+ struct line *line = TAILQ_LAST(&head, tailhead);
+
+ if (line == NULL)
+ return NULL;
+
+ TAILQ_REMOVE(&head, line, entries);
+ free(line->buf);
+ free(line);
+ }
+
+ return mem;
+}
/* Count string length w/o ansi esc sequences. */
size_t
@@ -319,9 +338,9 @@ main(int argc, char *argv[])
buf[pos++] = c;
if (pos == size) {
size *= 2;
- buf = realloc(buf, size);
+ buf = earealloc(buf, size);
if (buf == NULL)
- die("realloc:");
+ die("aerealloc:");
}
if (write(STDOUT_FILENO, &c, 1) == -1)
die("write:");