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

Re: [PATCH] Fix some sparse warnings

From
Stefan Beller <stefanbeller@googlemail.com>
Date
Jul 17, 2013, 22:08 UTC
Message-ID
<51E715D8.9040307@googlemail.com>
In-Reply-To
<6BDA2E3E7318418BBB2C19B475B2B118@PhilipOakley>
On 07/16/2013 10:53 PM, Philip Oakley wrote:
Show 6 quoted lines
> 
> Does anyone run the "new static checker called 'Stack' that precisely
> identifies unstable code"? [though the paper's conclusion says 'All
> Stack source code will be publicly available.' which suggests it's not
> yet available]
> 

So I started using the clang code analyzer on git. One of the first warnings actually is this:

object.c:241:7: warning: Branch condition evaluates to a garbage value
                if (!eaten)
So that part of object.c lookx like this:
	struct object *parse_object(const unsigned char *sha1) 
	{
		int eaten;
		...
		obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
		if (!eaten)
			free(buffer);
	}
And the parse_object_buffer looks like this with respect to the eaten 
variable:
	struct object *parse_object_buffer(...)
	{
		int eaten = 0;
		if (something)
			return NULL;
		...
		if (something_different)
			eaten=1;
		*eaten_p = eaten;
	}
So what might happen is, that parse_object_buffer exits early, without
executing 
	
	*eaten_p = eaten;

Then in the parse_object function eaten was never initialized nor set inside the call to parse_object_buffer. Then it is obvious that the free(buffer) is executed depending on garbage left on the stack. Definitely something what we want to change.

The obvious way to repair this would be to just initialize the eaten variable
inside parse_object.
	struct object *parse_object(const unsigned char *sha1) 
	{
		int eaten=0;
		...

However I'd like to propose another solution: In parse_object_buffer we do not have a local eaten variable, but directly write to *eaten_p. That would be the following patch.

Was there a particular idea or goal behind first having a local eaten variable, which later near the correct return of the function was used to set the eaten_p?

Thanks, Stefan

Previous: Johannes SixtNext: Stefan Beller
Message 8 of 12 in “Fix some sparse warnings”
  1. Fix some sparse warningsRamsay Jones, Jul 15, 2013
  2. Johannes SixtJul 16, 2013
  3. Jeff KingJul 16, 2013
  4. Philip OakleyJul 16, 2013
  5. Stefan BellerJul 16, 2013
  6. Philip OakleyJul 16, 2013
  7. Johannes SixtJul 17, 2013
  8. Stefan BellerJul 17, 2013
  9. parse_object_buffer: Correct freeing the buffer.Stefan Beller, Jul 17, 2013
  10. Stefan BellerJul 17, 2013
  11. Junio C HamanoJul 17, 2013
  12. Ramsay JonesJul 18, 2013

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.