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 <gitster@pobox.com>

Junio C Hamano committed May 5, 2023 at 14:19 UTC 83973981eb475ce90f829f8a5bd6ea99cd3bbd8e
2 files changed +13 -6
diff.c
+11 -6
@@ -2975,13 +2975,18 @@ static void conclude_dirstat(struct diff_options *options,
2975 struct dirstat_dir *dir,
2976 unsigned long changed)
2977 {
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 + }
2988
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);
2990 }
2991
2992 static void show_dirstat(struct diff_options *options)
t/t4047-diff-dirstat.sh
+2
@@ -1,6 +1,8 @@
1 #!/bin/sh
2
3 test_description='diff --dirstat tests'
4 +
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 # set up two commits where the second commit has these files