17aa40
From 81b967279f6e23474b1e7a0ea9b4ecf9405f87bb Mon Sep 17 00:00:00 2001
17aa40
From: Masahiro Matsuya <mmatsuya@redhat.com>
17aa40
Date: Wed, 31 Mar 2021 11:44:24 +0900
17aa40
Subject: [PATCH] tmpfiles: use a entry in hashmap as ItemArray in
17aa40
 read_config_file()
17aa40
17aa40
[zjs: squash commits and use size_t as appropriate.
17aa40
17aa40
Bug seems to have been introduced in 811a15877825da9e53f9a2a8603da34589af6bbb.
17aa40
Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1944468.]
17aa40
17aa40
(cherry picked from commit bec890e3cd6dac249cb12ce9430fdb78b6cf546b)
17aa40
17aa40
Resolves: #1944468
17aa40
---
17aa40
 src/tmpfiles/tmpfiles.c | 47 +++++++++++++++++++++++------------------
17aa40
 1 file changed, 26 insertions(+), 21 deletions(-)
17aa40
17aa40
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
17aa40
index 927de35f32..1aeeed0d2e 100644
17aa40
--- a/src/tmpfiles/tmpfiles.c
17aa40
+++ b/src/tmpfiles/tmpfiles.c
17aa40
@@ -2646,7 +2646,7 @@ static int read_config_file(char **config_dirs, const char *fn, bool ignore_enoe
17aa40
         char line[LINE_MAX];
17aa40
         Iterator iterator;
17aa40
         unsigned v = 0;
17aa40
-        Item *i;
17aa40
+        ItemArray *ia;
17aa40
         int r = 0;
17aa40
 
17aa40
         assert(fn);
17aa40
@@ -2692,32 +2692,37 @@ static int read_config_file(char **config_dirs, const char *fn, bool ignore_enoe
17aa40
         }
17aa40
 
17aa40
         /* we have to determine age parameter for each entry of type X */
17aa40
-        ORDERED_HASHMAP_FOREACH(i, globs, iterator) {
17aa40
-                Iterator iter;
17aa40
-                Item *j, *candidate_item = NULL;
17aa40
+        ORDERED_HASHMAP_FOREACH(ia, globs, iterator)
17aa40
+                for (size_t ni = 0; ni < ia->count; ni++) {
17aa40
+                        Iterator iter;
17aa40
+                        ItemArray *ja;
17aa40
+                        Item *i = ia->items + ni, *candidate_item = NULL;
17aa40
 
17aa40
-                if (i->type != IGNORE_DIRECTORY_PATH)
17aa40
-                        continue;
17aa40
-
17aa40
-                ORDERED_HASHMAP_FOREACH(j, items, iter) {
17aa40
-                        if (!IN_SET(j->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA))
17aa40
+                        if (i->type != IGNORE_DIRECTORY_PATH)
17aa40
                                 continue;
17aa40
 
17aa40
-                        if (path_equal(j->path, i->path)) {
17aa40
-                                candidate_item = j;
17aa40
-                                break;
17aa40
-                        }
17aa40
+                        ORDERED_HASHMAP_FOREACH(ja, items, iter)
17aa40
+                                for (size_t nj = 0; nj < ja->count; nj++) {
17aa40
+                                        Item *j = ja->items + nj;
17aa40
 
17aa40
-                        if ((!candidate_item && path_startswith(i->path, j->path)) ||
17aa40
-                            (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
17aa40
-                                candidate_item = j;
17aa40
-                }
17aa40
+                                        if (!IN_SET(j->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA))
17aa40
+                                                continue;
17aa40
 
17aa40
-                if (candidate_item && candidate_item->age_set) {
17aa40
-                        i->age = candidate_item->age;
17aa40
-                        i->age_set = true;
17aa40
+                                        if (path_equal(j->path, i->path)) {
17aa40
+                                                candidate_item = j;
17aa40
+                                                break;
17aa40
+                                        }
17aa40
+
17aa40
+                                        if ((!candidate_item && path_startswith(i->path, j->path)) ||
17aa40
+                                            (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
17aa40
+                                                candidate_item = j;
17aa40
+                                }
17aa40
+
17aa40
+                        if (candidate_item && candidate_item->age_set) {
17aa40
+                                i->age = candidate_item->age;
17aa40
+                                i->age_set = true;
17aa40
+                        }
17aa40
                 }
17aa40
-        }
17aa40
 
17aa40
         if (ferror(f)) {
17aa40
                 log_error_errno(errno, "Failed to read from file %s: %m", fn);