diff mbox series

[v2,2/2] diff: plug memory leak from regcomp() on {log,diff} -I

Message ID 20210211104535.16076-3-avarab@gmail.com (mailing list archive)
State Accepted
Commit c45dc9cf30a6f7f40adb3ea70dd2f2905ecd4afa
Headers show
Series [v2,1/2] diff: add an API for deferred freeing | expand

Commit Message

Ævar Arnfjörð Bjarmason Feb. 11, 2021, 10:45 a.m. UTC
Fix a memory leak in 296d4a94e7 (diff: add -I<regex> that ignores
matching changes, 2020-10-20) by freeing the memory it allocates in
the newly introduced diff_free(). See the previous commit for details
on that.

This memory leak was intentionally introduced in 296d4a94e7, see the
discussion on a previous iteration of it in
https://lore.kernel.org/git/xmqqeelycajx.fsf@gitster.c.googlers.com/

At that time freeing the memory was somewhat tedious, but since it
isn't anymore with the newly introduced diff_free() let's use it.

Let's retain the pattern for diff_free_file() and add a
diff_free_ignore_regex(), even though (unlike "diff_free_file") we
don't need to call it elsewhere. I think this'll make for more
readable code than gradually accumulating a giant diff_free()
function, sharing "int i" across unrelated code etc.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 diff.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
diff mbox series

Patch

diff --git a/diff.c b/diff.c
index a63c9ecae79..bf2cbf15e77 100644
--- a/diff.c
+++ b/diff.c
@@ -6342,12 +6342,24 @@  static void diff_free_file(struct diff_options *options)
 		fclose(options->file);
 }
 
+static void diff_free_ignore_regex(struct diff_options *options)
+{
+	int i;
+
+	for (i = 0; i < options->ignore_regex_nr; i++) {
+		regfree(options->ignore_regex[i]);
+		free(options->ignore_regex[i]);
+	}
+	free(options->ignore_regex);
+}
+
 void diff_free(struct diff_options *options)
 {
 	if (options->no_free)
 		return;
 
 	diff_free_file(options);
+	diff_free_ignore_regex(options);
 }
 
 void diff_flush(struct diff_options *options)