From: Junio C Hamano Date: Tue, 02 Oct 2012 19:59:58 GMT Subject: Re: [PATCH] run-command: don't try to execute directories Message-ID: <7vd3107igx.fsf@alter.siamese.dyndns.org> In-Reply-To: <87bogkisas.fsf@centaur.cmartin.tk> 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? > > ---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)