[systemd-devel] [PATCH 2/2] journald: fix fd leak in journal_file_empty

George McCollister george.mccollister at gmail.com
Wed Sep 4 05:12:44 PDT 2013


Before my previous patch, journal_file_empty wasn't be called with the
correct filename. Now that it's being called with the correct filename
it leaks file descriptors. This patch closes the file descriptors before
returning.

Signed-off-by: George McCollister <george.mccollister at gmail.com>
---
 src/journal/journal-vacuum.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/journal/journal-vacuum.c b/src/journal/journal-vacuum.c
index ace7722..2cfc73c 100644
--- a/src/journal/journal-vacuum.c
+++ b/src/journal/journal-vacuum.c
@@ -136,12 +136,18 @@ static int journal_file_empty(int dir_fd, const char *name) {
         if (fd < 0)
                 return -errno;
 
-        if (lseek(fd, offsetof(Header, n_entries), SEEK_SET) < 0)
+        if (lseek(fd, offsetof(Header, n_entries), SEEK_SET) < 0) {
+                close(fd);
                 return -errno;
+        }
 
         r = read(fd, &n_entries, sizeof(n_entries));
-        if (r != sizeof(n_entries))
+        if (r != sizeof(n_entries)) {
+                close(fd);
                 return r == 0 ? -EINVAL : -errno;
+        }
+
+        close(fd);
 
         return le64toh(n_entries) == 0;
 }
-- 
1.8.2.1



More information about the systemd-devel mailing list