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

Re: [PATCH] sha1_name: fix uninitialized memory errors

From
Jeff King <peff@peff.net>
Date
Feb 26, 2018, 20:41 UTC
Message-ID
<20180226204151.GA12598@sigill.intra.peff.net>
In-Reply-To
<1519657007-215623-1-git-send-email-dstolee@microsoft.com>
On Mon, Feb 26, 2018 at 09:56:47AM -0500, Derrick Stolee wrote:
Show 17 quoted lines
> diff --git a/sha1_name.c b/sha1_name.c
> index 611c7d2..44dd595 100644
> --- a/sha1_name.c
> +++ b/sha1_name.c
> @@ -546,17 +546,12 @@ static void find_abbrev_len_for_pack(struct packed_git *p,
>  	 * nearby for the abbreviation length.
>  	 */
>  	mad->init_len = 0;
> -	if (!match) {
> -		nth_packed_object_oid(&oid, p, first);
> +	if (!match && nth_packed_object_oid(&oid, p, first))
>  		extend_abbrev_len(&oid, mad);
> -	} else if (first < num - 1) {
> -		nth_packed_object_oid(&oid, p, first + 1);
> +	else if (first < num - 1 && nth_packed_object_oid(&oid, p, first + 1))
>  		extend_abbrev_len(&oid, mad);
> -	}

I think including the nth_packed_object_oid() in the main if-else chain works out, but it's kind of tricky.

In the code before, we'd hit the "first < num - 1" conditional only when we didn't match something. But now we also hit it if we _did_ match something, but nth_packed_object_oid() didn't work.

But this works out the same if we assume any match must also succeed at nth_packed_object_oid(). Which in turn implies that checking the result of nth_packed_object_oid() in the "else if" is redundant (though we already clamp it to "num - 1", so we'd expect it to always succeed anyway).

So I think this behaves well, but I wonder if the two-level conditionals like:

  if (!match) {
	if (nth_packed_object_oid(&oid, p, first))
		extend_abbrev_len(&oid, mad);
  } else if ...
are easier to reason about.
-Peff
Previous: Derrick StoleeNext: Derrick Stolee
Message 7 of 14 in “Use of uninitialised value of size 8 in sha1_name.c”
  1. Christian CouderFeb 26, 2018
  2. Jeff KingFeb 26, 2018
  3. Christian CouderFeb 26, 2018
  4. Derrick StoleeFeb 26, 2018
  5. Christian CouderFeb 26, 2018
  6. sha1_name: fix uninitialized memory errorsDerrick Stolee, Feb 26, 2018
  7. Jeff KingFeb 26, 2018
  8. sha1_name: fix uninitialized memory errorsDerrick Stolee, Feb 27, 2018
  9. Jeff KingFeb 27, 2018
  10. Junio C HamanoFeb 27, 2018
  11. Jeff KingFeb 27, 2018
  12. Junio C HamanoFeb 28, 2018
  13. Derrick StoleeFeb 28, 2018
  14. Junio C HamanoFeb 28, 2018

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.