coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] df: fix mount list processing with unstatable mount dirs


From: Pádraig Brady
Subject: [PATCH] df: fix mount list processing with unstatable mount dirs
Date: Tue, 9 Jul 2013 02:47:22 +0100

* 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.
---
 NEWS                        |    4 ++++
 src/df.c                    |    3 ++-
 tests/df/skip-duplicates.sh |   32 +++++++++++++++++++-------------
 3 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/NEWS b/NEWS
index 75ec253..53c9f4a 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,10 @@ GNU coreutils NEWS                                    -*- 
outline -*-
 
 ** Bug fixes
 
+  df now processes the mount list correctly in the presence of unstatable
+  mount points.  Previously it may have failed to output some mount points.
+  [bug introduced in coreutils-8.21]
+
   install now removes the target file if the strip program failed for any
   reason.  Before, that file was left behind, sometimes even with wrong
   permissions.
diff --git a/src/df.c b/src/df.c
index 0515131..bc507c7 100644
--- a/src/df.c
+++ b/src/df.c
@@ -620,7 +620,8 @@ filter_mount_list (void)
 
       if (-1 == stat (me->me_mountdir, &buf))
         {
-          ;  /* Stat failed - add ME to be able to complain about it later.  */
+          /* Stat failed - add ME to be able to complain about it later.  */
+          buf.st_dev = me->me_dev;
         }
       else
         {
diff --git a/tests/df/skip-duplicates.sh b/tests/df/skip-duplicates.sh
index 2b7de56..1e94dc0 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 <stdio.h>
+#include <stdlib.h>
 #include <mntent.h>
 
 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) -eq 2 || { fail=1; cat out; }
 
+# Ensure we fail when unable to stat invalid entries
+LD_PRELOAD=./k.so CU_TEST_DUPE_INVALID=1 df >out && fail=1
+test $(wc -l <out) -eq 2 || { fail=1; cat out; }
+
 # df should also prefer "/fsname" over "fsname"
 test $(grep -c '/fsname' <out) -eq 1 || { fail=1; cat out; }
 # ... and "/fsname" with '/' as Mounted on over '/root'
-- 
1.7.7.6




reply via email to

[Prev in Thread] Current Thread [Next in Thread]