[systemd-devel] [PATCH 2/2] journal, readahead: remove file handle leaks

Sami Kerola kerolasa at iki.fi
Sun Jan 27 13:51:12 PST 2013


Found with the coccicheck-0.2.4 open file check.
---
 src/journal/catalog.c            |  8 +++++++-
 src/readahead/readahead-common.c | 10 +++++++---
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/journal/catalog.c b/src/journal/catalog.c
index 3735ad9..f89b309 100644
--- a/src/journal/catalog.c
+++ b/src/journal/catalog.c
@@ -165,6 +165,7 @@ static int import_file(Hashmap *h, struct strbuf *sb, const char *path) {
                                 break;
 
                         log_error("Failed to read file %s: %m", path);
+                        fclose(f);
                         return -errno;
                 }
 
@@ -199,8 +200,10 @@ static int import_file(Hashmap *h, struct strbuf *sb, const char *path) {
 
                                 if (got_id) {
                                         r = finish_item(h, sb, id, language, payload);
-                                        if (r < 0)
+                                        if (r < 0) {
+                                                fclose(f);
                                                 return r;
+                                        }
                                 }
 
                                 if (with_language) {
@@ -209,10 +212,12 @@ static int import_file(Hashmap *h, struct strbuf *sb, const char *path) {
                                         c = strlen(t);
                                         if (c <= 0) {
                                                 log_error("[%s:%u] Language too short.", path, n);
+                                                fclose(f);
                                                 return -EINVAL;
                                         }
                                         if (c > sizeof(language)) {
                                                 log_error("[%s:%u] language too long.", path, n);
+                                                fclose(f);
                                                 return -EINVAL;
                                         }
 
@@ -230,6 +235,7 @@ static int import_file(Hashmap *h, struct strbuf *sb, const char *path) {
                                 continue;
                         }
                 }
+                fclose(f);
 
                 /* Payload */
                 if (!got_id) {
diff --git a/src/readahead/readahead-common.c b/src/readahead/readahead-common.c
index e8e4d45..4365b6f 100644
--- a/src/readahead/readahead-common.c
+++ b/src/readahead/readahead-common.c
@@ -96,8 +96,10 @@ int fs_on_ssd(const char *p) {
                         _cleanup_free_ char *opts = NULL;
                         int mid;
 
-                        if (!fgets(line, sizeof(line), f))
+                        if (!fgets(line, sizeof(line), f)) {
+                                fclose(f);
                                 return false;
+                        }
 
                         truncate_nl(line);
 
@@ -114,10 +116,12 @@ int fs_on_ssd(const char *p) {
                         if (sscanf(e+3, "%*s %*s %ms", &opts) != 1)
                                 continue;
 
-                        if (streq(opts, "ssd") || startswith(opts, "ssd,") || endswith(opts, ",ssd") || strstr(opts, ",ssd,"))
+                        if (streq(opts, "ssd") || startswith(opts, "ssd,") || endswith(opts, ",ssd") || strstr(opts, ",ssd,")) {
+                                fclose(f);
                                 return true;
+                        }
                 }
-
+                fclose(f);
                 return false;
         }
 
-- 
1.8.1.1



More information about the systemd-devel mailing list