Subject: [PATCH v2 01/21] path.c: avoid PATH_MAX as buffer size from get_pathname()

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sat, 14 Dec 2013 17:54:47 +0700

We've been avoiding PATH_MAX whenever possible. This patch avoids
PATH_MAX in get_pathname() and gives it enough room not to worry about
really long paths.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 path.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/path.c b/path.c
index 24594c4..4c1c144 100644
--- a/path.c
+++ b/path.c
@@ -16,10 +16,11 @@ static int get_st_mode_bits(const char *path, int *mode)
 
 static char bad_path[] = "/bad-path/";
 
-static char *get_pathname(void)
+static char *get_pathname(size_t *len)
 {
-	static char pathname_array[4][PATH_MAX];
+	static char pathname_array[4][4096];
 	static int index;
+	*len = sizeof(pathname_array[0]);
 	return pathname_array[3 & ++index];
 }
 
@@ -108,24 +109,26 @@ char *mkpath(const char *fmt, ...)
 {
 	va_list args;
 	unsigned len;
-	char *pathname = get_pathname();
+	size_t n;
+	char *pathname = get_pathname(&n);
 
 	va_start(args, fmt);
-	len = vsnprintf(pathname, PATH_MAX, fmt, args);
+	len = vsnprintf(pathname, n, fmt, args);
 	va_end(args);
-	if (len >= PATH_MAX)
+	if (len >= n)
 		return bad_path;
 	return cleanup_path(pathname);
 }
 
 char *git_path(const char *fmt, ...)
 {
-	char *pathname = get_pathname();
+	size_t len;
+	char *pathname = get_pathname(&len);
 	va_list args;
 	char *ret;
 
 	va_start(args, fmt);
-	ret = vsnpath(pathname, PATH_MAX, fmt, args);
+	ret = vsnpath(pathname, len, fmt, args);
 	va_end(args);
 	return ret;
 }
@@ -158,14 +161,15 @@ void home_config_paths(char **global, char **xdg, char *file)
 
 char *git_path_submodule(const char *path, const char *fmt, ...)
 {
-	char *pathname = get_pathname();
+	size_t n;
+	char *pathname = get_pathname(&n);
 	struct strbuf buf = STRBUF_INIT;
 	const char *git_dir;
 	va_list args;
 	unsigned len;
 
 	len = strlen(path);
-	if (len > PATH_MAX-100)
+	if (len > n-100)
 		return bad_path;
 
 	strbuf_addstr(&buf, path);
@@ -180,7 +184,7 @@ char *git_path_submodule(const char *path, const char *fmt, ...)
 	}
 	strbuf_addch(&buf, '/');
 
-	if (buf.len >= PATH_MAX)
+	if (buf.len >= n)
 		return bad_path;
 	memcpy(pathname, buf.buf, buf.len + 1);
 
@@ -188,9 +192,9 @@ char *git_path_submodule(const char *path, const char *fmt, ...)
 	len = strlen(pathname);
 
 	va_start(args, fmt);
-	len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
+	len += vsnprintf(pathname + len, n - len, fmt, args);
 	va_end(args);
-	if (len >= PATH_MAX)
+	if (len >= n)
 		return bad_path;
 	return cleanup_path(pathname);
 }
-- 
1.8.5.1.77.g42c48fa