git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: Justin Tobler <jltobler@gmail.com>
Cc: git@vger.kernel.org, Jeff King <peff@peff.net>
Subject: Re: [PATCH 6/9] builtin/log: use `size_t` to track indices
Date: Fri, 3 Jan 2025 07:43:13 +0100	[thread overview]
Message-ID: <Z3eHAWnkrwyGqtki@pks.im> (raw)
In-Reply-To: <3b7ep2hex2vae56p2ba2kjeextjrjcsu5jrufr7hqewrnv3z45@ftrrcuvihxxs>

On Thu, Jan 02, 2025 at 07:58:24PM -0600, Justin Tobler wrote:
> On 24/12/27 11:46AM, Patrick Steinhardt wrote:
> > Similar as with the preceding commit, adapt "builtin/log.c" so that it
> > tracks array indices via `size_t` instead of using signed integers. This
> > fixes a couple of -Wsign-compare warnings and prepares the code for
> > a similar refactoring of `repo_get_merge_bases_many()` in a subsequent
> > commit.
> > 
> > Signed-off-by: Patrick Steinhardt <ps@pks.im>
> > ---
> >  builtin/log.c | 23 +++++++++++++----------
> >  1 file changed, 13 insertions(+), 10 deletions(-)
> > 
> [snip]
> >  	if (show_progress)
> >  		progress = start_delayed_progress(_("Generating patches"), total);
> > -	while (0 <= --nr) {
> > +	for (i = 0; i < nr; i++) {
> > +		size_t idx = nr - i - 1;
> >  		int shown;
> > -		display_progress(progress, total - nr);
> > -		commit = list[nr];
> > -		rev.nr = total - nr + (start_number - 1);
> > +
> > +		display_progress(progress, total - idx);
> > +		commit = list[idx];
> > +		rev.nr = total - idx + (start_number - 1);
> 
> Along with updating array indices variables to use `size_t`, the loop
> structure here is also changed. Instead of iterating backwards from
> `nr`, the loop iterator increases and each iteration computes the index
> starting from the end. This is functionally the same behavior and it
> looks like it was done to improve readability.

It wasn't really done to improve readability, but to retain correctness.
Before this change we relied on `nr` being signed, and thus it could
also have a negative value. Now that it's a `size_t` it would overflow
eventually and that would make the loop condition a tautology. So I had
to refactor the condition so that it doesn't rely on such an overflow
anymore.

Patrick

  reply	other threads:[~2025-01-03  6:43 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-27 10:46 [PATCH 0/9] commit-reach: -Wsign-compare follow-ups Patrick Steinhardt
2024-12-27 10:46 ` [PATCH 1/9] prio-queue: fix type of `insertion_ctr` Patrick Steinhardt
2024-12-27 14:36   ` Jeff King
2024-12-27 10:46 ` [PATCH 2/9] commit-reach: fix index used to loop through unsigned integer Patrick Steinhardt
2024-12-27 14:46   ` Jeff King
2024-12-27 10:46 ` [PATCH 3/9] commit-reach: fix type of `min_commit_date` Patrick Steinhardt
2024-12-27 10:46 ` [PATCH 4/9] commit-reach: use `size_t` to track indices in `remove_redundant()` Patrick Steinhardt
2025-01-03  1:46   ` Justin Tobler
2024-12-27 10:46 ` [PATCH 5/9] commit-reach: use `size_t` to track indices in `get_reachable_subset()` Patrick Steinhardt
2024-12-27 10:46 ` [PATCH 6/9] builtin/log: use `size_t` to track indices Patrick Steinhardt
2025-01-03  1:58   ` Justin Tobler
2025-01-03  6:43     ` Patrick Steinhardt [this message]
2024-12-27 10:46 ` [PATCH 7/9] builtin/log: fix remaining -Wsign-compare warnings Patrick Steinhardt
2024-12-27 13:21   ` shejialuo
2024-12-27 13:57     ` Patrick Steinhardt
2024-12-27 14:03       ` shejialuo
2024-12-27 10:46 ` [PATCH 8/9] shallow: fix " Patrick Steinhardt
2024-12-27 10:46 ` [PATCH 9/9] commit-reach: use `size_t` to track indices when computing merge bases Patrick Steinhardt
2025-01-03  2:08   ` Justin Tobler
2025-01-03  6:43     ` Patrick Steinhardt
2024-12-27 19:47 ` [PATCH 0/9] commit-reach: -Wsign-compare follow-ups Junio C Hamano
2024-12-27 20:08   ` Junio C Hamano
2024-12-27 21:37     ` Jeff King
2024-12-28  8:41       ` Patrick Steinhardt
2024-12-28  0:00     ` Junio C Hamano
2024-12-28  8:27       ` Patrick Steinhardt
2024-12-28 15:38         ` Junio C Hamano
2024-12-28 19:05         ` racy leak sanitizer builds, was " Jeff King
2024-12-28 19:23           ` Jeff King
2024-12-28 19:31             ` Jeff King
2024-12-29 12:02             ` René Scharfe
2024-12-29 16:57               ` Jeff King
2024-12-30  4:32                 ` Jeff King
2024-12-30 13:46                 ` Junio C Hamano

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=Z3eHAWnkrwyGqtki@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=jltobler@gmail.com \
    --cc=peff@peff.net \
    /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;
as well as URLs for NNTP newsgroup(s).