From e548d975a79204ab88ab6638aa5b24c173402723 Mon Sep 17 00:00:00 2001
From: David Tardon <dtardon@redhat.com>
Date: Wed, 22 Mar 2023 12:53:20 +0100
Subject: [PATCH] journal-vacuum: use CLEANUP_ARRAY

(cherry picked from commit 567cb18cc9185900ac6f701f0783a7d378e213cf)

Related: #2182632
---
 src/libsystemd/sd-journal/journal-vacuum.c | 41 +++++++++++-----------
 1 file changed, 20 insertions(+), 21 deletions(-)

diff --git a/src/libsystemd/sd-journal/journal-vacuum.c b/src/libsystemd/sd-journal/journal-vacuum.c
index e3d691a1e9..6f8aaaee4c 100644
--- a/src/libsystemd/sd-journal/journal-vacuum.c
+++ b/src/libsystemd/sd-journal/journal-vacuum.c
@@ -47,6 +47,16 @@ static int vacuum_info_compare(const vacuum_info *a, const vacuum_info *b) {
         return strcmp(a->filename, b->filename);
 }
 
+static void vacuum_info_array_free(vacuum_info *list, size_t n) {
+        if (!list)
+                return;
+
+        FOREACH_ARRAY(i, list, n)
+                free(i->filename);
+
+        free(list);
+}
+
 static void patch_realtime(
                 int fd,
                 const char *fn,
@@ -129,6 +139,8 @@ int journal_directory_vacuum(
         usec_t retention_limit = 0;
         int r;
 
+        CLEANUP_ARRAY(list, n_list, vacuum_info_array_free);
+
         assert(directory);
 
         if (max_use <= 0 && max_retention_usec <= 0 && n_max_files <= 0)
@@ -141,7 +153,7 @@ int journal_directory_vacuum(
         if (!d)
                 return -errno;
 
-        FOREACH_DIRENT_ALL(de, d, r = -errno; goto finish) {
+        FOREACH_DIRENT_ALL(de, d, return -errno) {
                 unsigned long long seqnum = 0, realtime;
                 _cleanup_free_ char *p = NULL;
                 sd_id128_t seqnum_id;
@@ -182,10 +194,8 @@ int journal_directory_vacuum(
                         }
 
                         p = strdup(de->d_name);
-                        if (!p) {
-                                r = -ENOMEM;
-                                goto finish;
-                        }
+                        if (!p)
+                                return -ENOMEM;
 
                         de->d_name[q-8-16-1-16-1] = 0;
                         if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) {
@@ -224,10 +234,8 @@ int journal_directory_vacuum(
                         }
 
                         p = strdup(de->d_name);
-                        if (!p) {
-                                r = -ENOMEM;
-                                goto finish;
-                        }
+                        if (!p)
+                                return -ENOMEM;
 
                         if (sscanf(de->d_name + q-1-8-16-1-16, "%16llx-%16llx.journal~", &realtime, &tmp) != 2) {
                                 n_active_files++;
@@ -265,10 +273,8 @@ int journal_directory_vacuum(
 
                 patch_realtime(dirfd(d), p, &st, &realtime);
 
-                if (!GREEDY_REALLOC(list, n_list + 1)) {
-                        r = -ENOMEM;
-                        goto finish;
-                }
+                if (!GREEDY_REALLOC(list, n_list + 1))
+                        return -ENOMEM;
 
                 list[n_list++] = (vacuum_info) {
                         .filename = TAKE_PTR(p),
@@ -312,15 +318,8 @@ int journal_directory_vacuum(
         if (oldest_usec && i < n_list && (*oldest_usec == 0 || list[i].realtime < *oldest_usec))
                 *oldest_usec = list[i].realtime;
 
-        r = 0;
-
-finish:
-        for (i = 0; i < n_list; i++)
-                free(list[i].filename);
-        free(list);
-
         log_full(verbose ? LOG_INFO : LOG_DEBUG, "Vacuuming done, freed %s of archived journals from %s.",
                  FORMAT_BYTES(freed), directory);
 
-        return r;
+        return 0;
 }