bug-coreutils
[Top][All Lists]
Advanced

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

a couple of glitches in 'ln'


From: Paul Eggert
Subject: a couple of glitches in 'ln'
Date: Wed, 23 Jun 2004 14:18:24 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

While looking at some other things I noticed a couple of problems in
'ln'.  First, there's a subscript error when the user invokes "ln foo
''": the code attempts to access the (-1)st or SIZE_MAXth byte in an
empty string.  Second, there's an unnecessary call to lstat -- at
least, it appears to be unnecessary to me.  Here's a patch.

2004-06-23  Paul Eggert  <address@hidden>

        * src/ln.c (do_link): Remove unnecessary call to lstat.
        (main): Avoid subscript error when the destination is "".

Index: src/ln.c
===================================================================
RCS file: /home/meyering/coreutils/cu/src/ln.c,v
retrieving revision 1.136
diff -p -u -r1.136 ln.c
--- src/ln.c    21 Jun 2004 15:03:35 -0000      1.136
+++ src/ln.c    23 Jun 2004 21:06:27 -0000
@@ -243,7 +243,7 @@ do_link (const char *source, const char 
       return 1;
     }
 
-  if (lstat_status == 0 || lstat (dest, &dest_stats) == 0)
+  if (lstat_status == 0)
     {
       if (S_ISDIR (dest_stats.st_mode))
        {
@@ -298,11 +298,6 @@ do_link (const char *source, const char 
          return 1;
        }
     }
-  else if (errno != ENOENT)
-    {
-      error (0, errno, _("accessing %s"), quote (dest));
-      return 1;
-    }
 
   if (verbose)
     {
@@ -555,18 +550,17 @@ main (int argc, char **argv)
   else
     {
       struct stat source_stats;
-      const char *source;
-      char *dest;
       char *new_dest;
-
-      source = file[0];
-      dest = file[1];
+      char const *source = file[0];
+      char *dest = file[1];
+      size_t destlen = strlen (dest);
 
       /* When the destination is specified with a trailing slash and the
         source exists but is not a directory, convert the user's command
         `ln source dest/' to `ln source dest/basename(source)'.  */
 
-      if (dest[strlen (dest) - 1] == '/'
+      if (destlen != 0
+         && dest[destlen - 1] == '/'
          && lstat (source, &source_stats) == 0
          && !S_ISDIR (source_stats.st_mode))
        {




reply via email to

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