From 616fa9c7b335117243c0c157a6c45445e20fdfa0 Mon Sep 17 00:00:00 2001 From: Pádraig Brady Date: Tue, 9 Jul 2013 00:46:39 +0100 Subject: df: fix mount list processing with unstatable mount dirs * src/df.c (filter_mount_list): Initialize devlist->dev_num correctly when unable to stat() a mount point. This will avoid possible invalid deduplication done on the list due to use of uninitialized memory. * tests/df/skip-duplicates.sh: Ensure this code path is exercised. Also refactor the test to be table driven. * NEWS: Mention the bug fix. --- tests/df/skip-duplicates.sh | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'tests') diff --git a/tests/df/skip-duplicates.sh b/tests/df/skip-duplicates.sh index 2b7de562b..1e94dc0b2 100755 --- a/tests/df/skip-duplicates.sh +++ b/tests/df/skip-duplicates.sh @@ -24,8 +24,10 @@ require_gcc_shared_ df || skip_ "df fails" # Simulate an mtab file with two entries of the same device number. +# Also add entries with unstatable mount dirs to ensure that's handled. cat > k.c <<'EOF' || framework_failure_ #include +#include #include struct mntent *getmntent (FILE *fp) @@ -38,20 +40,20 @@ struct mntent *getmntent (FILE *fp) ++done; } - static struct mntent mntent; + static struct mntent mntents[] = { + {.mnt_fsname="/short", .mnt_dir="/invalid/mount/dir"}, + {.mnt_fsname="fsname", .mnt_dir="/",}, + {.mnt_fsname="/fsname", .mnt_dir="/root"}, + {.mnt_fsname="/fsname", .mnt_dir="/"}, + }; - while (done++ < 4) + if (!getenv ("CU_TEST_DUPE_INVALID") && done == 1) + done++; /* skip the first entry. */ + + while (done++ <= 4) { - /* File system - Mounted on - fsname / - /fsname /root - /fsname / - */ - mntent.mnt_fsname = (done == 2) ? "fsname" : "/fsname"; - mntent.mnt_dir = (done == 3) ? "/root" : "/"; - mntent.mnt_type = "-"; - - return &mntent; + mntents[done-2].mnt_type = "-"; + return &mntents[done-2]; } return NULL; } @@ -65,12 +67,16 @@ gcc --std=gnu99 -shared -fPIC -ldl -O2 k.c -o k.so \ LD_PRELOAD=./k.so df test -f x || skip_ "internal test failure: maybe LD_PRELOAD doesn't work?" -# The fake mtab file should only contain 2 entries, both +# The fake mtab file should only contain entries # having the same device number; thus the output should # consist of a header and one entry. LD_PRELOAD=./k.so df >out || fail=1 test $(wc -l out && fail=1 +test $(wc -l