From: Karel Zak Date: Wed, 9 Apr 2025 08:51:59 +0000 (+0200) Subject: swapoff: clean up tag resolution X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=702f6f0178fa74cf66a13813bd40e5c7839cfe6a;p=thirdparty%2Futil-linux.git swapoff: clean up tag resolution - Rename swapoff_resolve_tag() to resolve_swapfile_tag() and retain only code relevant to swapfiles. - Always call mnt_resolve_*() before resolve_swapfile_tag() to resolve tags/paths on standard block devices. - Call free() for resolve_swapfile_tag() to avoid memory leaks. Signed-off-by: Karel Zak --- diff --git a/sys-utils/swapoff.c b/sys-utils/swapoff.c index 5677d87a1..ab7e4ca11 100644 --- a/sys-utils/swapoff.c +++ b/sys-utils/swapoff.c @@ -54,20 +54,13 @@ static int all; * mnt_resolve_tag() and mnt_resolve_spec() works with system visible block * devices only. */ -static char *swapoff_resolve_tag(const char *name, const char *value, - struct libmnt_cache *cache) +static char *resolve_swapfile_tag(const char *name, const char *value) { char *path; struct libmnt_table *tb; struct libmnt_iter *itr; struct libmnt_fs *fs; - /* this is usual case for block devices (and it's really fast as it uses - * udev /dev/disk/by-* symlinks by default */ - path = mnt_resolve_tag(name, value, cache); - if (path) - return path; - /* try regular files from /proc/swaps */ tb = get_swaps(); if (!tb) @@ -103,6 +96,7 @@ static char *swapoff_resolve_tag(const char *name, const char *value, static int do_swapoff(const char *orig_special, int quiet, int canonic) { const char *special = orig_special; + char *buf = NULL; int rc = SWAPOFF_EX_OK; if (verbose) @@ -113,12 +107,14 @@ static int do_swapoff(const char *orig_special, int quiet, int canonic) special = mnt_resolve_spec(orig_special, mntcache); if (!special && blkid_parse_tag_string(orig_special, &n, &v) == 0) { - special = swapoff_resolve_tag(n, v, mntcache); + special = buf = resolve_swapfile_tag(n, v); free(n); free(v); } - if (!special) - return cannot_find(orig_special); + if (!special) { + rc = cannot_find(orig_special); + goto done; + } } if (swapoff(special) == 0) @@ -140,13 +136,25 @@ static int do_swapoff(const char *orig_special, int quiet, int canonic) } } +done: + free(buf); return rc; } static int swapoff_by(const char *name, const char *value, int quiet) { - const char *special = swapoff_resolve_tag(name, value, mntcache); - return special ? do_swapoff(special, quiet, CANONIC) : cannot_find(value); + const char *special; + char *buf = NULL; + int rc; + + special = mnt_resolve_tag(name, value, mntcache); + if (!special) + special = buf = resolve_swapfile_tag(name, value); + + rc = special ? do_swapoff(special, quiet, CANONIC) : cannot_find(value); + free(buf); + + return rc; } static void __attribute__((__noreturn__)) usage(void)