Re: [PATCH 07/13] dir: free allocations on parse-error paths in read_one_dir()
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Jul 1, 2026, 07:56 UTC
- Message-ID
- <akTIOeXFhNjJ7V3i@pks.im>
- In-Reply-To
- <62ce03454aa1928edd8fa538e0600155629939cd.1782889472.git.gitgitgadget@gmail.com>
On Wed, Jul 01, 2026 at 07:04:25AM +0000, Johannes Schindelin via GitGitGadget wrote:
Show 25 quoted lines
> diff --git a/dir.c b/dir.c
> index 32430090dc..23335b9f7a 100644
> --- a/dir.c
> +++ b/dir.c
> @@ -3792,13 +3792,18 @@ static int read_one_dir(struct untracked_cache_dir **untracked_,
> ALLOC_ARRAY(ud.untracked, ud.untracked_nr);
>
> ud.dirs_alloc = ud.dirs_nr = decode_varint(&data);
> - if (data > end)
> + if (data > end) {
> + free(ud.untracked);
> return -1;
> + }
> ALLOC_ARRAY(ud.dirs, ud.dirs_nr);
>
> eos = memchr(data, '\0', end - data);
> - if (!eos || eos == end)
> + if (!eos || eos == end) {
> + free(ud.untracked);
> + free(ud.dirs);
> return -1;
> + }
>
> *untracked_ = untracked = xmalloc(st_add3(sizeof(*untracked), eos - data, 1));
> memcpy(untracked, &ud, sizeof(ud));Hm. Here we assign ownership to the caller, but this still feels quite off to me as we also have two more early returns after this point that seem to leak memory. Do the callers make sure to always free the data?
Patrick