Subject: bug#11453: `ls` in coreutils-8.17 incorrectly shows broken symlinks in /

From: Jim Meyering <[email-masked]>
Date: Fri, 11 May 2012 20:48:33 +0200

Mike Frysinger wrote:
> coreutils-8.16 works fine (confirmed), and i don't recall seeing this bug
> before, so looks like a regression with 8.17
>
> easy to show:
> $ sudo ln -s dev /foo
> $ ls --color=auto /
> ... foo wrongly shows up in blinky text indicating it's a broken symlink ...
> $ ls --color=auto /.
> ... foo correctly shows up with normal coloring indicating it's a symlink ...
> $ cd / ; ls --color=auto
> ... foo correctly shows up with normal coloring indicating it's a symlink ...
> -mike

Rats.
That bug was introduced by my clean-up.
Here's the probable fix.
Thanks for the report!

diff --git a/src/ls.c b/src/ls.c
index 397e4ea..b548382 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -3213,7 +3213,8 @@ make_link_name (char const *name, char const *linkname)
     return xstrdup (linkname);

   char *p = xmalloc (prefix_len + 1 + strlen (linkname) + 1);
-  stpcpy (stpncpy (p, name, prefix_len + 1), linkname);
+  bool prefix_ends_in_slash = ISSLASH (name[prefix_len - 1]);
+  stpcpy (stpncpy (p, name, prefix_len + !prefix_ends_in_slash), linkname);
   return p;
 }