Skip to content

Commit 1e1dcb2

Browse files
committed
Merge branch 'jc/dirstat-plug-leaks'
"git diff --dirstat" leaked memory, which has been plugged. * jc/dirstat-plug-leaks: diff: plug leaks in dirstat diff: refactor common tail part of dirstat computation
2 parents cd2b740 + 8397398 commit 1e1dcb2

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

diff.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,6 +3007,24 @@ static int dirstat_compare(const void *_a, const void *_b)
30073007
return strcmp(a->name, b->name);
30083008
}
30093009

3010+
static void conclude_dirstat(struct diff_options *options,
3011+
struct dirstat_dir *dir,
3012+
unsigned long changed)
3013+
{
3014+
struct dirstat_file *to_free = dir->files;
3015+
3016+
if (!changed) {
3017+
/* This can happen even with many files, if everything was renames */
3018+
;
3019+
} else {
3020+
/* Show all directories with more than x% of the changes */
3021+
QSORT(dir->files, dir->nr, dirstat_compare);
3022+
gather_dirstat(options, dir, changed, "", 0);
3023+
}
3024+
3025+
free(to_free);
3026+
}
3027+
30103028
static void show_dirstat(struct diff_options *options)
30113029
{
30123030
int i;
@@ -3096,13 +3114,7 @@ static void show_dirstat(struct diff_options *options)
30963114
dir.nr++;
30973115
}
30983116

3099-
/* This can happen even with many files, if everything was renames */
3100-
if (!changed)
3101-
return;
3102-
3103-
/* Show all directories with more than x% of the changes */
3104-
QSORT(dir.files, dir.nr, dirstat_compare);
3105-
gather_dirstat(options, &dir, changed, "", 0);
3117+
conclude_dirstat(options, &dir, changed);
31063118
}
31073119

31083120
static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
@@ -3140,13 +3152,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
31403152
dir.nr++;
31413153
}
31423154

3143-
/* This can happen even with many files, if everything was renames */
3144-
if (!changed)
3145-
return;
3146-
3147-
/* Show all directories with more than x% of the changes */
3148-
QSORT(dir.files, dir.nr, dirstat_compare);
3149-
gather_dirstat(options, &dir, changed, "", 0);
3155+
conclude_dirstat(options, &dir, changed);
31503156
}
31513157

31523158
static void free_diffstat_file(struct diffstat_file *f)

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)