Skip to content

Commit ad4544b

Browse files
committed
dir: free allocations on parse-error paths in read_one_dir()
When read_one_dir() encounters a parse error while reading the untracked cache from disk, it returns -1 immediately. Two allocations made earlier in the function can leak on these early-return paths: ud.untracked (allocated at line 3846 when untracked_nr > 0) and ud.dirs (allocated at line 3851). Free both before returning on the two error paths between these allocations and the point where they are transferred into the final xmalloc'd struct at line 3857. Pointed out by Coverity. Assisted-by: Claude Opus 4.6 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 7e9d856 commit ad4544b

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

dir.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3791,13 +3791,18 @@ static int read_one_dir(struct untracked_cache_dir **untracked_,
37913791
ALLOC_ARRAY(ud.untracked, ud.untracked_nr);
37923792

37933793
ud.dirs_alloc = ud.dirs_nr = decode_varint(&data);
3794-
if (data > end)
3794+
if (data > end) {
3795+
free(ud.untracked);
37953796
return -1;
3797+
}
37963798
ALLOC_ARRAY(ud.dirs, ud.dirs_nr);
37973799

37983800
eos = memchr(data, '\0', end - data);
3799-
if (!eos || eos == end)
3801+
if (!eos || eos == end) {
3802+
free(ud.untracked);
3803+
free(ud.dirs);
38003804
return -1;
3805+
}
38013806

38023807
*untracked_ = untracked = xmalloc(st_add3(sizeof(*untracked), eos - data, 1));
38033808
memcpy(untracked, &ud, sizeof(ud));

0 commit comments

Comments
 (0)