Subject: [PATCH] Avoid interpreting too-long parameter as file name

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Mon, 8 Feb 2016 12:16:28 +0700

Even if it is easier to write HEAD~2000, it is legal to write
HEAD^^^... (repeats "^" 2000 times in total). However, such a string is
too long to be a legal filename (and on Windows, by default even much,
much shorter strings are still illegal because they exceed MAX_PATH).

Therefore, if the check_filename() function encounters too long a
command-line parameter, it should interpet the error code ENAMETOOLONG
as a strong hint that this is not a file name instead of dying with an
error message.

Noticed-by: Ole Tange <[email-masked]>
Helped-by: Johannes Schindelin <[email-masked]>
Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 Note, git grep ENOENT.*ENOTDIR reveals a couple more matches, but I
 didn't check if they should receive the same treatment.

 Another option is just use file_exists() here instead, but I guess
 that's too relaxing.

 setup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/setup.c b/setup.c
index 2c4b22c..ab8f85d 100644
--- a/setup.c
+++ b/setup.c
@@ -147,7 +147,7 @@ int check_filename(const char *prefix, const char *arg)
 		name = arg;
 	if (!lstat(name, &st))
 		return 1; /* file exists */
-	if (errno == ENOENT || errno == ENOTDIR)
+	if (errno == ENOENT || errno == ENOTDIR || errno == ENAMETOOLONG)
 		return 0; /* file does not exist */
 	die_errno("failed to stat '%s'", arg);
 }
-- 
2.7.0.377.g4cd97dd