From: Baptiste Daroussin Date: Wed, 3 Nov 2021 13:43:07 +0000 (+0100) Subject: text: use file descriptors when possible to avoid manipulating memory X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fa9cc3a3170c962896a6ba806649126d4381da61;p=thirdparty%2Fmlmmj.git text: use file descriptors when possible to avoid manipulating memory --- diff --git a/include/mlmmj.h b/include/mlmmj.h index 797e47a8..5fb05dc9 100644 --- a/include/mlmmj.h +++ b/include/mlmmj.h @@ -65,6 +65,10 @@ struct mlmmj_list { int fd; int controlfd; int textfd; + int defaulttextfd; + int entextfd; + const char *defaulttext; + const char *entext; char *addr; char *delim; char *name; diff --git a/src/mlmmj.c b/src/mlmmj.c index f8a036de..e169b1c5 100644 --- a/src/mlmmj.c +++ b/src/mlmmj.c @@ -38,6 +38,10 @@ mlmmj_list_init(struct mlmmj_list *list) list->fd = -1; list->controlfd = -1; list->textfd = -1; + list->defaulttextfd = -1; + list->entextfd = -1; + list->defaulttext = DEFAULTTEXTDIR "/default"; + list->entext = DEFAULTTEXTDIR "/en"; list->delim = NULL; list->addr = NULL; list->fqdn = NULL; diff --git a/src/prepstdreply.c b/src/prepstdreply.c index a020eb66..0e5fef6a 100644 --- a/src/prepstdreply.c +++ b/src/prepstdreply.c @@ -31,6 +31,7 @@ #include #include #include +#include #include "prepstdreply.h" #include "controls.h" @@ -452,9 +453,22 @@ char *substitute(const char *line, struct mlmmj_list *list, text *txt) } +static bool +open_textdirs(struct mlmmj_list *list) +{ + if (list->textfd == -1) + list->textfd = openat(list->fd, "text", O_DIRECTORY|O_CLOEXEC); + if (list->defaulttextfd == -1) + list->defaulttextfd = openat(list->fd, list->defaulttext, O_DIRECTORY|O_CLOEXEC); + if (list->entextfd == -1) + list->entextfd = openat(list->fd, list->entext, O_DIRECTORY|O_CLOEXEC); + if (list->textfd == -1 && list->defaulttextfd == -1 && list->entextfd == -1) + return (false); + return (true); +} + text *open_text_file(struct mlmmj_list *list, const char *filename) { - char *tmp; text *txt; txt = mymalloc(sizeof(text)); @@ -484,19 +498,18 @@ text *open_text_file(struct mlmmj_list *list, const char *filename) txt->cond = NULL; txt->skip = NULL; - myasprintf(&tmp, "text/%s", filename); - txt->src->fd = openat(list->fd, tmp, O_RDONLY); - myfree(tmp); + if (!open_textdirs(list)) { + warnx("Unable to open any text directory"); + return (NULL); + } + + txt->src->fd = openat(list->textfd, filename, O_RDONLY); if (txt->src->fd >= 0) return txt; - tmp = concatstr(2, DEFAULTTEXTDIR "/default/", filename); - txt->src->fd = open(tmp, O_RDONLY); - myfree(tmp); + txt->src->fd = openat(list->defaulttextfd, filename, O_RDONLY); if (txt->src->fd >= 0) return txt; - tmp = concatstr(2, DEFAULTTEXTDIR "/en/", filename); - txt->src->fd = open(tmp, O_RDONLY); - myfree(tmp); + txt->src->fd = openat(list->entextfd, filename, O_RDONLY); if (txt->src->fd >= 0) return txt; return NULL;