From: Karel Zak Date: Tue, 24 Jan 2023 17:35:21 +0000 (+0100) Subject: libmount: don't require return argument in get-like functions X-Git-Tag: v2.39-rc1~116 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6729c112f73d04232db8a976b335e5eab2bd519e;p=thirdparty%2Futil-linux.git libmount: don't require return argument in get-like functions Fixes: https://github.com/util-linux/util-linux/issues/2039 Signed-off-by: Karel Zak --- diff --git a/libmount/src/monitor.c b/libmount/src/monitor.c index dada02ed39..f99751e71e 100644 --- a/libmount/src/monitor.c +++ b/libmount/src/monitor.c @@ -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; } diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index 964b896735..438ccfa7ef 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -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) diff --git a/libmount/src/optlist.c b/libmount/src/optlist.c index a226028ee8..d0243d9039 100644 --- a/libmount/src/optlist.c +++ b/libmount/src/optlist.c @@ -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; } diff --git a/libmount/src/tab.c b/libmount/src/tab.c index 0133ff6185..a7da09481c 100644 --- a/libmount/src/tab.c +++ b/libmount/src/tab.c @@ -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) diff --git a/libmount/src/tab_diff.c b/libmount/src/tab_diff.c index 81694bc6df..d5fbc4d49c 100644 --- a/libmount/src/tab_diff.c +++ b/libmount/src/tab_diff.c @@ -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; }