bug-coreutils
[Top][All Lists]
Advanced

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

Re: [PATCH] ls: don't use an undefined struct stat after failed stat/lst


From: Pádraig Brady
Subject: Re: [PATCH] ls: don't use an undefined struct stat after failed stat/lstat
Date: Wed, 30 Sep 2009 12:46:35 +0100
User-agent: Thunderbird 2.0.0.6 (X11/20071008)

Jim Meyering wrote:
> 
> Thanks for the review.
> BTW, here's the merged version:

And attached one handles the `ls -Ls` case,
which I'll push soon.

cheers,
Pádraig.
>From 3edaa2363db0367e8fa472c4dc5c5696537cde61 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?P=C3=A1draig=20Brady?= <address@hidden>
Date: Tue, 29 Sep 2009 15:43:01 +0100
Subject: [PATCH] ls: always print "?" for allocated size of a dereferenced 
dangling symlink

Previously for `ls -Ls` (but not `ls -Lsl`), we referenced
the st_blocks returned from the previous failed stat() call.
This undefined value was seen to be 0 for danglink symlinks at least.
* src/ls.c (print_file_name_and_frills, length_of_file_name_and_frills):
Don't use st_blocks if the previous stat() failed
* tests/ls/dangle: Add a test case
* NEWS: Mention the bug fix
---
 NEWS            |    3 +++
 src/ls.c        |   12 +++++++-----
 tests/ls/dangle |   10 ++++++++--
 3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/NEWS b/NEWS
index f1f7347..e6a860c 100644
--- a/NEWS
+++ b/NEWS
@@ -25,6 +25,9 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   Before this change, "ls -Li dangling-symlink" would mistakenly
   print the inode number of the symlink under some conditions.
 
+  ls -Ls is now consistent with ls -Lsl in printing "?", not "0" as the
+  allocated size of a dangling symlink.
+
 ** Portability
 
   On Solaris 9, many commands would mistakenly treat file/ the same as
diff --git a/src/ls.c b/src/ls.c
index 801e717..30df92c 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -4016,8 +4016,9 @@ print_file_name_and_frills (const struct fileinfo *f, 
size_t start_col)
 
   if (print_block_size)
     printf ("%*s ", format == with_commas ? 0 : block_size_width,
-            human_readable (ST_NBLOCKS (f->stat), buf, human_output_opts,
-                            ST_NBLOCKSIZE, output_block_size));
+            ! f->stat_ok ? "?"
+            : human_readable (ST_NBLOCKS (f->stat), buf, human_output_opts,
+                              ST_NBLOCKSIZE, output_block_size));
 
   if (print_scontext)
     printf ("%*s ", format == with_commas ? 0 : scontext_width, f->scontext);
@@ -4234,9 +4235,10 @@ length_of_file_name_and_frills (const struct fileinfo *f)
 
   if (print_block_size)
     len += 1 + (format == with_commas
-                ? strlen (human_readable (ST_NBLOCKS (f->stat), buf,
-                                          human_output_opts, ST_NBLOCKSIZE,
-                                          output_block_size))
+                ? strlen (! f->stat_ok ? "?"
+                          : human_readable (ST_NBLOCKS (f->stat), buf,
+                                            human_output_opts, ST_NBLOCKSIZE,
+                                            output_block_size))
                 : block_size_width);
 
   if (print_scontext)
diff --git a/tests/ls/dangle b/tests/ls/dangle
index 6abad92..687d3df 100755
--- a/tests/ls/dangle
+++ b/tests/ls/dangle
@@ -28,7 +28,8 @@ mkdir -p dir/sub || framework_failure
 ln -s dir slink-to-dir || framework_failure
 mkdir d || framework_failure
 ln -s no-such d/dangle || framework_failure
-echo '? dangle' > subdir_exp || framework_failure
+printf '? dangle\n' > subdir_Li_exp || framework_failure
+printf 'total 0\n? dangle\n' > subdir_Ls_exp || framework_failure
 
 fail=0
 
@@ -56,6 +57,11 @@ compare out exp || fail=1
 # Ensure that ls -Li prints "?" as the inode of a dangling symlink.
 rm -f out
 ls -Li d > out 2>/dev/null && fail=1
-compare out subdir_exp || fail=1
+compare out subdir_Li_exp || fail=1
+
+# Ensure that ls -Ls prints "?" as the allocation of a dangling symlink.
+rm -f out
+ls -Ls d > out 2>/dev/null && fail=1
+compare out subdir_Ls_exp || fail=1
 
 Exit $fail
-- 
1.6.2.5


reply via email to

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