dag

Djmoch's Auto Generator
git clone git://git.danielmoch.com/dag.git
Log | Files | Refs | README | LICENSE

commit 8570aa8230cc9f06ac78e5fbb0763d6a76964c12
parent aa554186f25b5ff77b63764b0a5108bbb033648f
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Tue, 23 Nov 2021 07:13:21 -0500

Check for errors during memory allocations

Diffstat:
Mdagfile.c | 33+++++++++++++++++++++++++++------
Mdagindex.c | 12++++++------
Mhtml.c | 20++++++++++++++------
Mparse.y | 69++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
4 files changed, 103 insertions(+), 31 deletions(-)

diff --git a/dagfile.c b/dagfile.c @@ -170,7 +170,12 @@ copy_file(char *file) if (outdated(target, 1, file)) { char fmt[] = "cp %s %s"; - char *cmd = malloc((strlen(file) + strlen(target) + strlen(fmt) + 1) * sizeof(char)); + char *cmd; + + if ((cmd = malloc((strlen(file) + strlen(target) + strlen(fmt) + 1) * sizeof(char))) == NULL) { + err(errno, "malloc failed"); + } + sprintf(cmd, fmt, file, target); printf("%s\n", cmd); system(cmd); @@ -184,7 +189,11 @@ static char * make_outpath(const char *file) { int len = strlen(file) + strlen(tgt->path) + 1; - char *target = malloc(len * sizeof(char)); + char *target; + + if ((target = malloc(len * sizeof(char))) == NULL) { + err(errno, "malloc failed"); + } strcpy(target, file); strnswp(target, src->path, tgt->path, len); @@ -194,8 +203,11 @@ make_outpath(const char *file) * to be changed */ if (sfx) { + char *tmp; len = strlen(target) + strlen(sfx->value) + 1; - char *tmp = malloc(len * sizeof(char)); + if ((tmp = malloc(len * sizeof(char))) == NULL) { + err(errno, "malloc failed"); + } strcpy(tmp, target); strnswp(tmp, ext->value, sfx->value, len); free(target); @@ -255,7 +267,11 @@ fmt_filter(char *f_cmd, char *file, char *target) { struct requirement *r = sfx->requirements; char i = '1'; - char *cmd = strdup(f_cmd); + char *cmd; + + if ((cmd = strdup(f_cmd)) == NULL) { + err(errno, "strdup failed"); + } while (r != NULL) { if (i > '9') @@ -273,9 +289,12 @@ fmt_filter(char *f_cmd, char *file, char *target) static char * expand(char *cmd, char pat, char *subst) { + char *tmp; int ct = 0; int len = strlen(cmd) + strcnt(cmd, '%') + 1; - cmd = realloc(cmd, len); + if ((cmd = realloc(cmd, len)) == NULL) { + err(errno, "realloc failed"); + } strnesc(cmd, len); for (int j=0;;j++) { if (cmd[j] == '\0') @@ -289,7 +308,9 @@ expand(char *cmd, char pat, char *subst) cmd[j+1] = 's'; } } - char *tmp = malloc((strlen(cmd) + (ct * strlen(subst)) + 1) * sizeof(char)); + if ((tmp = malloc((strlen(cmd) + (ct * strlen(subst)) + 1) * sizeof(char))) == NULL) { + err(errno, "malloc failed"); + } sprintf_ct(tmp, cmd, subst, ct); free(cmd); return tmp; diff --git a/dagindex.c b/dagindex.c @@ -62,17 +62,17 @@ populate_str(const char *src, const char *dft) if (verbose >= 2) { fprintf(stderr, "DEBUG: populate_field src is non-NULL: %s\n", src); } - dst = strdup(src); + if ((dst = strdup(src)) == NULL) { + err(errno, "strdup failed"); + } } else { if (verbose >= 2) { fprintf(stderr, "DEBUG: populate_field src is NULL\n"); } - dst = strdup(dft); - } - - if (dst == NULL) { - err(errno, "strdup failed in populate_str"); + if ((dst = strdup(dft)) == NULL) { + err(errno, "strdup failed"); + } } return dst; diff --git a/html.c b/html.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details */ #include <err.h> +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -15,13 +16,13 @@ static void htmlescape(char *s); void html_db_fmt(struct db_index *index) { + char *date, *title; puts("<table>"); struct db_entry *entry = index->entries; + struct tm *pub_tm = NULL; + char pub[DATE_STRING_LENGTH]; while (entry != NULL) { - struct tm *pub_tm = NULL; - char pub[DATE_STRING_LENGTH]; - if ((pub_tm = gmtime(&(entry->date_published))) == NULL) { errx(1, "call to gmtime failed"); } @@ -29,8 +30,13 @@ html_db_fmt(struct db_index *index) errx(1, "call to strftime failed"); } - char *date = strdup(pub); - char *title = strdup(entry->title); + if ((date = strdup(pub)) == NULL) { + err(errno, "strdup failed"); + } + + if ((title = strdup(entry->title)) == NULL) { + err(errno, "strdup failed"); + } htmlescape(date); htmlescape(title); @@ -67,7 +73,9 @@ htmlescape(char *s) } maxlen = len + (ct * strlen("&quot;")); - s = realloc(s, maxlen); + if ((s = realloc(s, maxlen)) == NULL) { + err(errno, "realloc failed"); + } strnswp(s, "'", "&#39;", maxlen); strnswp(s, "\"", "&quot;", maxlen); diff --git a/parse.y b/parse.y @@ -1,5 +1,7 @@ %{ /* See LICENSE file for copyright and license details */ +#include <err.h> +#include <errno.h> #include <stdio.h> #include <string.h> #include "dagfile.h" @@ -83,7 +85,10 @@ filter: FILTER STRING { push_filter($2); } struct dagfile * parse_dagfile(FILE *file) { - dagfile = malloc(sizeof(struct dagfile)); + if ((dagfile = malloc(sizeof(struct dagfile))) == NULL) { + err(errno, "malloc failed"); + } + dagfile->target = NULL; t = NULL; e = NULL; @@ -103,8 +108,14 @@ push_target(char *string) exit(1); } - t = malloc(sizeof(struct target)); - t->path = strdup(unquote(string)); + if ((t = malloc(sizeof(struct target))) == NULL) { + err(errno, "malloc failed"); + } + + if ((t->path = strdup(unquote(string))) == NULL) { + err(errno, "strdup failed"); + } + t->sources = NULL; dagfile->target = t; } @@ -112,8 +123,14 @@ push_target(char *string) void push_source(char *string) { - so = malloc(sizeof(struct source)); - so->path = strdup(unquote(string)); + if ((so = malloc(sizeof(struct source))) == NULL) { + err(errno, "malloc failed"); + } + + if ((so->path = strdup(unquote(string))) == NULL) { + err(errno, "strdup failed"); + } + so->extensions = NULL; so->next = NULL; @@ -133,8 +150,14 @@ push_source(char *string) void push_extension(char *string) { - e = malloc(sizeof(struct extension)); - e->value = strdup(unquote(string)); + if ((e = malloc(sizeof(struct extension))) == NULL) { + err(errno, "malloc failed"); + } + + if ((e->value = strdup(unquote(string))) == NULL) { + err(errno, "strdup failed"); + } + e->suffixes = NULL; e->next = NULL; @@ -154,8 +177,14 @@ push_extension(char *string) void push_suffix(char *string) { - s = malloc(sizeof(struct suffix)); - s->value = strdup(unquote(string)); + if ((s = malloc(sizeof(struct suffix))) == NULL) { + err(errno, "malloc failed"); + } + + if ((s->value = strdup(unquote(string))) == NULL) { + err(errno, "strdup failed"); + } + s->requirements = NULL; s->filters = NULL; s->next = NULL; @@ -176,8 +205,15 @@ push_suffix(char *string) void push_requirement(char *string) { - struct requirement *r = malloc(sizeof(struct requirement)); - r->path = strdup(unquote(string)); + struct requirement *r; + if ((r = malloc(sizeof(struct requirement))) == NULL) { + err(errno, "malloc failed"); + } + + if ((r->path = strdup(unquote(string))) == NULL) { + err(errno, "strdup failed"); + } + r->next = NULL; if (s->requirements == NULL) { @@ -196,8 +232,15 @@ push_requirement(char *string) void push_filter(char *string) { - struct filter *f = malloc(sizeof(struct filter)); - f->cmd = strdup(unquote(string)); + struct filter *f; + if ((f = malloc(sizeof(struct filter))) == NULL) { + err(errno, "malloc failed"); + } + + if ((f->cmd = strdup(unquote(string))) == NULL) { + err(errno, "strdup failed"); + } + f->next = NULL; if (s->filters == NULL) {