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

Re: [PATCH 2/4] Fix use-after-free warning with GCC at -O3

From
Patrick Steinhardt <ps@pks.im>
Date
Jun 4, 2025, 07:36 UTC
Message-ID
<aD_3ZzWSbyXIk81_@pks.im>
In-Reply-To
<20250603230646.2322671-2-mh@glandium.org>
On Wed, Jun 04, 2025 at 08:06:44AM +0900, Mike Hommey wrote:
Show 14 quoted lines
> ```
> reftable/basics.c: In function ‘parse_names’:
> reftable/basics.c:233:17: error: pointer ‘names’ may be used after ‘free’ [-Werror=use-after-free]
>   233 |                 reftable_free(names[i]);
>       |                 ^~~~~~~~~~~~~~~~~~~~~~~
> In function ‘reftable_free’,
>     inlined from ‘reftable_realloc’ at reftable/basics.c:30:3,
>     inlined from ‘reftable_realloc’ at reftable/basics.c:27:7,
>     inlined from ‘reftable_alloc_grow’ at reftable/basics.h:228:10,
>     inlined from ‘parse_names’ at reftable/basics.c:214:8:
> reftable/basics.c:44:17: note: call to ‘free’ here
>    44 |                 free(p);
>       |                 ^~~~~~~
> ```

Same here, only posting the warning isn't sufficient to explain what's going on.

Show 18 quoted lines
> diff --git a/reftable/basics.c b/reftable/basics.c
> index 9988ebd635..de21fe6ef7 100644
> --- a/reftable/basics.c
> +++ b/reftable/basics.c
> @@ -229,9 +229,11 @@ char **parse_names(char *buf, int size)
>  	return names;
>  
>  err:
> -	for (size_t i = 0; i < names_len; i++)
> -		reftable_free(names[i]);
> -	reftable_free(names);
> +	if (names) {
> +		for (size_t i = 0; i < names_len; i++)
> +			reftable_free(names[i]);
> +		reftable_free(names);
> +	}
>  	return NULL;
>  }

This change shouldn't be needed in theory: `names_len` has a positive value if and only if `names` is non-NULL. So the warning is a false positive.

That being said I'm not opposed to squelching this warning. But details like this should be explained in the commit message.

Patrick
Previous: Junio C HamanoNext: Patrick Steinhardt
Message 9 of 11 in “Fix maybe-uninitialized warning with GCC at -O3”
  1. 1/4 Fix maybe-uninitialized warning with GCC at -O3Mike Hommey, Jun 3, 2025
  2. 4/4 Fix unreachable-code warning with clang on WindowsMike Hommey, Jun 3, 2025
  3. Patrick SteinhardtJun 4, 2025
  4. Junio C HamanoJun 4, 2025
  5. 3/4 Fix comma warnings with clang on WindowsMike Hommey, Jun 3, 2025
  6. Junio C HamanoJun 3, 2025
  7. 2/4 Fix use-after-free warning with GCC at -O3Mike Hommey, Jun 3, 2025
  8. Junio C HamanoJun 3, 2025
  9. Patrick SteinhardtJun 4, 2025
  10. Patrick SteinhardtJun 4, 2025
  11. Jeff KingJun 6, 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.