From: Baptiste Daroussin Date: Tue, 4 Jan 2022 21:04:19 +0000 (+0100) Subject: send_list: avoid memory allocation by using openat X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=da11452f16937bb2c9d046cd535e85a8ca54a700;p=thirdparty%2Fmlmmj.git send_list: avoid memory allocation by using openat --- diff --git a/src/send_list.c b/src/send_list.c index 19906ee8..4e75e3b3 100644 --- a/src/send_list.c +++ b/src/send_list.c @@ -33,7 +33,6 @@ #include "xmalloc.h" #include "mlmmj.h" #include "send_list.h" -#include "strgen.h" #include "log_error.h" #include "chomp.h" #include "mygetline.h" @@ -92,11 +91,13 @@ static void rewind_subs_list(void *state) static const char *get_sub(void *state) { subs_list_state *s = (subs_list_state *)state; - char *filename; struct dirent *dp; + int fd; if (s == NULL) return NULL; if (s->dirp == NULL) return NULL; + fd = open(s->dirname, O_DIRECTORY); + if (fd == -1) return NULL; if (s->line != NULL) { free(s->line); @@ -109,21 +110,19 @@ static const char *get_sub(void *state) if (dp == NULL) { closedir(s->dirp); s->dirp = NULL; + close(fd); return NULL; } if ((strcmp(dp->d_name, "..") == 0) || (strcmp(dp->d_name, ".") == 0)) continue; - filename = concatstr(2, s->dirname, dp->d_name); - s->fd = open(filename, O_RDONLY); + s->fd = openat(fd, dp->d_name, O_RDONLY); if(s->fd < 0) { log_error(LOG_ARGS, - "Could not open %s for reading", - filename); - free(filename); + "Could not open %s/%s for reading", + s->dirname, dp->d_name); continue; } - free(filename); } s->line = mygetline(s->fd); if (s->line == NULL) { @@ -132,6 +131,7 @@ static const char *get_sub(void *state) continue; } chomp(s->line); + close(fd); return s->line; } }