]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
send_list: avoid memory allocation by using openat
authorBaptiste Daroussin <bapt@FreeBSD.org>
Tue, 4 Jan 2022 21:04:19 +0000 (22:04 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Tue, 4 Jan 2022 21:05:14 +0000 (22:05 +0100)
src/send_list.c

index 19906ee8ac1619a1bef0065de0ce47079731a58a..4e75e3b33a0bb8cd01bb804c2ab79ba9f725c0b1 100644 (file)
@@ -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;
        }
 }