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

Re: [PATCH 5/5] refspec.c: use rhs in parse_refspec instead of potentially uninitialized item->dst

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 1, 2018, 02:01 UTC
Message-ID
<xmqqzi0f9see.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20180530170433.191495-1-sbeller@google.com>
Stefan Beller <sbeller@google.com> writes:
> 'item->dst' has not been assigned if '!rhs' is true. As the caller is allowed to pass in uninitialized
> memory (we don't assume 'item' was zeroed out before calling), this fixes an access to
> uninitialized memory.
Did I miss the other 4 patches that this might depend on it?
Show 13 quoted lines
> diff --git a/refspec.c b/refspec.c
> index c59a4ccf1e5..ea169dec0d3 100644
> --- a/refspec.c
> +++ b/refspec.c
> @@ -108,7 +108,7 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
>  		 * - empty is not allowed.
>  		 * - otherwise it must be a valid looking ref.
>  		 */
> -		if (!item->dst) {
> +		if (!rhs) {
>  			if (check_refname_format(item->src, flags))
>  				return 0;
>  		} else if (!*item->dst) {

Perhaps a better fisx is to explicitly assign NULL to item->dst when we see there is no right-hand-side.

Aside from the "uninitialized" issue, the original if/else cascade around here makes a lot more sense than the updated version. If we do not leave item->dst uninitialized, the control (and the readers' understanding) can flow without having to carry the invariant "item->dst is set ONLY when rhs != NULL" throughout this codepath, in order to understand that this if/else cascade is asking: is pointer NULL? then do one thing, otherwise is pointee NUL? then do another thing, otherwise we have a non-empty string so do something on it.

Previous: Stefan BellerNext: Junio C Hamano
Message 2 of 4 in “refspec.c: use rhs in parse_refspec instead of potentially uninitialized item->dst”
  1. 5/5 refspec.c: use rhs in parse_refspec instead of potentially uninitialized item->dstStefan Beller, May 30, 2018
  2. Junio C HamanoJun 1, 2018
  3. Junio C HamanoJun 1, 2018
  4. Stefan BellerJun 1, 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.