git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] run-command: don't try to execute directories

From
Jeff King <peff@peff.net>
Date
Oct 2, 2012, 21:26 UTC
Message-ID
<20121002212645.GA26789@sigill.intra.peff.net>
In-Reply-To
<87bogkisas.fsf@centaur.cmartin.tk>
On Tue, Oct 02, 2012 at 09:32:11PM +0200, Carlos Martín Nieto wrote:
Show 17 quoted lines
> >> @@ -101,8 +102,9 @@ static char *locate_in_PATH(const char *file)
> >>  		}
> >>  		strbuf_addstr(&buf, file);
> >>  
> >> -		if (!access(buf.buf, F_OK))
> >> +		if (!stat(buf.buf, &st) && !S_ISDIR(st.st_mode)) {
> >>  			return strbuf_detach(&buf, NULL);
> >> +		}
> >
> > So we used to say "if it exists and accessible, return that".  Now
> > we say "if it exists and is not a directory, return that".
> >
> > I have to wonder what would happen if it exists as a non-directory
> > but we cannot access it.  Is that a regression?
> 
> I guess it would be, yeah. Would this be related to tha situation where
> the user isn't allowed to access something in their PATH?

This code path is related to correcting EACCES errors into ENOENT. But it does not bother checking permissions itself. We know there is some permission problem, because execvp told us EACCES, so we are only checking whether such a file actually exists at all in the PATH. And that is why we are using F_OK with access, and not X_OK.

So any reason for which stat() would fail would presumably cause access(F_OK) to fail, too (mostly things like leading directories not being readable), and I think converting the access into a stat is OK.

Adding the !ISDIR on top of it makes sense if you want to consider the directory in your PATH to be a harmless thing to be ignored. However, I am not sure that is a good idea. The intent of ignoring the original EACCES is that it could be caused by totally uninteresting crap, like an inaccessible directory in your PATH.

Whereas in this case, the error really is that we found "git-foo", but it is somehow broken. And it almost certainly is a configuration error on the part of the user (why would they put a git-foo directory in their PATH? Presumably they meant to put its contents into the PATH).

So I think your implementation is fine, but I'm a little dubious of the value of ignoring such an error.

-Peff
Previous: Junio C Hamano
Message 5 of 5 in “run-command: don't try to execute directories”
  1. run-command: don't try to execute directoriesCarlos Martín Nieto, Oct 2, 2012
  2. Junio C HamanoOct 2, 2012
  3. Carlos Martín NietoOct 2, 2012
  4. Junio C HamanoOct 2, 2012
  5. Jeff KingOct 2, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.