commit 4887fe8e1f55ed41a01b8dd934c218fdea3eb3e1
parent ca1e2a971e3c9de6ed026860056128a13949e5ff
Author: hhvn <dev@hhvn.uk>
Date: Sun, 20 Feb 2022 19:43:39 +0000
Error-checking realloc & use emalloc in more places
Diffstat:
2 files changed, 19 insertions(+), 7 deletions(-)
diff --git a/src/main.c b/src/main.c
@@ -59,6 +59,19 @@ estrdup(const char *str) {
return ret;
}
+void *
+erealloc(void *ptr, size_t size) {
+ void *mem;
+
+ if ((mem = realloc(ptr, size)) == NULL) {
+ perror("realloc()");
+ endwin();
+ exit(EXIT_FAILURE);
+ }
+
+ return mem;
+}
+
/* Assign memory and store in array for freeing by main loop */
void *
talloc(size_t size) {
@@ -70,10 +83,9 @@ talloc(size_t size) {
if (size) {
mem = emalloc(size);
if (!mems)
- mema = malloc((sizeof(char *)) * (mems + 1));
+ mema = emalloc((sizeof(char *)) * (mems + 1));
else
- mema = realloc(mema, (sizeof(char *)) * (mems + 1));
- assert(mema != NULL);
+ mema = erealloc(mema, (sizeof(char *)) * (mems + 1));
*(mema + mems) = mem;
mems++;
diff --git a/src/serv.c b/src/serv.c
@@ -414,7 +414,7 @@ support_set(struct Server *server, char *key, char *value) {
return;
if (!server->supports) {
- server->supports = malloc(sizeof(struct Support));
+ server->supports = emalloc(sizeof(struct Support));
server->supports->prev = server->supports->next = NULL;
server->supports->key = key ? strdup(key) : NULL;
server->supports->value = value ? strdup(value) : NULL;
@@ -429,7 +429,7 @@ support_set(struct Server *server, char *key, char *value) {
}
}
- p->next = malloc(sizeof(struct Support));
+ p->next = emalloc(sizeof(struct Support));
p->next->prev = p;
p->next->next = NULL;
p->next->key = key ? strdup(key) : NULL;
@@ -523,7 +523,7 @@ schedule_push(struct Server *server, char *tmsg, char *msg) {
return;
if (!server->schedule) {
- server->schedule = malloc(sizeof(struct Schedule));
+ server->schedule = emalloc(sizeof(struct Schedule));
server->schedule->prev = server->schedule->next = NULL;
server->schedule->tmsg = strdup(tmsg);
server->schedule->msg = strdup(msg);
@@ -532,7 +532,7 @@ schedule_push(struct Server *server, char *tmsg, char *msg) {
for (p = server->schedule; p && p->next; p = p->next);
- p->next = malloc(sizeof(struct Schedule));
+ p->next = emalloc(sizeof(struct Schedule));
p->next->prev = p;
p->next->next = NULL;
p->next->tmsg = strdup(tmsg);