Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,  Phillip Wood <phillip.wood123@gmail.com>,
	 Patrick Steinhardt <ps@pks.im>,
	 Harald Nordgren <haraldnordgren@gmail.com>,
	Simon Richter <Simon.Richter@hogyros.de>,
	 Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: Re: [PATCH v3 3/5] wrapper: avoid byte-count overflow in write_in_full()
Date: Wed, 25 Nov 2026 15:47:38 -0800	[thread overview]
Message-ID: <xmqq33sowk11.fsf@gitster.g> (raw)
In-Reply-To: <f38c7e71337632f2e86ef5716e90c776c491b762.1795640149.git.gitgitgadget@gmail.com> (Johannes Schindelin via GitGitGadget's message of "Wed, 25 Nov 2026 20:55:47 +0000")

"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Johannes Schindelin <johannes.schindelin@gmx.de>
>
> Accumulating the output count in `write_in_full()` can overflow because
> it counts via a `ssize_t` variable which can overflow for valid `size_t`
> sizes passed as input.
>
> Since this function either completes the requested count or fails,
> callers already know how many bytes were written on success. Let's just
> return status instead, so the total transfer size need not fit in
> `ssize_t`.

This is a great simplification.

"git grep write_in_full(" shows quite a lot of hits.

 - Surprising number of them are totally unchecked, calledin the void
   context.

 - Many are in the form:

	if (write_in_full(...) < 0)

   and presumably this update to return -1 on error should be safe on them.

And the rest would need to be audited and updated, like this one:

> diff --git a/imap-send.c b/imap-send.c
> index bf1d2cf74d..dccc2264de 100644
> --- a/imap-send.c
> +++ b/imap-send.c
> @@ -392,7 +392,7 @@ static int socket_write(struct imap_socket *sock, const char *buf, int len)
>  		n = SSL_write(sock->ssl, buf, len);
>  	else
>  #endif
> -		n = write_in_full(sock->fd[1], buf, len);
> +		n = write_in_full(sock->fd[1], buf, len) < 0 ? -1 : len;
>  	if (n != len) {

which looks correct.

> diff --git a/notes-merge.c b/notes-merge.c
> index d361e70a48..991490a187 100644
> --- a/notes-merge.c
> +++ b/notes-merge.c
> @@ -319,17 +319,8 @@ static void write_buf_to_worktree(const struct object_id *obj,
>  
>  	fd = xopen(path, O_WRONLY | O_EXCL | O_CREAT, 0666);
>  
> -	while (size > 0) {
> -		ssize_t ret = write_in_full(fd, buf, size);
> -		if (ret < 0) {
> -			/* Ignore epipe */
> -			if (errno == EPIPE)
> -				break;
> -			die_errno("notes-merge");
> -		}
> -		size -= ret;
> -		buf += ret;
> -	}
> +	if (write_in_full(fd, buf, size) < 0 && errno != EPIPE)
> +		die_errno("notes-merge");

Hmph, what is going on here.  Did the original not know write_in_full()
would not return until it fails or writes everything?  It comes from
809f38c8ab (git notes merge: Manual conflict resolution, part 1/2,
2010-11-09) that is ancient, and was touched by Peff in 634eb82b1a
(notes-merge: use ssize_t for write_in_full() return value, 2017-09-13)
to correct the type from "long ret" to "ssize_t ret".  I guess nobody
noticed when these two patches was reviewed.

I didn't check what is *not* touched by the patch, trusting your LLM did
the heavylifting for you ;-).

Thanks.

  reply	other threads:[~2026-11-25 23:47 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14  6:09 [PATCH 0/5] Address overflows pointed out by Coverity Johannes Schindelin via GitGitGadget
2026-10-14  6:09 ` [PATCH 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-10-14  6:09 ` [PATCH 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-10-16  8:38   ` Patrick Steinhardt
2026-10-16 14:30     ` Junio C Hamano
2026-10-17  0:18       ` Simon Richter
2026-10-17  4:10         ` Junio C Hamano
2026-10-17  4:11         ` Junio C Hamano
2026-10-14  6:09 ` [PATCH 3/5] wrapper: reject insanely large byte counts when reading/writing Johannes Schindelin via GitGitGadget
2026-10-14 11:37   ` Phillip Wood
2026-11-15 19:24     ` Johannes Schindelin
2026-10-14  6:09 ` [PATCH 4/5] git-compat-util: add `signed_mult_overflows` and `mult_overflows` macros Johannes Schindelin via GitGitGadget
2026-10-15  9:27   ` Phillip Wood
2026-11-15 19:29     ` Johannes Schindelin
2026-10-14  6:09 ` [PATCH 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-10-14 21:44   ` Junio C Hamano
2026-10-15  9:28   ` Phillip Wood
2026-11-15 19:35     ` Johannes Schindelin
2026-10-16  8:43 ` [PATCH 0/5] Address overflows pointed out by Coverity Harald Nordgren
2026-11-07 22:36 ` Junio C Hamano
2026-11-23 22:13 ` [PATCH v2 " Johannes Schindelin via GitGitGadget
2026-11-23 22:13   ` [PATCH v2 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-11-24 17:34     ` Junio C Hamano
2026-11-23 22:13   ` [PATCH v2 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-11-24 17:54     ` Junio C Hamano
2026-11-25 14:04       ` Johannes Schindelin
2026-11-23 22:13   ` [PATCH v2 3/5] wrapper: avoid byte-count overflow in write_in_full() Johannes Schindelin via GitGitGadget
2026-11-24 14:37     ` Phillip Wood
2026-11-25 14:20       ` Johannes Schindelin
2026-11-23 22:13   ` [PATCH v2 4/5] wrapper: add read_in_full_or_error() Johannes Schindelin via GitGitGadget
2026-11-24 14:24     ` Junio C Hamano
2026-11-23 22:13   ` [PATCH v2 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-11-24 14:44     ` Phillip Wood
2026-11-24 18:04   ` [PATCH v2 0/5] Address overflows pointed out by Coverity Junio C Hamano
2026-11-25 20:55 ` [PATCH v3 " Johannes Schindelin via GitGitGadget
2026-11-25 20:55   ` [PATCH v3 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-11-25 20:55   ` [PATCH v3 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-11-25 20:55   ` [PATCH v3 3/5] wrapper: avoid byte-count overflow in write_in_full() Johannes Schindelin via GitGitGadget
2026-11-25 23:47     ` Junio C Hamano [this message]
2026-11-26 15:14       ` Phillip Wood
2026-11-25 20:55   ` [PATCH v3 4/5] wrapper: add read_in_full_or_error() Johannes Schindelin via GitGitGadget
2026-11-25 23:57     ` Junio C Hamano
2026-11-25 20:55   ` [PATCH v3 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-11-25 22:08     ` Junio C Hamano
2026-11-26 14:08     ` Aniket Gupta

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=xmqq33sowk11.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=Simon.Richter@hogyros.de \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=haraldnordgren@gmail.com \
    --cc=johannes.schindelin@gmx.de \
    --cc=phillip.wood123@gmail.com \
    --cc=ps@pks.im \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox