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

Re: [PATCH 1/2] config: check if config path is a file before parsing it

From
Jeff King <peff@peff.net>
Date
Mar 3, 2017, 10:15 UTC
Message-ID
<20170303101503.lf2ub2c7i6w7kg3t@sigill.intra.peff.net>
In-Reply-To
<CACsJy8DU7-o06mfuw1L02CFFR2wmoNa0MQJ8KqsV79ULzjRaRQ@mail.gmail.com>
On Fri, Mar 03, 2017 at 05:06:57PM +0700, Duy Nguyen wrote:
Show 11 quoted lines
> > But if we do, I think we'd either want to:
> >
> >        a. actually check ferror() after getting EOF and report the read
> >           error. That catches EISDIR, along with any other unexpected
> >           errors.
> >
> >        b. use an fopen wrapper that checks fstat(fileno(fh)) after the
> >           open, and turns fopen(some_dir) into an error.
> 
> If you don't like extra check, I guess you're negative on b as well
> since it is an extra check on Windows. That leaves us with option a.

I don't mind _doing_ the extra check that much. I don't think we fopen so many files that an extra fstat on each would kill us. I mostly just don't like having to sprinkle the explicit call to it everywhere. I'd be OK with:

  FILE *xfopen(const char *path, const char *mode)
  {
	FILE *ret = fopen(path, mode);
  #ifdef FOPEN_OPENS_DIRECTORIES
	if (ret) {
		struct stat st;
		if (!fstat(fileno(ret), &st) && S_ISDIR(st.st_mode)) {
			fclose(ret);
			ret = NULL;
		}
	}
  #endif
	return ret;
  }

But I do think option (a) is cleaner. The only trick is that for errno to be valid, we need to make sure we check ferror() soon after seeing the EOF return value. I suspect it would work OK in practice for the git_config_from_file() case.

-Peff
Previous: Duy NguyenNext: Jeff King
Message 6 of 11 in “Improve error messages when opening a directory as file”
  1. 0/2 Improve error messages when opening a directory as fileNguyễn Thái Ngọc Duy, Mar 3, 2017
  2. 2/2 attr.c: check if .gitattributes is a file before parsing itNguyễn Thái Ngọc Duy, Mar 3, 2017
  3. 1/2 config: check if config path is a file before parsing itNguyễn Thái Ngọc Duy, Mar 3, 2017
  4. Jeff KingMar 3, 2017
  5. Duy NguyenMar 3, 2017
  6. Jeff KingMar 3, 2017
  7. Jeff KingMar 3, 2017
  8. Duy NguyenMar 3, 2017
  9. Jeff KingMar 3, 2017
  10. Junio C HamanoMar 3, 2017
  11. Ramsay JonesMar 3, 2017

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.