Re: [PATCH] run-command: don't try to execute directories
From: Junio C Hamano <hidden>
Date: 2016-06-15 22:54:55
cmn@elego.de (Carlos Martín Nieto) writes:
How about something like this instead? We keep the access check and only do the stat call when we have found something we want to look at.
Sounds safer. Looking at the way the stat call is indented twice, I suspect that the variable can be defined inner scope, not at the top-level of the function?
quoted hunk
---8<---diff --git a/run-command.c b/run-command.c index 1101ef7..fb8a93c 100644 --- a/run-command.c +++ b/run-command.c@@ -85,6 +85,7 @@ static char *locate_in_PATH(const char *file) { const char *p = getenv("PATH"); struct strbuf buf = STRBUF_INIT; + struct stat st; if (!p || !*p) return NULL;@@ -101,7 +102,8 @@ static char *locate_in_PATH(const char *file) } strbuf_addstr(&buf, file); - if (!access(buf.buf, F_OK)) + if (!access(buf.buf, F_OK) && + !stat(buf.buf, &st) && !S_ISDIR(st.st_mode)) return strbuf_detach(&buf, NULL); if (!*end)