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

Re: [PATCH 1/2] read_info_alternates: read contents into strbuf

From
Jeff King <peff@peff.net>
Date
Sep 19, 2017, 05:03 UTC
Message-ID
<20170919050316.nfbk6wckgdtmzvvd@sigill.intra.peff.net>
In-Reply-To
<20170919024253.GC175206@aiede.mtv.corp.google.com>
On Mon, Sep 18, 2017 at 07:42:53PM -0700, Jonathan Nieder wrote:
Show 9 quoted lines
> Jeff King wrote:
> 
> > Reported-by: Michael Haggerty <mhagger@alum.mit.edu>
> > Signed-off-by: Jeff King <peff@peff.net>
> > ---
> >  sha1_file.c | 29 +++++++++--------------------
> >  1 file changed, 9 insertions(+), 20 deletions(-)
> 
> Thanks for tracking it down.

To be fair, Michael did most of the work in identifying and bisecting the bug. He even wrote a very similar patch in parallel; I just swooped in at the end.

Show 13 quoted lines
> >  	path = xstrfmt("%s/info/alternates", relative_base);
> > -	fd = git_open(path);
> > -	free(path);
> > -	if (fd < 0)
> > -		return;
> > -	if (fstat(fd, &st) || (st.st_size == 0)) {
> > -		close(fd);
> > +	if (strbuf_read_file(&buf, path, 1024) < 0) {
> > +		free(path);
> >  		return;
> 
> strbuf_read_file is careful to release buf on failure, so this doesn't
> leak (but it's a bit subtle).

Yep. I didn't think it was worth calling out with a comment since the "don't allocate on failure" pattern is common to most of the strbuf functions.

> What happened to the !st.st_size case?  Is it eliminated for
> simplicity?

Good question, and the answer is yes. Obviously we can bail early on an empty file, but I don't think there's any reason to complicate the code with it (the original seems to come from d5a63b9983 (Alternate object pool mechanism updates., 2005-08-14), where it avoided a corner case that has long since been deleted.

-Peff
Previous: Jonathan NiederNext: Jeff King
Message 5 of 14 in “fix read past end of array in alternates files”
  1. 0/2 fix read past end of array in alternates filesJeff King, Sep 18, 2017
  2. 1/2 read_info_alternates: read contents into strbufJeff King, Sep 18, 2017
  3. Junio C HamanoSep 19, 2017
  4. Jonathan NiederSep 19, 2017
  5. Jeff KingSep 19, 2017
  6. 2/2 read_info_alternates: warn on non-trivial errorsJeff King, Sep 18, 2017
  7. Jonathan NiederSep 19, 2017
  8. Jeff KingSep 19, 2017
  9. Jonathan NiederSep 19, 2017
  10. Jeff KingSep 19, 2017
  11. 0/2 fix read past end of array in alternates filesJeff King, Sep 19, 2017
  12. 1/2 read_info_alternates: read contents into strbufJeff King, Sep 19, 2017
  13. 2/2 read_info_alternates: warn on non-trivial errorsJeff King, Sep 19, 2017
  14. Junio C HamanoSep 20, 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.