From d298747580c704ba4385e2ec45510de11cf226a4 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 20 Mar 2024 19:55:38 -0500 Subject: [PATCH] If /sys isn't mounted or the loop device is disassociated between reading /proc/mounts and asking for follow-up data from sysfs, readfile() returns NULL and there should have been an else case setting it back to "" instead of NULL so the printf() and if (!*ss) free(ss) didn't try to dereference the NULL. That said, the initial "" was only specutavely replaced and then needing to be put back because code really wants a third variable, and I can re-use s if I move the xabspath() down a bit, so... --- toys/lsb/mount.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/toys/lsb/mount.c b/toys/lsb/mount.c index 2b397f34..ff0b7e12 100644 --- a/toys/lsb/mount.c +++ b/toys/lsb/mount.c @@ -373,24 +373,24 @@ void mount_main(void) // show mounts from /proc/mounts } else if (!dev) { for (mtl = xgetmountlist(0); mtl && (mm = dlist_pop(&mtl)); free(mm)) { - char *s = mm->device, *ss = ""; + char *s = mm->device, *ss = "", *temp; struct stat st; if (TT.t && strcmp(TT.t, mm->type)) continue; if (*s == '/') { - s = xabspath(mm->device, 0); - if (!stat(s, &st) && S_ISBLK(st.st_mode) &&dev_major(st.st_rdev)==7) { - char *temp = xmprintf("/sys/block/loop%d/loop/backing_file", + if (!stat(mm->device, &st) && S_ISBLK(st.st_mode) && + dev_major(st.st_rdev)==7) + { + temp = xmprintf("/sys/block/loop%d/loop/backing_file", dev_minor(st.st_rdev)); - - ss = chomp(readfile(temp, 0, 0)); + s = chomp(readfile(temp, 0, 0)); free(temp); - if (ss) { - temp = xmprintf(",file=%s"+!*mm->opts, ss); - free(ss); - ss = temp; - } + if (s) { + ss = xmprintf(",file=%s"+!*mm->opts, s); + free(s); + }; } + s = xabspath(mm->device, 0); } xprintf("%s on %s type %s (%s%s)\n", s, mm->dir, mm->type, mm->opts, ss); if (s != mm->device) free(s); -- 2.39.2