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

Re: [PATCH v2 1/3] merge-ll: use strbuf to read back external merge result

From
Jeff King <peff@peff.net>
Date
Sep 14, 2026, 16:53 UTC
Message-ID
<20260914165350.GA32247@peff.net>
In-Reply-To
<CABPp-BG9Hkc7i_JxAbYfyzu+b4Mc_pZUr0jJF=vY0jHSARpHzw@mail.gmail.com>
On Fri, Sep 11, 2026 at 11:06:33AM -0700, Elijah Newren wrote:
Show 18 quoted lines
> > -       result->size = st.st_size;
> > -       result->ptr = xmallocz(result->size);
> > -       if (read_in_full(fd, result->ptr, result->size) != result->size) {
> > -               FREE_AND_NULL(result->ptr);
> > -               result->size = 0;
> > +
> > +       if (strbuf_read_file(&result_buf, temp[1], 0) >= 0) {
> > +               result->size = result_buf.len;
> > +               result->ptr = strbuf_detach(&result_buf, NULL);
> 
> I know the type mismatch is pre-existing, but the order makes the new
> behavior different. On LLP64, assuming the usual wraparound, a result
> of LONG_MAX + 101  narrows to the negative value  LONG_MIN + 100 .
> 
> The old code narrows before  xmallocz() , so it requests an impossibly
> large allocation and dies. The new code allocates the actual buffer
> first, then records a negative size; callers converting that size back
> to size_t could read past the allocation.

Hmm, yeah. I noticed the possible truncation, but reasoned that it was roughly the same before and after (the only difference being that we know would actually have the full buffer, just a truncated size). But you're right that negative values introduce their own distinct type of confusion.

> Would a simple fail-fast make sense?
> 
> if (result_buf.len > LONG_MAX)
>         die(_("external merge result is too large"));

Yeah. I think we should be doing that even with the current code, as it's possible for us to silently truncate a merge result (e.g., wrapping beyond 4GB goes back to 0).

There's a similar case in read_mmfile(). There we actually bother to use xsize_t() to catch _some_ problems, but of course we are using "long" and not "size_t" in the mmfile, so it's still subject to truncation.

We can't just use read_mmfile() here, because there is an artificial distinction between mmfile_t and mmbuffer_t, even though they hold the exact same members (IIRC, one is for "output"). But possibly we can use it and just assign the members, which is no worse than what we have to do with the strbuf.

I'll plan to add a check like the one above here and in read_mmfile(), and then look at re-working this cleanup to use that function. I'll probably also peel this off of the other patches. It's really two separate topics: this file-read cleanup, and the tempfile-deletion improvement that started the thread.

-Peff
Previous: Junio C HamanoNext: Jeff King
Message 8 of 22 in “merge-ll: Cleanup merge driver temporaries after interrupt”
  1. merge-ll: Cleanup merge driver temporaries after interruptMichal Koutný, Sep 10, 2026
  2. Jeff KingSep 10, 2026
  3. Michal KoutnýSep 11, 2026
  4. 0/3 merge-ll: Cleanup merge driver temporaries afterJeff King, Sep 11, 2026
  5. 1/3 merge-ll: use strbuf to read back external merge resultJeff King, Sep 11, 2026
  6. Elijah NewrenSep 11, 2026
  7. Junio C HamanoSep 11, 2026
  8. Jeff KingSep 14, 2026
  9. 2/3 merge-ll: catch close() errors when writing external tempfilesJeff King, Sep 11, 2026
  10. Elijah NewrenSep 11, 2026
  11. Jeff KingSep 14, 2026
  12. 3/3 merge-ll: use tempfile API for external driver filesJeff King, Sep 11, 2026
  13. Elijah NewrenSep 11, 2026
  14. Michal KoutnýSep 14, 2026
  15. Jeff KingSep 14, 2026
  16. Michal KoutnýSep 14, 2026
  17. Jeff KingSep 14, 2026
  18. 0/2 merge-ll: Cleanup merge driver temporaries after signalJeff King, Sep 29, 2026
  19. 1/2 merge-ll: catch close() errors when writing external tempfilesJeff King, Sep 29, 2026
  20. 2/2 merge-ll: use tempfile API for external driver filesJeff King, Sep 29, 2026
  21. Junio C HamanoSep 29, 2026
  22. Jeff KingSep 29, 2026

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.