]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - posix/execlp.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / posix / execlp.c
index 950705604c40defa3e2d97958bcc3f9a98064b31..dbb46ecda0f4cfaca69da4183f43abc8698b9a15 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 93, 96, 97, 98, 99 Free Software Foundation, Inc.
+/* Copyright (C) 1991-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
 
-#include <alloca.h>
 #include <unistd.h>
 #include <stdarg.h>
-#include <stddef.h>
-#include <string.h>
-
-#include <stackinfo.h>
+#include <errno.h>
+#include <sys/param.h>
 
 /* Execute FILE, searching in the `PATH' environment variable if
    it contains no slashes, with all arguments after FILE until a
 int
 execlp (const char *file, const char *arg, ...)
 {
-  size_t argv_max = 1024;
-  const char **argv = alloca (argv_max * sizeof (const char *));
-  unsigned int i;
-  va_list args;
-
-  argv[0] = arg;
-
-  va_start (args, arg);
-  i = 0;
-  while (argv[i++] != NULL)
+  ptrdiff_t argc;
+  va_list ap;
+  va_start (ap, arg);
+  for (argc = 1; va_arg (ap, const char *); argc++)
     {
-      if (i == argv_max)
+      if (argc == INT_MAX)
        {
-         const char **nptr = alloca ((argv_max *= 2) * sizeof (const char *));
-
-#ifndef _STACK_GROWS_UP
-         if ((char *) nptr + argv_max == (char *) argv)
-           {
-             /* Stack grows down.  */
-             argv = (const char **) memcpy (nptr, argv,
-                                            i * sizeof (const char *));
-             argv_max += i;
-           }
-         else
-#endif
-#ifndef _STACK_GROWS_DOWN
-           if ((char *) argv + i == (char *) nptr)
-           /* Stack grows up.  */
-           argv_max += i;
-         else
-#endif
-           /* We have a hole in the stack.  */
-           argv = (const char **) memcpy (nptr, argv,
-                                          i * sizeof (const char *));
+         va_end (ap);
+         errno = E2BIG;
+         return -1;
        }
-
-      argv[i] = va_arg (args, const char *);
     }
-  va_end (args);
-
-  return execvp (file, (char *const *) argv);
+  va_end (ap);
+
+  /* Although posix does not state execlp as an async-safe function
+     it can not use malloc to allocate the arguments since it might
+     be used in a vfork scenario and it may lead to malloc internal
+     bad state.  */
+  ptrdiff_t i;
+  char *argv[argc + 1];
+  va_start (ap, arg);
+  argv[0] = (char *) arg;
+  for (i = 1; i <= argc; i++)
+    argv[i] = va_arg (ap, char *);
+  va_end (ap);
+
+  return __execvpe (file, argv, __environ);
 }
+libc_hidden_def (execlp)