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

Re: [PATCH 3/5] midx-write: use cleanup when incremental midx fails

From
Taylor Blau <me@ttaylorr.com>
Date
Aug 29, 2025, 01:29 UTC
Message-ID
<aLECkfDqFVgNA1xh@nand.local>
In-Reply-To
<xmqqwm6ntbjt.fsf@gitster.g>
On Thu, Aug 28, 2025 at 01:51:18PM -0700, Junio C Hamano wrote:
Show 37 quoted lines
> "Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > From: Derrick Stolee <stolee@gmail.com>
> >
> > The incremental mode of writing a multi-pack-index has a few extra
> > conditions that could lead to failure, but these are currently
> > short-ciruiting with 'return -1' instead of setting the method's
> > 'result' variable and going to the cleanup tag.
> >
> > Replace these returns with gotos to avoid memory issues when exiting
> > early due to error conditions.
> >
> > Unfortunately, these error conditions are difficult to reproduce with
> > test cases, which is perhaps one reason why the memory loss was not
> > caught by existing test cases in memory tracking modes.
> >
> > Signed-off-by: Derrick Stolee <stolee@gmail.com>
> > ---
> >  midx-write.c | 18 ++++++++++++------
> >  1 file changed, 12 insertions(+), 6 deletions(-)
>
> Good thinking, but may I suggest us to go one more step to adopt
> even better convention if we were to do this?
>
> Pessimistically initialize the "result" to -1 and let many "goto
> cleanup" just jump there.  And have "result = 0" just before the
> cleanup label where the success code path joins the final cleanup
> part of the function.
>
> This is often the right way to make the flow easier to see, because
> often the success code path is straight forward, and these error
> conditions are what employ the "goto cleanup" from many places.  By
> starting pessimistic, and declaring the success at the very end of
> the straight-forward success case code path, all other flows to the
> clean-up labels do not have to set the "ah I failed" flag.  It would
> eliminate the need for patches like the previous step if the
> original were following that pattern.
Alternatively replacing something like:
    error(...);
    result = -1;
    goto cleanup;
with just
    result = error(...);
    goto cleanup;

would IMHO make the code easier to read, though I agree that nothing is forcing us to remember to assign result in the first place ;-). I am not sure the pessimistic initialization is better in all cases either, since we have to remember to place it before any "cleanup" label, and make sure that that does not regress.

So, I dunno. I'm OK with what is written here, and I think we could certainly have a separate discussion to perhaps have CodingGuidelines take a stronger stance here.

Thanks, Taylor

Previous: Junio C HamanoNext: Derrick Stolee
Message 11 of 47 in “midx-write: fix segfault and do several cleanups”
  1. 0/5 midx-write: fix segfault and do several cleanupsDerrick Stolee via GitGitGadget, Aug 28, 2025
  2. 1/5 midx-write: only load initialized packsDerrick Stolee via GitGitGadget, Aug 28, 2025
  3. Junio C HamanoAug 28, 2025
  4. Taylor BlauAug 29, 2025
  5. Derrick StoleeAug 30, 2025
  6. 2/5 midx-write: put failing response value backDerrick Stolee via GitGitGadget, Aug 28, 2025
  7. Junio C HamanoAug 28, 2025
  8. Taylor BlauAug 29, 2025
  9. 3/5 midx-write: use cleanup when incremental midx failsDerrick Stolee via GitGitGadget, Aug 28, 2025
  10. Junio C HamanoAug 28, 2025
  11. Taylor BlauAug 29, 2025
  12. Derrick StoleeAug 30, 2025
  13. 4/5 midx-write: use uint32_t for preferred_pack_idxDerrick Stolee via GitGitGadget, Aug 28, 2025
  14. Junio C HamanoAug 28, 2025
  15. Taylor BlauAug 29, 2025
  16. 5/5 midx-write: reenable signed comparison errorsDerrick Stolee via GitGitGadget, Aug 28, 2025
  17. Junio C HamanoAug 28, 2025
  18. Taylor BlauAug 29, 2025
  19. Taylor BlauAug 29, 2025
  20. 0/6 midx-write: fix segfault and do several cleanupsDerrick Stolee via GitGitGadget, Aug 30, 2025
  21. 1/6 midx-write: only load initialized packsDerrick Stolee via GitGitGadget, Aug 30, 2025
  22. Patrick SteinhardtSep 3, 2025
  23. Derrick StoleeSep 5, 2025
  24. 2/6 midx-write: put failing response value backDerrick Stolee via GitGitGadget, Aug 30, 2025
  25. Patrick SteinhardtSep 3, 2025
  26. Derrick StoleeSep 5, 2025
  27. 3/6 midx-write: use cleanup when incremental midx failsDerrick Stolee via GitGitGadget, Aug 30, 2025
  28. Patrick SteinhardtSep 3, 2025
  29. 4/6 midx-write: use uint32_t for preferred_pack_idxDerrick Stolee via GitGitGadget, Aug 30, 2025
  30. Patrick SteinhardtSep 3, 2025
  31. Derrick StoleeSep 5, 2025
  32. 5/6 midx-write: reenable signed comparison errorsDerrick Stolee via GitGitGadget, Aug 30, 2025
  33. Patrick SteinhardtSep 3, 2025
  34. 6/6 midx-write: simplify error casesDerrick Stolee via GitGitGadget, Aug 30, 2025
  35. Patrick SteinhardtSep 3, 2025
  36. Junio C HamanoSep 3, 2025
  37. 0/6 midx-write: fix segfault and do several cleanupsDerrick Stolee via GitGitGadget, Sep 5, 2025
  38. 1/6 midx-write: only load initialized packsDerrick Stolee via GitGitGadget, Sep 5, 2025
  39. 2/6 midx-write: put failing response value backDerrick Stolee via GitGitGadget, Sep 5, 2025
  40. 3/6 midx-write: use cleanup when incremental midx failsDerrick Stolee via GitGitGadget, Sep 5, 2025
  41. 4/6 midx-write: use uint32_t for preferred_pack_idxDerrick Stolee via GitGitGadget, Sep 5, 2025
  42. 5/6 midx-write: reenable signed comparison errorsDerrick Stolee via GitGitGadget, Sep 5, 2025
  43. 6/6 midx-write: simplify error casesDerrick Stolee via GitGitGadget, Sep 5, 2025
  44. Junio C HamanoSep 5, 2025
  45. Derrick StoleeSep 5, 2025
  46. Taylor BlauSep 11, 2025
  47. Junio C HamanoSep 11, 2025

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.