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

Re: [PATCH 1/4] cat_one_file(): make it easy to see that the `size` variable is initialized

From
Jeff King <peff@peff.net>
Date
Mar 28, 2025, 03:46 UTC
Message-ID
<20250328034628.GA3067273@coredump.intra.peff.net>
In-Reply-To
<b5e98d5e144f4b3a2771a421690b4b729dc1b7ae.1743079429.git.gitgitgadget@gmail.com>
On Thu, Mar 27, 2025 at 12:43:46PM +0000, Johannes Schindelin via GitGitGadget wrote:
Show 9 quoted lines
> From: Johannes Schindelin <johannes.schindelin@gmx.de>
> 
> The large `switch` statement makes it a bit impractical to reason about
> the code.
> 
> One of the code paths can technically lead to using `size` without being
> initialized: if the `t` case is taken and the type name is set to the
> empty string, we would actually leave `size` unintialized right until we
> use it.

I don't think that's quite true. If we have an empty type name we leave the switch and hit these lines:

	if (!buf)
		die("git cat-file %s: bad file", obj_name);
	write_or_die(1, buf, size);

Since we set buf to NULL before the switch and never touch it in the 't' case, we'll always hit that die() call.

So this really is a false positive, regardless of what happens to the type name buffer. I'm a little surprised that CodeQL would get this wrong, just because it is very easy to see that buf is not touched in the 't' case at all (and thus must be NULL). But maybe I'm missing something.

I do agree that the flow through the switch statement (where "break" is good for some cases and a failure mode for others) makes this code rather hard to reason about. I'm sure it could be rewritten, but I'm not sure if it's worth spending time on.

Show 5 quoted lines
> Practically, this cannot happen because the
> `do_oid_object_info_extended()` function is expected to always populate
> the `type_name` if asked for. However, it is quite unnecessary to leave
> the code as unwieldy to reason about: Just initialize the variable to 0
> and be done with it.
You can trigger the path in question like this:
  oid=$(echo foo | git hash-object --literally --stdin -w -t '')
  git cat-file --allow-unknown -t $oid
which hits the "bad file" message.

(Obviously the above is horrible and arguably something we should consider forbidding; I have some patches moving towards ripping out support for non-standard types entirely).

Show 10 quoted lines
> diff --git a/builtin/cat-file.c b/builtin/cat-file.c
> index b13561cf73b..128c901fa8e 100644
> --- a/builtin/cat-file.c
> +++ b/builtin/cat-file.c
> @@ -104,7 +104,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
>  	struct object_id oid;
>  	enum object_type type;
>  	char *buf;
> -	unsigned long size;
> +	unsigned long size = 0;

So even though I think your analysis above had a few wrong details, I do agree this is a false positive in CodeQL and is probably OK to fix as you do here. Though it might make more sense to do it alongside the assignment to "buf" (or to move the initialization of "buf" up here).

-Peff
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 3 of 10 in “Initialize a few uninitialized variables”
  1. 0/4 Initialize a few uninitialized variablesJohannes Schindelin via GitGitGadget, Mar 27, 2025
  2. 1/4 cat_one_file(): make it easy to see that the `size` variable is initializedJohannes Schindelin via GitGitGadget, Mar 27, 2025
  3. Jeff KingMar 28, 2025
  4. 2/4 fsck: avoid using an uninitialized variableJohannes Schindelin via GitGitGadget, Mar 27, 2025
  5. Jeff KingMar 28, 2025
  6. 3/4 load_revindex_from_disk(): avoid accessing uninitialized dataJohannes Schindelin via GitGitGadget, Mar 27, 2025
  7. Taylor BlauMar 27, 2025
  8. 4/4 load_pack_mtimes_file(): avoid accessing uninitialized dataJohannes Schindelin via GitGitGadget, Mar 27, 2025
  9. Taylor BlauMar 27, 2025
  10. Johannes SchindelinJul 17, 2025

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.