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

Re: [PATCH 3/3] commit-graph.c: handle corrupt/missing trees

From
Taylor Blau <me@ttaylorr.com>
Date
Sep 6, 2019, 15:42 UTC
Message-ID
<20190906154214.GA3657@syl.local>
In-Reply-To
<20190906061919.GA5122@sigill.intra.peff.net>
On Fri, Sep 06, 2019 at 02:19:20AM -0400, Jeff King wrote:
Show 28 quoted lines
> On Thu, Sep 05, 2019 at 06:04:57PM -0400, Taylor Blau wrote:
>
> > @@ -846,7 +847,11 @@ static void write_graph_chunk_data(struct hashfile *f, int hash_len,
> >  		if (parse_commit_no_graph(*list))
> >  			die(_("unable to parse commit %s"),
> >  				oid_to_hex(&(*list)->object.oid));
> > -		hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
> > +		tree = get_commit_tree_oid(*list);
> > +		if (!tree)
> > +			die(_("unable to get tree for %s"),
> > +				oid_to_hex(&(*list)->object.oid));
> > +		hashwrite(f, tree->hash, hash_len);
>
> Yeah, I think this is a good stop-gap to protect ourselves, until a time
> when parse_commit() and friends consistently warn us about the breakage.
>
> > diff --git a/commit.c b/commit.c
> > index a98de16e3d..fab22cb740 100644
> > --- a/commit.c
> > +++ b/commit.c
> > @@ -358,7 +358,8 @@ struct tree *repo_get_commit_tree(struct repository *r,
> >
> >  struct object_id *get_commit_tree_oid(const struct commit *commit)
> >  {
> > -	return &get_commit_tree(commit)->object.oid;
> > +	struct tree *tree = get_commit_tree(commit);
> > +	return tree ? &tree->object.oid : NULL;
> >  }

You mentioned in the version of this series that is rebased on GitHub's fork that it may be worth putting this hunk in a separate commit entirely. I don't disagree, so if there are other comments that merit a reroll of this, I'm happy to pull this change out as 3/4.

Show 6 quoted lines
> This one in theory benefits lots of other callsites, too, since it means
> we'll actually return NULL instead of nonsense like "8". But grepping
> around for calls to this function, I found literally zero of them
> actually bother checking for a NULL result. So there are probably dozens
> of similar segfaults waiting to happen in other code paths.
> Discouraging.

Discouraging indeed. I think that you suggest it below, but perhaps the right thing to do here is implement 'get_commit_tree_oid()' as follows:

  struct object_id *get_commit_tree_oid(const struct commit *commit)
  {
    struct tree *tree = get_commit_tree(commit);
    if (!tree)
      die(_("unable to get tree from commit %s"),
          oid_to_hex(&commit->object.oid));
    return &tree->object.oid;
  }

Which then puts the onus on the *caller* to check their commit pointer to make sure that it has a legit tree in it, unless they're OK with dying.

In the commit-graph change that this whole thread is in response to, that's exactly what we want: I don't want to have to check the return value of two function calls myself. I'm perfectly happy to die() in the middle of things if there is an object corruption, but the library code should take care of that for me, and not allow for dozens of checks, each with their own unique 'die()'-ing message.

All of that said, I don't know if I think it's worth holding this series up on the above in the meantime. I do think that it (or something like it) is generally worth doing, but I'm not sure that now is the time to do it.

Show 5 quoted lines
> This is sort-of attributable to my 834876630b (get_commit_tree(): return
> NULL for broken tree, 2019-04-09). Before then it was a BUG(). However,
> that state was relatively short-lived. Before 7b8a21dba1 (commit-graph:
> lazy-load trees for commits, 2018-04-06), we'd have similarly returned
> NULL (and anyway, BUG() is clearly wrong since it's a data error).

Ha, I was wondering why that commit message looked familiar... it turns out that I'm the culprit, too, via the 'Co-authored-by' trailer. Oops.

Show 5 quoted lines
> None of which argues against your patches, but it's kind of sad that the
> issue is present in so many code paths. I wonder if we could be handling
> this in a more central way, but I don't see how short of dying.
>
> -Peff

Thanks, Taylor

Previous: Jeff KingNext: Jeff King
Message 7 of 15 in “commit-graph: harden against various corruptions”
  1. 0/3 commit-graph: harden against various corruptionsTaylor Blau, Sep 5, 2019
  2. 1/3 t/t5318: introduce failing 'git commit-graph write' testsTaylor Blau, Sep 5, 2019
  3. Derrick StoleeSep 6, 2019
  4. 2/3 commit-graph.c: handle commit parsing errorsTaylor Blau, Sep 5, 2019
  5. 3/3 commit-graph.c: handle corrupt/missing treesTaylor Blau, Sep 5, 2019
  6. Jeff KingSep 6, 2019
  7. Taylor BlauSep 6, 2019
  8. Jeff KingSep 6, 2019
  9. Derrick StoleeSep 6, 2019
  10. Jeff KingSep 6, 2019
  11. Junio C HamanoSep 6, 2019
  12. Junio C HamanoSep 6, 2019
  13. Jeff KingSep 6, 2019
  14. Jeff KingSep 6, 2019
  15. Junio C HamanoSep 9, 2019

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.