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

From: Junio C Hamano <[email-masked]>
Date: Tue, 03 Jun 2025 16:47:53 -0700

Mike Hommey <[email-masked]> writes:

> Cc: [email-masked], Mike Hommey <[email-masked]>

Not to me, but to the designated area expert.

Thanks.

> ```
> 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);
>       |                 ^~~~~~~
> ```
>
> Signed-off-by: Mike Hommey <[email-masked]>
> ---
>  reftable/basics.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
>
> 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;
>  }