ls: don't use an undefined struct stat after failed stat/lstat
authorJim Meyering <meyering@redhat.com>
Tue, 29 Sep 2009 05:28:01 +0000 (07:28 +0200)
committerJim Meyering <meyering@redhat.com>
Tue, 29 Sep 2009 12:12:30 +0000 (14:12 +0200)
* src/ls.c (format_inode): Access f->stat only if f->stat_ok is set.
* NEWS (Bug fixes): Mention it.
Improved-by: Pádraig Brady <P@draigBrady.com>
NEWS
src/ls.c

diff --git a/NEWS b/NEWS
index 68ac24b..075c0fa 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,10 @@ GNU coreutils NEWS                                    -*- outline -*-
   ls -Li is now consistent with ls -Lil in printing "?", not "0" as the
   inode of a dangling symlink.
 
+  ls -Li no longer relies on unspecified behavior of stat/lstat.
+  Before this change, "ls -Li dangling-symlink" would mistakenly
+  print the inode number of the symlink under some conditions.
+
 ** Portability
 
   On Solaris 9, many commands would mistakenly treat file/ the same as
index c8e8abb..801e717 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -3563,9 +3563,9 @@ static char *
 format_inode (char *buf, size_t buflen, const struct fileinfo *f)
 {
   assert (INT_BUFSIZE_BOUND (uintmax_t) <= buflen);
-  return (f->stat.st_ino == NOT_AN_INODE_NUMBER
-          ? (char *) "?"
-          : umaxtostr (f->stat.st_ino, buf));
+  return (f->stat_ok && f->stat.st_ino != NOT_AN_INODE_NUMBER
+          ? umaxtostr (f->stat.st_ino, buf)
+          : (char *) "?");
 }
 
 /* Print information about F in long format.  */