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: Tue, 29 Sep 2009 11:48:43 +0100
User-agent: Thunderbird 2.0.0.6 (X11/20071008)

Jim Meyering wrote:
> Nearly missed this one.
> ls initializes a struct stat to all NUL bytes.
> It calls stat or lstat on a dangling symlink, and that fails.
> ls then tests stat.st_ino.
> 
> Sometimes, it's 0, but sometimes it's the inode of the symlink,
> possibly depending on how the package was configured/built or
> the environment.
> I haven't determined precisely what makes the difference,
> but ls (from coreutils-7.6) built some way prints 0 as the inode
> number, and other ways, it prints the inode of the dangling symlink.
> 
> 
>>From f7db178fdff1ebb113841035b55b103e074b5f6f Mon Sep 17 00:00:00 2001
> From: Jim Meyering <address@hidden>
> Date: Tue, 29 Sep 2009 07:28:01 +0200
> Subject: [PATCH] ls: don't use an undefined struct stat after failed 
> stat/lstat
> 
> * src/ls.c (gobble_file): After a failed stat/lstat call,
> clear the f->stat buffer, since the syscall may have modified it,
> and we may need to know that stat.st_ino is zero.

Well spotted. By the same token is this useful?

diff --git a/src/ls.c b/src/ls.c
index 4531b94..fe51bb8 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -4001,8 +4001,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);
@@ -4219,9 +4220,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)






reply via email to

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