From 4e88ea446cbc773e8f8633a6fc7bda2c155ed20a Mon Sep 17 00:00:00 2001
From: Eric Andersen <andersen@codepoet.org>
Date: Mon, 7 May 2001 18:29:33 +0000
Subject: Change FILENAME_MAX to be only 255.  Max filename on ext2 is 255, so
 there is no reason to allocate 4k.  Change working of execvep.c per patch
 from Matthias Kilian <kili@outback.escape.de> so that there is not a fixed
 127 byte buffer.  Too easy to overflow...  -Erik

---
 libc/sysdeps/linux/arm/bits/stdio_lim.h     |  2 +-
 libc/sysdeps/linux/i386/bits/stdio_lim.h    |  2 +-
 libc/sysdeps/linux/m68k/bits/stdio_lim.h    |  2 +-
 libc/sysdeps/linux/powerpc/bits/stdio_lim.h |  2 +-
 libc/sysdeps/linux/sh/bits/stdio_lim.h      |  2 +-
 libc/sysdeps/linux/sparc/bits/stdio_lim.h   |  2 +-
 libc/unistd/execl.c                         |  2 +-
 libc/unistd/execlp.c                        |  2 +-
 libc/unistd/execvep.c                       | 59 +++++++++++++++--------------
 9 files changed, 39 insertions(+), 36 deletions(-)

diff --git a/libc/sysdeps/linux/arm/bits/stdio_lim.h b/libc/sysdeps/linux/arm/bits/stdio_lim.h
index bf79f2062..9787b4358 100644
--- a/libc/sysdeps/linux/arm/bits/stdio_lim.h
+++ b/libc/sysdeps/linux/arm/bits/stdio_lim.h
@@ -23,7 +23,7 @@
 #ifdef _STDIO_H
 # define L_tmpnam 20
 # define TMP_MAX 238328
-# define FILENAME_MAX 4095 
+# define FILENAME_MAX 255
 
 # ifdef __USE_POSIX
 #  define L_ctermid 9
diff --git a/libc/sysdeps/linux/i386/bits/stdio_lim.h b/libc/sysdeps/linux/i386/bits/stdio_lim.h
index bf79f2062..9787b4358 100644
--- a/libc/sysdeps/linux/i386/bits/stdio_lim.h
+++ b/libc/sysdeps/linux/i386/bits/stdio_lim.h
@@ -23,7 +23,7 @@
 #ifdef _STDIO_H
 # define L_tmpnam 20
 # define TMP_MAX 238328
-# define FILENAME_MAX 4095 
+# define FILENAME_MAX 255
 
 # ifdef __USE_POSIX
 #  define L_ctermid 9
diff --git a/libc/sysdeps/linux/m68k/bits/stdio_lim.h b/libc/sysdeps/linux/m68k/bits/stdio_lim.h
index bf79f2062..9787b4358 100644
--- a/libc/sysdeps/linux/m68k/bits/stdio_lim.h
+++ b/libc/sysdeps/linux/m68k/bits/stdio_lim.h
@@ -23,7 +23,7 @@
 #ifdef _STDIO_H
 # define L_tmpnam 20
 # define TMP_MAX 238328
-# define FILENAME_MAX 4095 
+# define FILENAME_MAX 255
 
 # ifdef __USE_POSIX
 #  define L_ctermid 9
diff --git a/libc/sysdeps/linux/powerpc/bits/stdio_lim.h b/libc/sysdeps/linux/powerpc/bits/stdio_lim.h
index bf79f2062..9787b4358 100644
--- a/libc/sysdeps/linux/powerpc/bits/stdio_lim.h
+++ b/libc/sysdeps/linux/powerpc/bits/stdio_lim.h
@@ -23,7 +23,7 @@
 #ifdef _STDIO_H
 # define L_tmpnam 20
 # define TMP_MAX 238328
-# define FILENAME_MAX 4095 
+# define FILENAME_MAX 255
 
 # ifdef __USE_POSIX
 #  define L_ctermid 9
diff --git a/libc/sysdeps/linux/sh/bits/stdio_lim.h b/libc/sysdeps/linux/sh/bits/stdio_lim.h
index bf79f2062..9787b4358 100644
--- a/libc/sysdeps/linux/sh/bits/stdio_lim.h
+++ b/libc/sysdeps/linux/sh/bits/stdio_lim.h
@@ -23,7 +23,7 @@
 #ifdef _STDIO_H
 # define L_tmpnam 20
 # define TMP_MAX 238328
-# define FILENAME_MAX 4095 
+# define FILENAME_MAX 255
 
 # ifdef __USE_POSIX
 #  define L_ctermid 9
diff --git a/libc/sysdeps/linux/sparc/bits/stdio_lim.h b/libc/sysdeps/linux/sparc/bits/stdio_lim.h
index bf79f2062..9787b4358 100644
--- a/libc/sysdeps/linux/sparc/bits/stdio_lim.h
+++ b/libc/sysdeps/linux/sparc/bits/stdio_lim.h
@@ -23,7 +23,7 @@
 #ifdef _STDIO_H
 # define L_tmpnam 20
 # define TMP_MAX 238328
-# define FILENAME_MAX 4095 
+# define FILENAME_MAX 255
 
 # ifdef __USE_POSIX
 #  define L_ctermid 9
diff --git a/libc/unistd/execl.c b/libc/unistd/execl.c
index a05867ef8..9d49d5abd 100644
--- a/libc/unistd/execl.c
+++ b/libc/unistd/execl.c
@@ -26,7 +26,7 @@ int execl(__const char *path, __const char *arg, ...)
 	if (i <= 16)
 		argv = shortargv;
 	else {
-		argv = (const char **) malloc(sizeof(char *) * i);
+		argv = (const char **) alloca(sizeof(char *) * i);
 	}
 
 	argv[0] = arg;
diff --git a/libc/unistd/execlp.c b/libc/unistd/execlp.c
index fef10d376..1e2f6558d 100644
--- a/libc/unistd/execlp.c
+++ b/libc/unistd/execlp.c
@@ -28,7 +28,7 @@ int execlp(__const char *file, __const char *arg, ...)
 	if (i <= 16)
 		argv = shortargv;
 	else {
-		argv = (const char **) malloc(sizeof(char *) * i);
+		argv = (const char **) alloca(sizeof(char *) * i);
 	}
 
 	argv[0] = arg;
diff --git a/libc/unistd/execvep.c b/libc/unistd/execvep.c
index 4ac7692e9..6cd8cbaf9 100644
--- a/libc/unistd/execvep.c
+++ b/libc/unistd/execvep.c
@@ -1,43 +1,46 @@
-
 #include <unistd.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 
 int execvep(const char *path, char *__const argv[], char *__const envp[])
 {
-	if (!strchr(path, '/')) {
-		char partial[128];
-		char *p = getenv("PATH");
-		char *p2;
-
-		if (!p)
-			p = "/bin:/usr/bin";
+    if (!strchr(path, '/')) {
+	char *p = getenv("PATH");
 
-		for (; p && *p;) {
+	if (!p)
+	    p = "/bin:/usr/bin";
 
-			strcpy(partial, p);
+	for (; p && *p;) {
+	    char partial[FILENAME_MAX];
+	    char *p2;
 
-			p2 = strchr(partial, ':');
-			if (p2)
-				*p2 = '\0';
+	    p2 = strchr(p, ':');
+	    if (p2) {
+		size_t len = p2 - p;
+		strncpy(partial, p, len);
+		partial[len] = 0;
+	    } else {
+		strcpy(partial, p);
+	    }
 
-			if (strlen(partial))
-				strcat(partial, "/");
-			strcat(partial, path);
+	    if (strlen(partial))
+		strcat(partial, "/");
+	    strcat(partial, path);
 
-			execve(partial, argv, envp);
+	    execve(partial, argv, envp);
 
-			if (errno != ENOENT)
-				return -1;
-
-			p2 = strchr(p, ':');
-			if (p2)
-				p = p2 + 1;
-			else
-				p = 0;
-		}
+	    if (errno != ENOENT)
 		return -1;
-	} else
-		return execve(path, argv, envp);
+
+	    if (p2) {
+		p = p2 + 1;
+	    } else {
+		p = 0;
+	    }
+	}
+	return -1;
+    } else
+	return execve(path, argv, envp);
 }
-- 
cgit v1.2.3