git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] remote: initialize values that might not be set

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 7, 2021, 23:21 UTC
Message-ID
<xmqqlf7ltg4q.fsf@gitster.g>
In-Reply-To
<nycvar.QRO.7.76.6.2106072355110.55@tvgsbejvaqbjf.bet>
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 19 quoted lines
>> diff --git a/remote.c b/remote.c
>> index c3f85c17ca7c..a116392fb057 100644
>> --- a/remote.c
>> +++ b/remote.c
>> @@ -2101,7 +2101,7 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
>>  int format_tracking_info(struct branch *branch, struct strbuf *sb,
>>  			 enum ahead_behind_flags abf)
>>  {
>> -	int ours, theirs, sti;
>> +	int ours = 0, theirs = 0, sti = 0;
>
> While I like this change, I am somewhat confused where the values are used
> for branching. The only time I see them used when `stat_branch_pair()` has
> _not_ initialized `ours` and `theirs` is in those `trace2_data_intmax()`
> calls. Otherwise `sti` is set to -1 and the other users of `ours` and
> `theirs` aren't reached.
>
> If my reading of the code is correct, maybe the commit message could be
> adjusted to talk about tracing instead of branching?

I too wondered why initializing them to 0 is safe (instead of hiding latent bugs). I think that stat_tracking_info() would always return -1 if returns before reaching the point in stat_branch_pair(), but it is not clear how we can futureproof the whole thing.

If these two are initialized to say -1 here, and then we had some sanity check, perhaps like so:

	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	assert(sti < 0 || (0 <= ours && 0 <= theirs));
	if (sti < 0) {
		if (!full_base)
	...

to enforce the invariant we assume (i.e. OK sti means ours and theirs are set), it would allow us to sleep better, perhaps?

Previous: Johannes SchindelinNext: Johannes Schindelin
Message 3 of 6 in “remote: initialize values that might not be set”
  1. remote: initialize values that might not be setDerrick Stolee via GitGitGadget, Jun 7, 2021
  2. Johannes SchindelinJun 7, 2021
  3. Junio C HamanoJun 7, 2021
  4. Johannes SchindelinJun 10, 2021
  5. Junio C HamanoJun 11, 2021
  6. Derrick StoleeJun 11, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.