All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Elijah Newren <newren@gmail.com>
Cc: "Michal Koutný" <mkoutny@suse.com>,
	git@vger.kernel.org, "Jean Delvare" <jdelvare@suse.de>,
	"Usman Akinyemi" <usmanakinyemi202@gmail.com>,
	"Taylor Blau" <me@ttaylorr.com>,
	"Junio C Hamano" <gitster@pobox.com>,
	"René Scharfe" <l.s.r@web.de>
Subject: Re: [PATCH v2 1/3] merge-ll: use strbuf to read back external merge result
Date: Mon, 14 Sep 2026 12:53:50 -0400	[thread overview]
Message-ID: <20260914165350.GA32247@peff.net> (raw)
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:

> > -       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

  parent reply	other threads:[~2026-09-14 16:53 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-10 15:06 [PATCH] merge-ll: Cleanup merge driver temporaries after interrupt Michal Koutný
2026-09-10 16:22 ` Jeff King
2026-09-11 14:43   ` Michal Koutný
2026-09-11 17:10     ` [PATCH v2 0/3] merge-ll: Cleanup merge driver temporaries after Jeff King
2026-09-11 17:11       ` [PATCH v2 1/3] merge-ll: use strbuf to read back external merge result Jeff King
2026-09-11 18:06         ` Elijah Newren
2026-09-11 18:32           ` Junio C Hamano
2026-09-14 16:53           ` Jeff King [this message]
2026-09-11 17:11       ` [PATCH v2 2/3] merge-ll: catch close() errors when writing external tempfiles Jeff King
2026-09-11 18:06         ` Elijah Newren
2026-09-14 16:56           ` Jeff King
2026-09-11 17:13       ` [PATCH v2 3/3] merge-ll: use tempfile API for external driver files Jeff King
2026-09-11 18:10         ` Elijah Newren
2026-09-14 13:24           ` Michal Koutný
2026-09-14 16:57             ` Jeff King
2026-09-14 13:23       ` [PATCH v2 0/3] merge-ll: Cleanup merge driver temporaries after Michal Koutný
2026-09-14 16:59         ` Jeff King
2026-09-29  5:12       ` [PATCH v3 0/2] merge-ll: Cleanup merge driver temporaries after signal Jeff King
2026-09-29  5:12         ` [PATCH v3 1/2] merge-ll: catch close() errors when writing external tempfiles Jeff King
2026-09-29  5:13         ` [PATCH v3 2/2] merge-ll: use tempfile API for external driver files Jeff King
2026-09-29 16:51           ` Junio C Hamano
2026-09-29 18:25             ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260914165350.GA32247@peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jdelvare@suse.de \
    --cc=l.s.r@web.de \
    --cc=me@ttaylorr.com \
    --cc=mkoutny@suse.com \
    --cc=newren@gmail.com \
    --cc=usmanakinyemi202@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.