]> Savannah Git Hosting - gnulib.git/commitdiff
findprog, relocatable-prog: Ignore directories during PATH search.
authorBruno Haible <bruno@clisp.org>
Fri, 10 Apr 2020 13:57:10 +0000 (15:57 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 10 Apr 2020 13:57:10 +0000 (15:57 +0200)
Reported by Frederick Eaton via Dmitry Goncharov in
<https://lists.gnu.org/archive/html/bug-gnulib/2020-03/msg00003.html>.

* lib/findprog.c (find_in_path): When the file found in a PATH element
is a directory, continue searching.
* modules/findprog (Depends-on): Add sys_stat, stat.
* modules/findprog-lgpl (Depends-on): Likewise.

* lib/progreloc.c (maybe_executable): When the file found in a PATH
element is a directory, continue searching.
* lib/relocwrapper.c: Update comments.
* modules/relocatable-prog-wrapper (Files): Add m4/largefile.m4.
(configure.ac-early): New section.

ChangeLog
lib/findprog.c
lib/progreloc.c
lib/relocwrapper.c
modules/findprog
modules/findprog-lgpl
modules/relocatable-prog-wrapper

index ee0d9323e6f3a673493d1db79bd43b48852547f6..27d0d7a7f1d1efc9c9ef745497e9e8c7699eb008 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2020-04-10  Bruno Haible  <bruno@clisp.org>
+
+       findprog, relocatable-prog: Ignore directories during PATH search.
+       Reported by Frederick Eaton via Dmitry Goncharov in
+       <https://lists.gnu.org/archive/html/bug-gnulib/2020-03/msg00003.html>.
+
+       * lib/findprog.c (find_in_path): When the file found in a PATH element
+       is a directory, continue searching.
+       * modules/findprog (Depends-on): Add sys_stat, stat.
+       * modules/findprog-lgpl (Depends-on): Likewise.
+
+       * lib/progreloc.c (maybe_executable): When the file found in a PATH
+       element is a directory, continue searching.
+       * lib/relocwrapper.c: Update comments.
+       * modules/relocatable-prog-wrapper (Files): Add m4/largefile.m4.
+       (configure.ac-early): New section.
+
 2020-04-10  Bruno Haible  <bruno@clisp.org>
 
        MODULES.html.sh: Support for reproducible builds from git-less tarballs.
index d0d41791e22fb3e0755241e56c213a7c5614b040..b562e9da6ad9ac35ca6244ae689f644c135ab195 100644 (file)
@@ -25,6 +25,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#if !(defined _WIN32 || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__)
+# include <sys/stat.h>
+#endif
 
 /* Avoid collision between findprog.c and findprog-lgpl.c.  */
 #if IN_FINDPROG_LGPL || ! GNULIB_FINDPROG_LGPL
@@ -105,22 +108,29 @@ find_in_path (const char *progname)
          design flaw.  */
       if (eaccess (progpathname, X_OK) == 0)
         {
-          /* Found!  */
-          if (strcmp (progpathname, progname) == 0)
+          /* Check that the progpathname does not point to a directory.  */
+          struct stat statbuf;
+
+          if (stat (progpathname, &statbuf) >= 0
+              && ! S_ISDIR (statbuf.st_mode))
             {
-              free (progpathname);
-
-              /* Add the "./" prefix for real, that xconcatenated_filename()
-                 optimized away.  This avoids a second PATH search when the
-                 caller uses execlp/execvp.  */
-              progpathname = XNMALLOC (2 + strlen (progname) + 1, char);
-              progpathname[0] = '.';
-              progpathname[1] = '/';
-              memcpy (progpathname + 2, progname, strlen (progname) + 1);
+              /* Found!  */
+              if (strcmp (progpathname, progname) == 0)
+                {
+                  free (progpathname);
+
+                  /* Add the "./" prefix for real, that xconcatenated_filename()
+                     optimized away.  This avoids a second PATH search when the
+                     caller uses execlp/execvp.  */
+                  progpathname = XNMALLOC (2 + strlen (progname) + 1, char);
+                  progpathname[0] = '.';
+                  progpathname[1] = '/';
+                  memcpy (progpathname + 2, progname, strlen (progname) + 1);
+                }
+
+              free (path);
+              return progpathname;
             }
-
-          free (path);
-          return progpathname;
         }
 
       free (progpathname);
index b555211317dfa42f62b585f9dedfd934aead7660..45be1ca5a4d6f18b66e28ba75ed6bdabf6046806 100644 (file)
@@ -154,7 +154,7 @@ static int executable_fd = -1;
 /* Define this function only when it's needed.  */
 #if !(defined WINDOWS_NATIVE || defined __EMX__)
 
-/* Tests whether a given pathname may belong to the executable.  */
+/* Tests whether a given filename may belong to the executable.  */
 static bool
 maybe_executable (const char *filename)
 {
@@ -173,18 +173,20 @@ maybe_executable (const char *filename)
       struct stat statfile;
 
       if (fstat (executable_fd, &statexe) >= 0)
-        {
-          if (stat (filename, &statfile) < 0)
-            return false;
-          if (!(statfile.st_dev
+        return (stat (filename, &statfile) >= 0
+                && statfile.st_dev
                 && statfile.st_dev == statexe.st_dev
-                && statfile.st_ino == statexe.st_ino))
-            return false;
-        }
+                && statfile.st_ino == statexe.st_ino);
     }
 # endif
 
-  return true;
+  /* Check that the filename does not point to a directory.  */
+  {
+    struct stat statfile;
+
+    return (stat (filename, &statfile) >= 0
+            && ! S_ISDIR (statfile.st_mode));
+  }
 }
 
 #endif
index dfe7e4f1fa5c843b7de31af097b93272bcaf89d1..e3dc197dcad1ae0c161440d79f8beac3825465f4 100644 (file)
    relocwrapper
     -> progname
     -> progreloc
+       -> stat
+          -> filename
+          -> pathmax
+          -> verify
        -> areadlink
           -> careadlinkat
              -> allocator
           -> readlink
              -> stat
-                -> filename
-                -> pathmax
-                -> verify
        -> canonicalize-lgpl
           -> filename
           -> malloca
index d48158b7910102a09df788a3ac9eb243d8fc7f61..b5f3c20d51cf25503ba27037ffc880850ead58ac 100644 (file)
@@ -9,9 +9,11 @@ m4/eaccess.m4
 
 Depends-on:
 stdbool
+sys_stat
 xalloc
 xconcat-filename
 access
+stat
 unistd
 
 configure.ac:
index 477eccb003b4260afc8be6cb92e8a0f817c8cf70..3c56f0245da6d8848af5f1b7dee52622168077c0 100644 (file)
@@ -10,9 +10,11 @@ m4/eaccess.m4
 
 Depends-on:
 stdbool
+sys_stat
 strdup
 concat-filename
 access
+stat
 unistd
 
 configure.ac:
index 3cc3d76a45e04a4136ac7c154d853ee16523f876..f97fdae65672496a5805be3adf747ec0558b3a60 100644 (file)
@@ -25,6 +25,7 @@ lib/relocatable.c
 lib/setenv.c
 lib/c-ctype.h
 lib/c-ctype.c
+m4/largefile.m4
 m4/malloca.m4
 m4/canonicalize.m4
 m4/eealloc.m4
@@ -50,6 +51,9 @@ string
 verify
 xalloc-oversized
 
+configure.ac-early:
+AC_REQUIRE([AC_SYS_LARGEFILE])
+
 configure.ac:
 AC_REQUIRE([AC_C_RESTRICT])
 gl_FUNC_READLINK_SEPARATE