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
maint
Junio C Hamano 2023-05-15 13:59:04 -07:00
commit 1e1dcb2a42
2 changed files with 22 additions and 14 deletions

34
diff.c
View File

@ -3007,6 +3007,24 @@ static int dirstat_compare(const void *_a, const void *_b)
return strcmp(a->name, b->name);
}

static void conclude_dirstat(struct diff_options *options,
struct dirstat_dir *dir,
unsigned long changed)
{
struct dirstat_file *to_free = dir->files;

if (!changed) {
/* This can happen even with many files, if everything was renames */
;
} else {
/* Show all directories with more than x% of the changes */
QSORT(dir->files, dir->nr, dirstat_compare);
gather_dirstat(options, dir, changed, "", 0);
}

free(to_free);
}

static void show_dirstat(struct diff_options *options)
{
int i;
@ -3096,13 +3114,7 @@ found_damage:
dir.nr++;
}

/* This can happen even with many files, if everything was renames */
if (!changed)
return;

/* Show all directories with more than x% of the changes */
QSORT(dir.files, dir.nr, dirstat_compare);
gather_dirstat(options, &dir, changed, "", 0);
conclude_dirstat(options, &dir, changed);
}

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
dir.nr++;
}

/* This can happen even with many files, if everything was renames */
if (!changed)
return;

/* Show all directories with more than x% of the changes */
QSORT(dir.files, dir.nr, dirstat_compare);
gather_dirstat(options, &dir, changed, "", 0);
conclude_dirstat(options, &dir, changed);
}

static void free_diffstat_file(struct diffstat_file *f)

View File

@ -1,6 +1,8 @@
#!/bin/sh

test_description='diff --dirstat tests'

TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh

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