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 v2 4/5] wrapper: add read_in_full_or_error()
Date: Tue, 24 Nov 2026 06:24:03 -0800	[thread overview]
Message-ID: <xmqqpkvuwbng.fsf@gitster.g> (raw)
In-Reply-To: <ca30ed613d94ab11348e46ea60bc4f970e2f506c.1795472027.git.gitgitgadget@gmail.com> (Johannes Schindelin via GitGitGadget's message of "Mon, 23 Nov 2026 22:13:45 +0000")

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

> From: Johannes Schindelin <johannes.schindelin@gmx.de>

Thanks.  In this step, I only saw a few minor nits.

> Callers of the `read_in_full()` function want to read all requested
> bytes, or get an error. However, limiting the return value type to
> `ssize_t` is insufficient, as file sizes can easily exceed that type's
> range.
>
> For those callers that do not require the return value to report the
> number of read bytes, add a new function, `read_in_full_or_error()`,
> that returns a Boolean to indicate success or faiure.

It returns an int to indicate success or failure.  It is moral parallel
to returning a Boolean, but the polarity is reversed (0 is success, a
negative is failure, following our usual convention).

> Callers that do need to know that byte count can still call
> `read_in_full()`, but will now error out early if the requested byte
> count exceeds what `ssize_t` can represent.

"error out early" -> "abort to signal a programming error".

read_in_full() aborts with BUG() when the requested byte count exceeds
ssize_t max, instead of erroring out.  We declare that our API contract
is that count passed to read_in_full() must fit in ssize_t, so that any
positive return value from the function reliably means a successful
read, and in such a world, passing overly large count is a programming
error (i.e., the caller must be written to check) and aborting with
BUG() is perfetly fine.  Let's document that design decision here in the
log message.

Looking at the whole working tree after applying this series, all
remaining callers of read_in_full() are small or explicitly bounded
(setup.c, object-file.c, http-backend.c, pkt-line.c, odb/streaming.c).

Except that worktree.c:should_prune_worktree() passes size_t len that is
computed as xsize_t(st.st_size) for a .git gitdir file.  While gitdir
files are tiny in practice, converting it to read_in_full_or_error()
would be consistent with the other st.st_size conversions.

> +/*
> + * Read until count bytes have been read or EOF. On success, store the number
> + * read in out if non-NULL. Return false on an I/O error, with errno set.
> + */
> +static bool read_in_full_1(int fd, void *buf, size_t count, size_t *out)
>  {
>  	char *p = buf;
> -	ssize_t total = 0;
> +	size_t remaining = count;
>  
> -	while (count > 0) {
> -		ssize_t loaded = xread(fd, p, count);
> +	while (remaining) {
> +		ssize_t loaded = xread(fd, p, remaining);
>  		if (loaded < 0)
> -			return -1;
> -		if (loaded == 0)
> -			return total;
> -		count -= loaded;
> +			return false;
> +		if (!loaded)
> +			break;
> +		remaining -= loaded;
>  		p += loaded;
> -		total += loaded;
>  	}
>  
> -	return total;
> +	if (out)
> +		*out = count - remaining;

All two callers of this file-scope static helper passes &total, so this
reporting of the total count can and should be done unconditionally.

> +	return true;
> +}

  reply	other threads:[~2026-11-24 14:24 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 [this message]
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
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=xmqqpkvuwbng.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