git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
Cc: git@vger.kernel.org, Phillip Wood <phillip.wood123@gmail.com>
Subject: Re: [PATCH v2] sequencer: simplify allocation of result array in todo_list_rearrange_squash()
Date: Wed, 09 Aug 2023 13:51:51 -0700	[thread overview]
Message-ID: <xmqqsf8skkgo.fsf@gitster.g> (raw)
In-Reply-To: <20230809171532.2564880-1-oswald.buddenhagen@gmx.de> (Oswald Buddenhagen's message of "Wed, 9 Aug 2023 19:15:32 +0200")

Oswald Buddenhagen <oswald.buddenhagen@gmx.de> writes:

>  	if (rearranged) {
> +		ALLOC_ARRAY(items, todo_list->nr);
> +
>  		for (i = 0; i < todo_list->nr; i++) {
>  			enum todo_command command = todo_list->items[i].command;
>  			int cur = i;
> @@ -6357,16 +6359,15 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
>  				continue;
>  
>  			while (cur >= 0) {
> -				ALLOC_GROW(items, nr + 1, alloc);
>  				items[nr++] = todo_list->items[cur];
>  				cur = next[cur];
>  			}
>  		}
>  
> +		assert(nr == todo_list->nr);

The assert() made me wonder what kind of bugs we are worried about.

The next[] array elements are initialized to -1 and then gets "i"
that runs (0..todo_list->nr) and get shuffled among the elements of
the same array, so next[cur] reference in the while loop we see
above will always be within (0..todo_list->nr).  It is trivial to
see that the items[] array that is preallocated so that it can hold
up to todo_list->nr items is large enough.

But the condition of the assert() is even stronger.  We want to make
sure we did not drop any original items in the todo_list, as the
objective of this helper function is to shuffle the insns and
selectively turn "pick 01234567 fixup! title of another commit"
into "fixup 01234567 fixup! title of another commit".  There is no
reason we should have fewer elements in the result.

All makes sense.  Nice.

Thanks, will queue.




> +		todo_list->alloc = nr;
>  		FREE_AND_NULL(todo_list->items);
>  		todo_list->items = items;
> -		todo_list->nr = nr;
> -		todo_list->alloc = alloc;
>  	}
>  
>  	free(next);

      reply	other threads:[~2023-08-09 20:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-09 17:15 [PATCH v2] sequencer: simplify allocation of result array in todo_list_rearrange_squash() Oswald Buddenhagen
2023-08-09 20:51 ` Junio C Hamano [this message]

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=xmqqsf8skkgo.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=oswald.buddenhagen@gmx.de \
    --cc=phillip.wood123@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 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).