Skip to content

Commit 8397398

Browse files
committed
diff: plug leaks in dirstat
The array of dirstat_file contained in the dirstat_dir structure is not freed after the processing ends. Unfortunately, the member that points at the array, .files, is incremented as the gather_dirstat() function recursively walks it, and this needs to be plugged by remembering the beginning of the array before gather_dirstat() mucks with it and freeing it after we are done. We can mark t4047 as leak-free. t4000, which is marked as leak-free, now can exercise dirstat in it, which will happen next. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 34a9489 commit 8397398

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

diff.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,13 +2975,18 @@ static void conclude_dirstat(struct diff_options *options,
29752975
struct dirstat_dir *dir,
29762976
unsigned long changed)
29772977
{
2978-
/* This can happen even with many files, if everything was renames */
2979-
if (!changed)
2980-
return;
2978+
struct dirstat_file *to_free = dir->files;
2979+
2980+
if (!changed) {
2981+
/* This can happen even with many files, if everything was renames */
2982+
;
2983+
} else {
2984+
/* Show all directories with more than x% of the changes */
2985+
QSORT(dir->files, dir->nr, dirstat_compare);
2986+
gather_dirstat(options, dir, changed, "", 0);
2987+
}
29812988

2982-
/* Show all directories with more than x% of the changes */
2983-
QSORT(dir->files, dir->nr, dirstat_compare);
2984-
gather_dirstat(options, dir, changed, "", 0);
2989+
free(to_free);
29852990
}
29862991

29872992
static void show_dirstat(struct diff_options *options)

t/t4047-diff-dirstat.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='diff --dirstat tests'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
# set up two commits where the second commit has these files

0 commit comments

Comments
 (0)