Skip to content

Commit e5a917f

Browse files
avargitster
authored andcommitted
unpack-trees: don't leak memory in verify_clean_subdirectory()
Fix two different but related memory leaks in verify_clean_subdirectory(). We leaked both the "pathbuf" if read_directory() returned non-zero, and we never cleaned up our own "struct dir_struct" either. * "pathbuf": When the read_directory() call followed by the free(pathbuf) was added in c819353 (Fix switching to a branch with D/F when current branch has file D., 2007-03-15) we didn't bother to free() before we called die(). But when this code was later libified in 203a2fe (Allow callers of unpack_trees() to handle failure, 2008-02-07) we started to leak as we returned data to the caller. This fixes that memory leak, which can be observed under SANITIZE=leak with e.g. the "t1001-read-tree-m-2way.sh" test. * "struct dir_struct": We've leaked the dir_struct ever since this code was added back in c819353. When that commit was written there wasn't an equivalent of dir_clear(). Since it was added in 270be81 (dir.c: provide clear_directory() for reclaiming dir_struct memory, 2013-01-06) we've omitted freeing the memory allocated here. This memory leak could also be observed under SANITIZE=leak and the "t1001-read-tree-m-2way.sh" test. This makes all the test in "t1001-read-tree-m-2way.sh" pass under "GIT_TEST_PASSING_SANITIZE_LEAK=true", we'd previously die in tests 25, 26 & 28. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9d05b45 commit e5a917f

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

t/t1001-read-tree-m-2way.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ In the test, these paths are used:
2020
rezrov - in H, deleted in M
2121
yomin - not in H or M
2222
'
23+
24+
TEST_PASSES_SANITIZE_LEAK=true
2325
. ./test-lib.sh
2426
. "$TEST_DIRECTORY"/lib-read-tree.sh
2527

unpack-trees.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2136,9 +2136,10 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
21362136
if (o->dir)
21372137
d.exclude_per_dir = o->dir->exclude_per_dir;
21382138
i = read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);
2139+
dir_clear(&d);
2140+
free(pathbuf);
21392141
if (i)
21402142
return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
2141-
free(pathbuf);
21422143
return cnt;
21432144
}
21442145

0 commit comments

Comments
 (0)