]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: don't require return argument in get-like functions
authorKarel Zak <kzak@redhat.com>
Tue, 24 Jan 2023 17:35:21 +0000 (18:35 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 24 Jan 2023 17:35:21 +0000 (18:35 +0100)
Fixes: https://github.com/util-linux/util-linux/issues/2039
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/monitor.c
libmount/src/mountP.h
libmount/src/optlist.c
libmount/src/tab.c
libmount/src/tab_diff.c

index dada02ed3910ea2be7ebe6eadadb92159f07bd48..f99751e71e36ddc0bcbfa032baff0e99bce35b65 100644 (file)
@@ -171,14 +171,16 @@ static int monitor_next_entry(struct libmnt_monitor *mn,
 
        assert(mn);
        assert(itr);
-       assert(me);
 
-       *me = NULL;
+       if (me)
+               *me = NULL;
 
        if (!itr->head)
                MNT_ITER_INIT(itr, &mn->ents);
        if (itr->p != itr->head) {
-               MNT_ITER_ITERATE(itr, *me, struct monitor_entry, ents);
+               if (me)
+                       *me = MNT_ITER_GET_ENTRY(itr, struct monitor_entry, ents);
+               MNT_ITER_ITERATE(itr);
                rc = 0;
        }
 
index 964b8967356c57653e2d4cc5186f5600dc3b92cf..438ccfa7efc720a36ca10e2762e5841299a77c23 100644 (file)
@@ -174,9 +174,11 @@ struct libmnt_iter {
                (itr)->head = (list); \
        } while(0)
 
-#define MNT_ITER_ITERATE(itr, res, restype, member) \
+#define MNT_ITER_GET_ENTRY(itr, restype, member) \
+               list_entry((itr)->p, restype, member)
+
+#define MNT_ITER_ITERATE(itr) \
        do { \
-               res = list_entry((itr)->p, restype, member); \
                (itr)->p = IS_ITER_FORWARD(itr) ? \
                                (itr)->p->next : (itr)->p->prev; \
        } while(0)
index a226028ee868ab6dc399242c9bf07c3a07533619..d0243d9039546a62d92c643aa367a344914ff701 100644 (file)
@@ -232,14 +232,17 @@ int mnt_optlist_next_opt(struct libmnt_optlist *ls,
 {
        int rc = 1;
 
-       if (!ls || !itr || !opt)
+       if (!ls || !itr)
                return -EINVAL;
-       *opt = NULL;
+       if (opt)
+               *opt = NULL;
 
        if (!itr->head)
                MNT_ITER_INIT(itr, &ls->opts);
        if (itr->p != itr->head) {
-               MNT_ITER_ITERATE(itr, *opt, struct libmnt_opt, opts);
+               if (opt)
+                       *opt = MNT_ITER_GET_ENTRY(itr, struct libmnt_opt, opts);
+               MNT_ITER_ITERATE(itr);
                rc = 0;
        }
 
index 0133ff6185baff3d5a9321c8aa9c8b34502ac548..a7da09481c2f59a3cc600c1788c145f2cd623802 100644 (file)
@@ -590,7 +590,7 @@ static inline struct libmnt_fs *get_parent_fs(struct libmnt_table *tb, struct li
 /**
  * mnt_table_get_root_fs:
  * @tb: mountinfo file (/proc/self/mountinfo)
- * @root: returns pointer to the root filesystem (/)
+ * @root: NULL or returns pointer to the root filesystem (/)
  *
  * The function uses the parent ID from the mountinfo file to determine the
  * root filesystem (the filesystem with the smallest ID with parent ID missing
@@ -609,37 +609,38 @@ static inline struct libmnt_fs *get_parent_fs(struct libmnt_table *tb, struct li
 int mnt_table_get_root_fs(struct libmnt_table *tb, struct libmnt_fs **root)
 {
        struct libmnt_iter itr;
-       struct libmnt_fs *fs;
+       struct libmnt_fs *fs, *root_fs = NULL;
        int root_id = 0;
 
-       if (!tb || !root || !is_mountinfo(tb))
+       if (!tb || !is_mountinfo(tb))
                return -EINVAL;
 
        DBG(TAB, ul_debugobj(tb, "lookup root fs"));
 
-       *root = NULL;
-
        /* get smallest possible ID from the table */
        mnt_reset_iter(&itr, MNT_ITER_FORWARD);
-       while(mnt_table_next_fs(tb, &itr, &fs) == 0) {
+       while (mnt_table_next_fs(tb, &itr, &fs) == 0) {
                int id = mnt_fs_get_parent_id(fs);
 
-               if (!*root || id < root_id) {
-                       *root = fs;
+               if (!root_fs || id < root_id) {
+                       root_fs = fs;
                        root_id = id;
                }
        }
 
        /* go to the root node by "parent_id -> id" relation */
-       while (*root) {
-               struct libmnt_fs *x = get_parent_fs(tb, *root);
-               if (!x || x == *root)
+       while (root_fs) {
+               struct libmnt_fs *x = get_parent_fs(tb, root_fs);
+               if (!x || x == root_fs)
                        break;
                DBG(TAB, ul_debugobj(tb, " messy mountinfo, walk to %s", mnt_fs_get_target(x)));
-               *root = x;
+               root_fs = x;
        }
 
-       return *root ? 0 : -EINVAL;
+       if (root)
+               *root = root_fs;
+
+       return root_fs ? 0 : -EINVAL;
 }
 
 /**
@@ -647,7 +648,7 @@ int mnt_table_get_root_fs(struct libmnt_table *tb, struct libmnt_fs **root)
  * @tb: mountinfo file (/proc/self/mountinfo)
  * @itr: iterator
  * @parent: parental FS
- * @chld: returns the next child filesystem
+ * @chld: NULL or returns the next child filesystem
  *
  * Note that filesystems are returned in the order of mounting (according to
  * IDs in /proc/self/mountinfo).
@@ -657,7 +658,7 @@ int mnt_table_get_root_fs(struct libmnt_table *tb, struct libmnt_fs **root)
 int mnt_table_next_child_fs(struct libmnt_table *tb, struct libmnt_iter *itr,
                        struct libmnt_fs *parent, struct libmnt_fs **chld)
 {
-       struct libmnt_fs *fs;
+       struct libmnt_fs *fs, *chfs = NULL;
        int parent_id, lastchld_id = 0, chld_id = 0;
 
        if (!tb || !itr || !parent || !is_mountinfo(tb))
@@ -665,19 +666,17 @@ int mnt_table_next_child_fs(struct libmnt_table *tb, struct libmnt_iter *itr,
 
        DBG(TAB, ul_debugobj(tb, "lookup next child of '%s'",
                                mnt_fs_get_target(parent)));
-
        parent_id = mnt_fs_get_id(parent);
 
        /* get ID of the previously returned child */
        if (itr->head && itr->p != itr->head) {
-               MNT_ITER_ITERATE(itr, fs, struct libmnt_fs, ents);
+               fs = MNT_ITER_GET_ENTRY(itr, struct libmnt_fs, ents);
+               MNT_ITER_ITERATE(itr);
                lastchld_id = mnt_fs_get_id(fs);
        }
 
-       *chld = NULL;
-
        mnt_reset_iter(itr, MNT_ITER_FORWARD);
-       while(mnt_table_next_fs(tb, itr, &fs) == 0) {
+       while (mnt_table_next_fs(tb, itr, &fs) == 0) {
                int id;
 
                if (mnt_fs_get_parent_id(fs) != parent_id)
@@ -691,17 +690,19 @@ int mnt_table_next_child_fs(struct libmnt_table *tb, struct libmnt_iter *itr,
                        continue;
 
                if ((!lastchld_id || id > lastchld_id) &&
-                   (!*chld || id < chld_id)) {
-                       *chld = fs;
+                   (!chfs || id < chld_id)) {
+                       chfs = fs;
                        chld_id = id;
                }
        }
 
-       if (!*chld)
+       if (chld)
+               *chld = chfs;
+       if (!chfs)
                return 1;       /* end of iterator */
 
-       /* set the iterator to the @chld for the next call */
-       mnt_table_set_iter(tb, itr, *chld);
+       /* set the iterator to the @chfs for the next call */
+       mnt_table_set_iter(tb, itr, chfs);
 
        return 0;
 }
@@ -765,7 +766,7 @@ int mnt_table_over_fs(struct libmnt_table *tb, struct libmnt_fs *parent,
  * mnt_table_next_fs:
  * @tb: tab pointer
  * @itr: iterator
- * @fs: returns the next tab entry
+ * @fs: NULL or returns the next tab entry
  *
  * Returns: 0 on success, negative number in case of error or 1 at the end of list.
  *
@@ -785,14 +786,17 @@ int mnt_table_next_fs(struct libmnt_table *tb, struct libmnt_iter *itr, struct l
 {
        int rc = 1;
 
-       if (!tb || !itr || !fs)
+       if (!tb || !itr)
                return -EINVAL;
-       *fs = NULL;
+       if (fs)
+               *fs = NULL;
 
        if (!itr->head)
                MNT_ITER_INIT(itr, &tb->ents);
        if (itr->p != itr->head) {
-               MNT_ITER_ITERATE(itr, *fs, struct libmnt_fs, ents);
+               if (fs)
+                       *fs = MNT_ITER_GET_ENTRY(itr, struct libmnt_fs, ents);
+               MNT_ITER_ITERATE(itr);
                rc = 0;
        }
 
@@ -802,34 +806,36 @@ int mnt_table_next_fs(struct libmnt_table *tb, struct libmnt_iter *itr, struct l
 /**
  * mnt_table_first_fs:
  * @tb: tab pointer
- * @fs: returns the first tab entry
+ * @fs: NULL or returns the first tab entry
  *
  * Returns: 0 on success, negative number in case of error or 1 at the end of list.
  */
 int mnt_table_first_fs(struct libmnt_table *tb, struct libmnt_fs **fs)
 {
-       if (!tb || !fs)
+       if (!tb)
                return -EINVAL;
        if (list_empty(&tb->ents))
                return 1;
-       *fs = list_first_entry(&tb->ents, struct libmnt_fs, ents);
+       if (fs)
+               *fs = list_first_entry(&tb->ents, struct libmnt_fs, ents);
        return 0;
 }
 
 /**
  * mnt_table_last_fs:
  * @tb: tab pointer
- * @fs: returns the last tab entry
+ * @fs: NULL or returns the last tab entry
  *
  * Returns: 0 on success, negative number in case of error or 1 at the end of list.
  */
 int mnt_table_last_fs(struct libmnt_table *tb, struct libmnt_fs **fs)
 {
-       if (!tb || !fs)
+       if (!tb)
                return -EINVAL;
        if (list_empty(&tb->ents))
                return 1;
-       *fs = list_last_entry(&tb->ents, struct libmnt_fs, ents);
+       if (fs)
+               *fs = list_last_entry(&tb->ents, struct libmnt_fs, ents);
        return 0;
 }
 
@@ -839,7 +845,7 @@ int mnt_table_last_fs(struct libmnt_table *tb, struct libmnt_fs **fs)
  * @itr: iterator
  * @match_func: function returning 1 or 0
  * @userdata: extra data for match_func
- * @fs: returns pointer to the next matching table entry
+ * @fs: NULL or returns pointer to the next matching table entry
  *
  * This function allows searching in @tb.
  *
@@ -849,26 +855,32 @@ int mnt_table_find_next_fs(struct libmnt_table *tb, struct libmnt_iter *itr,
                int (*match_func)(struct libmnt_fs *, void *), void *userdata,
                struct libmnt_fs **fs)
 {
-       if (!tb || !itr || !fs || !match_func)
+       struct libmnt_fs *re = NULL;
+       int match = 0;
+
+       if (!tb || !itr || !match_func)
                return -EINVAL;
 
        DBG(TAB, ul_debugobj(tb, "lookup next fs"));
 
+       if (fs)
+               *fs = NULL;
        if (!itr->head)
                MNT_ITER_INIT(itr, &tb->ents);
 
-       do {
-               if (itr->p != itr->head)
-                       MNT_ITER_ITERATE(itr, *fs, struct libmnt_fs, ents);
-               else
-                       break;                  /* end */
+       while (!match) {
+               if (itr->p != itr->head) {
+                       re = MNT_ITER_GET_ENTRY(itr, struct libmnt_fs, ents);
+                       MNT_ITER_ITERATE(itr);
+               } else
+                       return 1;       /*end */
 
-               if (match_func(*fs, userdata))
-                       return 0;
-       } while(1);
+               match = match_func(re, userdata);
+       }
 
-       *fs = NULL;
-       return 1;
+       if (fs)
+               *fs = re;
+       return 0;
 }
 
 static int mnt_table_move_parent(struct libmnt_table *tb, int oldid, int newid)
index 81694bc6df495ecb6747fcf4bc7b2a4bfdd1eda0..d5fbc4d49c8d9839650e100bfbf1d63d06a76641 100644 (file)
@@ -110,7 +110,8 @@ int mnt_tabdiff_next_change(struct libmnt_tabdiff *df, struct libmnt_iter *itr,
        if (!itr->head)
                MNT_ITER_INIT(itr, &df->changes);
        if (itr->p != itr->head) {
-               MNT_ITER_ITERATE(itr, de, struct tabdiff_entry, changes);
+               de = MNT_ITER_GET_ENTRY(itr, struct tabdiff_entry, changes);
+               MNT_ITER_ITERATE(itr);
                rc = 0;
        }