git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: James Liu <james@jamesliu.io>,
	karthik nayak <karthik.188@gmail.com>,
	Phillip Wood <phillip.wood123@gmail.com>,
	Junio C Hamano <gitster@pobox.com>, Taylor Blau <me@ttaylorr.com>
Subject: [PATCH v3 19/22] userdiff: fix leaking memory for configured diff drivers
Date: Tue, 13 Aug 2024 11:32:05 +0200	[thread overview]
Message-ID: <39b2921e3e6aeb7bc5d6d08ed9861d26b738a42c.1723540931.git.ps@pks.im> (raw)
In-Reply-To: <cover.1723540931.git.ps@pks.im>

The userdiff structures may be initialized either statically on the
stack or dynamically via configuration keys. In the latter case we end
up leaking memory because we didn't have any infrastructure to discern
those strings which have been allocated statically and those which have
been allocated dynamically.

Refactor the code such that we have two pointers for each of these
strings: one that holds the value as accessed by other subsystems, and
one that points to the same string in case it has been allocated. Like
this, we can safely free the second pointer and thus plug those memory
leaks.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 range-diff.c                     |  6 +++--
 t/t4018-diff-funcname.sh         |  1 +
 t/t4042-diff-textconv-caching.sh |  2 ++
 t/t4048-diff-combined-binary.sh  |  1 +
 t/t4209-log-pickaxe.sh           |  2 ++
 userdiff.c                       | 38 ++++++++++++++++++++++++--------
 userdiff.h                       |  4 ++++
 7 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/range-diff.c b/range-diff.c
index 5f01605550..bbb0952264 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -450,8 +450,10 @@ static void output_pair_header(struct diff_options *diffopt,
 }
 
 static struct userdiff_driver section_headers = {
-	.funcname = { "^ ## (.*) ##$\n"
-		      "^.?@@ (.*)$", REG_EXTENDED }
+	.funcname = {
+		.pattern = "^ ## (.*) ##$\n^.?@@ (.*)$",
+		.cflags = REG_EXTENDED,
+	},
 };
 
 static struct diff_filespec *get_filespec(const char *name, const char *p)
diff --git a/t/t4018-diff-funcname.sh b/t/t4018-diff-funcname.sh
index e026fac1f4..8128c30e7f 100755
--- a/t/t4018-diff-funcname.sh
+++ b/t/t4018-diff-funcname.sh
@@ -5,6 +5,7 @@
 
 test_description='Test custom diff function name patterns'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
diff --git a/t/t4042-diff-textconv-caching.sh b/t/t4042-diff-textconv-caching.sh
index 8ebfa3c1be..a179205394 100755
--- a/t/t4042-diff-textconv-caching.sh
+++ b/t/t4042-diff-textconv-caching.sh
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='test textconv caching'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 cat >helper <<'EOF'
diff --git a/t/t4048-diff-combined-binary.sh b/t/t4048-diff-combined-binary.sh
index 0260cf64f5..f399484bce 100755
--- a/t/t4048-diff-combined-binary.sh
+++ b/t/t4048-diff-combined-binary.sh
@@ -4,6 +4,7 @@ test_description='combined and merge diff handle binary files and textconv'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup binary merge conflict' '
diff --git a/t/t4209-log-pickaxe.sh b/t/t4209-log-pickaxe.sh
index 64e1623733..b42fdc54fc 100755
--- a/t/t4209-log-pickaxe.sh
+++ b/t/t4209-log-pickaxe.sh
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='log --grep/--author/--regexp-ignore-case/-S/-G'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_log () {
diff --git a/userdiff.c b/userdiff.c
index c4ebb9ff73..989629149f 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -399,8 +399,11 @@ static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t
 static int parse_funcname(struct userdiff_funcname *f, const char *k,
 		const char *v, int cflags)
 {
-	if (git_config_string((char **) &f->pattern, k, v) < 0)
+	f->pattern = NULL;
+	FREE_AND_NULL(f->pattern_owned);
+	if (git_config_string(&f->pattern_owned, k, v) < 0)
 		return -1;
+	f->pattern = f->pattern_owned;
 	f->cflags = cflags;
 	return 0;
 }
@@ -444,20 +447,37 @@ int userdiff_config(const char *k, const char *v)
 		return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
 	if (!strcmp(type, "binary"))
 		return parse_tristate(&drv->binary, k, v);
-	if (!strcmp(type, "command"))
-		return git_config_string((char **) &drv->external.cmd, k, v);
+	if (!strcmp(type, "command")) {
+		FREE_AND_NULL(drv->external.cmd);
+		return git_config_string(&drv->external.cmd, k, v);
+	}
 	if (!strcmp(type, "trustexitcode")) {
 		drv->external.trust_exit_code = git_config_bool(k, v);
 		return 0;
 	}
-	if (!strcmp(type, "textconv"))
-		return git_config_string((char **) &drv->textconv, k, v);
+	if (!strcmp(type, "textconv")) {
+		int ret;
+		FREE_AND_NULL(drv->textconv_owned);
+		ret = git_config_string(&drv->textconv_owned, k, v);
+		drv->textconv = drv->textconv_owned;
+		return ret;
+	}
 	if (!strcmp(type, "cachetextconv"))
 		return parse_bool(&drv->textconv_want_cache, k, v);
-	if (!strcmp(type, "wordregex"))
-		return git_config_string((char **) &drv->word_regex, k, v);
-	if (!strcmp(type, "algorithm"))
-		return git_config_string((char **) &drv->algorithm, k, v);
+	if (!strcmp(type, "wordregex")) {
+		int ret;
+		FREE_AND_NULL(drv->word_regex_owned);
+		ret = git_config_string(&drv->word_regex_owned, k, v);
+		drv->word_regex = drv->word_regex_owned;
+		return ret;
+	}
+	if (!strcmp(type, "algorithm")) {
+		int ret;
+		FREE_AND_NULL(drv->algorithm_owned);
+		ret = git_config_string(&drv->algorithm_owned, k, v);
+		drv->algorithm = drv->algorithm_owned;
+		return ret;
+	}
 
 	return 0;
 }
diff --git a/userdiff.h b/userdiff.h
index 7565930337..827361b0bc 100644
--- a/userdiff.h
+++ b/userdiff.h
@@ -8,6 +8,7 @@ struct repository;
 
 struct userdiff_funcname {
 	const char *pattern;
+	char *pattern_owned;
 	int cflags;
 };
 
@@ -20,11 +21,14 @@ struct userdiff_driver {
 	const char *name;
 	struct external_diff external;
 	const char *algorithm;
+	char *algorithm_owned;
 	int binary;
 	struct userdiff_funcname funcname;
 	const char *word_regex;
+	char *word_regex_owned;
 	const char *word_regex_multi_byte;
 	const char *textconv;
+	char *textconv_owned;
 	struct notes_cache *textconv_cache;
 	int textconv_want_cache;
 };
-- 
2.46.0.46.g406f326d27.dirty


  parent reply	other threads:[~2024-08-13  9:32 UTC|newest]

Thread overview: 146+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-06  8:59 [PATCH 00/22] Memory leak fixes (pt.4) Patrick Steinhardt
2024-08-06  8:59 ` [PATCH 01/22] remote: plug memory leak when aliasing URLs Patrick Steinhardt
2024-08-06  8:59 ` [PATCH 02/22] git: fix leaking system paths Patrick Steinhardt
2024-08-07  4:02   ` James Liu
2024-08-06  8:59 ` [PATCH 03/22] object-file: fix memory leak when reading corrupted headers Patrick Steinhardt
2024-08-06  8:59 ` [PATCH 04/22] object-name: fix leaking symlink paths in object context Patrick Steinhardt
2024-08-06  8:59 ` [PATCH 05/22] bulk-checkin: fix leaking state TODO Patrick Steinhardt
2024-08-06  9:00 ` [PATCH 06/22] read-cache: fix leaking hashfile when writing index fails Patrick Steinhardt
2024-08-07  7:01   ` James Liu
2024-08-08  5:04     ` Patrick Steinhardt
2024-08-06  9:00 ` [PATCH 07/22] submodule-config: fix leaking name enrty when traversing submodules Patrick Steinhardt
2024-08-06  9:00 ` [PATCH 08/22] config: fix leaking comment character config Patrick Steinhardt
2024-08-07  7:11   ` James Liu
2024-08-08  5:04     ` Patrick Steinhardt
2024-08-08 15:54       ` Junio C Hamano
2024-08-06  9:00 ` [PATCH 09/22] builtin/rebase: fix leaking `commit.gpgsign` value Patrick Steinhardt
2024-08-07  7:32   ` James Liu
2024-08-08  5:05     ` Patrick Steinhardt
2024-08-08 10:07   ` Phillip Wood
2024-08-08 12:58     ` Patrick Steinhardt
2024-08-06  9:00 ` [PATCH 10/22] builtin/notes: fix leaking `struct notes_tree` when merging notes Patrick Steinhardt
2024-08-06  9:00 ` [PATCH 11/22] builtin/fast-import: plug trivial memory leaks Patrick Steinhardt
2024-08-06  9:00 ` [PATCH 12/22] builtin/fast-export: fix leaking diff options Patrick Steinhardt
2024-08-06  9:00 ` [PATCH 13/22] builtin/fast-export: plug leaking tag names Patrick Steinhardt
2024-08-07  8:31   ` James Liu
2024-08-08  5:05     ` Patrick Steinhardt
2024-08-06  9:00 ` [PATCH 14/22] merge-ort: unconditionally release attributes index Patrick Steinhardt
2024-08-06  9:00 ` [PATCH 15/22] sequencer: release todo list on error paths Patrick Steinhardt
2024-08-08 10:08   ` Phillip Wood
2024-08-08 16:31     ` Junio C Hamano
2024-08-06  9:00 ` [PATCH 16/22] unpack-trees: clear index when not propagating it Patrick Steinhardt
2024-08-06  9:00 ` [PATCH 17/22] diff: fix leak when parsing invalid ignore regex option Patrick Steinhardt
2024-08-06  9:00 ` [PATCH 18/22] builtin/format-patch: fix various trivial memory leaks Patrick Steinhardt
2024-08-07  8:51   ` James Liu
2024-08-08  5:05     ` Patrick Steinhardt
2024-08-06  9:01 ` [PATCH 19/22] userdiff: fix leaking memory for configured diff drivers Patrick Steinhardt
2024-08-07  9:25   ` James Liu
2024-08-08  5:05     ` Patrick Steinhardt
2024-08-08 16:05       ` Junio C Hamano
2024-08-06  9:01 ` [PATCH 20/22] builtin/log: fix leak when showing converted blob contents Patrick Steinhardt
2024-08-06  9:01 ` [PATCH 21/22] diff: free state populated via options Patrick Steinhardt
2024-08-06  9:01 ` [PATCH 22/22] builtin/diff: free symmetric diff members Patrick Steinhardt
2024-08-07  9:27 ` [PATCH 00/22] Memory leak fixes (pt.4) James Liu
2024-08-08  5:05   ` Patrick Steinhardt
2024-08-08  6:00     ` James Liu
2024-08-07 16:59 ` Junio C Hamano
2024-08-07 17:03   ` Patrick Steinhardt
2024-08-08  0:32     ` Junio C Hamano
2024-08-08 13:04 ` [PATCH v2 " Patrick Steinhardt
2024-08-08 13:04   ` [PATCH v2 01/22] remote: plug memory leak when aliasing URLs Patrick Steinhardt
2024-08-12  8:27     ` karthik nayak
2024-08-12 14:08     ` Taylor Blau
2024-08-12 14:37     ` Jeff King
2024-08-13  6:34       ` Patrick Steinhardt
2024-08-08 13:04   ` [PATCH v2 02/22] git: fix leaking system paths Patrick Steinhardt
2024-08-12 14:11     ` Taylor Blau
2024-08-13  6:30       ` Patrick Steinhardt
2024-08-13 16:02         ` Junio C Hamano
2024-08-08 13:04   ` [PATCH v2 03/22] object-file: fix memory leak when reading corrupted headers Patrick Steinhardt
2024-08-12  8:43     ` karthik nayak
2024-08-08 13:04   ` [PATCH v2 04/22] object-name: fix leaking symlink paths in object context Patrick Steinhardt
2024-08-08 13:04   ` [PATCH v2 05/22] bulk-checkin: fix leaking state TODO Patrick Steinhardt
2024-08-08 13:04   ` [PATCH v2 06/22] read-cache: fix leaking hashfile when writing index fails Patrick Steinhardt
2024-08-08 13:05   ` [PATCH v2 07/22] submodule-config: fix leaking name enrty when traversing submodules Patrick Steinhardt
2024-08-08 13:05   ` [PATCH v2 08/22] config: fix leaking comment character config Patrick Steinhardt
2024-08-08 17:12     ` Junio C Hamano
2024-08-12  7:45       ` Patrick Steinhardt
2024-08-12 20:32         ` Junio C Hamano
2024-08-13  6:54           ` Patrick Steinhardt
2024-08-08 13:05   ` [PATCH v2 09/22] builtin/rebase: fix leaking `commit.gpgsign` value Patrick Steinhardt
2024-08-08 13:05   ` [PATCH v2 10/22] builtin/notes: fix leaking `struct notes_tree` when merging notes Patrick Steinhardt
2024-08-08 13:05   ` [PATCH v2 11/22] builtin/fast-import: plug trivial memory leaks Patrick Steinhardt
2024-08-08 13:05   ` [PATCH v2 12/22] builtin/fast-export: fix leaking diff options Patrick Steinhardt
2024-08-12  9:05     ` karthik nayak
2024-08-08 13:05   ` [PATCH v2 13/22] builtin/fast-export: plug leaking tag names Patrick Steinhardt
2024-08-08 13:05   ` [PATCH v2 14/22] merge-ort: unconditionally release attributes index Patrick Steinhardt
2024-08-08 13:05   ` [PATCH v2 15/22] sequencer: release todo list on error paths Patrick Steinhardt
2024-08-08 13:05   ` [PATCH v2 16/22] unpack-trees: clear index when not propagating it Patrick Steinhardt
2024-08-08 13:05   ` [PATCH v2 17/22] diff: fix leak when parsing invalid ignore regex option Patrick Steinhardt
2024-08-08 13:05   ` [PATCH v2 18/22] builtin/format-patch: fix various trivial memory leaks Patrick Steinhardt
2024-08-08 13:05   ` [PATCH v2 19/22] userdiff: fix leaking memory for configured diff drivers Patrick Steinhardt
2024-08-08 13:06   ` [PATCH v2 20/22] builtin/log: fix leak when showing converted blob contents Patrick Steinhardt
2024-08-08 13:06   ` [PATCH v2 21/22] diff: free state populated via options Patrick Steinhardt
2024-08-08 13:06   ` [PATCH v2 22/22] builtin/diff: free symmetric diff members Patrick Steinhardt
2024-08-12  9:12     ` karthik nayak
2024-08-12  9:13   ` [PATCH v2 00/22] Memory leak fixes (pt.4) karthik nayak
2024-08-12 15:49     ` Junio C Hamano
2024-08-13  6:27       ` Patrick Steinhardt
2024-08-12 14:01   ` Phillip Wood
2024-08-12 15:50     ` Junio C Hamano
2024-08-13  9:31 ` [PATCH v3 " Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 01/22] remote: plug memory leak when aliasing URLs Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 02/22] git: fix leaking system paths Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 03/22] object-file: fix memory leak when reading corrupted headers Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 04/22] object-name: fix leaking symlink paths in object context Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 05/22] bulk-checkin: fix leaking state TODO Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 06/22] read-cache: fix leaking hashfile when writing index fails Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 07/22] submodule-config: fix leaking name entry when traversing submodules Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 08/22] config: fix leaking comment character config Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 09/22] builtin/rebase: fix leaking `commit.gpgsign` value Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 10/22] builtin/notes: fix leaking `struct notes_tree` when merging notes Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 11/22] builtin/fast-import: plug trivial memory leaks Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 12/22] builtin/fast-export: fix leaking diff options Patrick Steinhardt
2024-08-13 16:34     ` Junio C Hamano
2024-08-14  4:49       ` Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 13/22] builtin/fast-export: plug leaking tag names Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 14/22] merge-ort: unconditionally release attributes index Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 15/22] sequencer: release todo list on error paths Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 16/22] unpack-trees: clear index when not propagating it Patrick Steinhardt
2024-08-13  9:31   ` [PATCH v3 17/22] diff: fix leak when parsing invalid ignore regex option Patrick Steinhardt
2024-08-13  9:32   ` [PATCH v3 18/22] builtin/format-patch: fix various trivial memory leaks Patrick Steinhardt
2024-08-13 16:55     ` Junio C Hamano
2024-08-14  4:56       ` Patrick Steinhardt
2024-08-13 16:55     ` Junio C Hamano
2024-08-13  9:32   ` Patrick Steinhardt [this message]
2024-08-13  9:32   ` [PATCH v3 20/22] builtin/log: fix leak when showing converted blob contents Patrick Steinhardt
2024-08-13  9:32   ` [PATCH v3 21/22] diff: free state populated via options Patrick Steinhardt
2024-08-13 16:31     ` Junio C Hamano
2024-08-13  9:32   ` [PATCH v3 22/22] builtin/diff: free symmetric diff members Patrick Steinhardt
2024-08-13 16:25     ` Junio C Hamano
2024-08-14  5:01       ` Patrick Steinhardt
2024-08-14 15:28         ` Junio C Hamano
2024-08-13 16:58   ` [PATCH v3 00/22] Memory leak fixes (pt.4) Junio C Hamano
2024-08-14  6:51 ` [PATCH v4 " Patrick Steinhardt
2024-08-14  6:51   ` [PATCH v4 01/22] remote: plug memory leak when aliasing URLs Patrick Steinhardt
2024-08-14  6:51   ` [PATCH v4 02/22] git: fix leaking system paths Patrick Steinhardt
2024-08-14  6:51   ` [PATCH v4 03/22] object-file: fix memory leak when reading corrupted headers Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 04/22] object-name: fix leaking symlink paths in object context Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 05/22] bulk-checkin: fix leaking state TODO Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 06/22] read-cache: fix leaking hashfile when writing index fails Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 07/22] submodule-config: fix leaking name entry when traversing submodules Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 08/22] config: fix leaking comment character config Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 09/22] builtin/rebase: fix leaking `commit.gpgsign` value Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 10/22] builtin/notes: fix leaking `struct notes_tree` when merging notes Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 11/22] builtin/fast-import: plug trivial memory leaks Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 12/22] builtin/fast-export: fix leaking diff options Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 13/22] builtin/fast-export: plug leaking tag names Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 14/22] merge-ort: unconditionally release attributes index Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 15/22] sequencer: release todo list on error paths Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 16/22] unpack-trees: clear index when not propagating it Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 17/22] diff: fix leak when parsing invalid ignore regex option Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 18/22] builtin/format-patch: fix various trivial memory leaks Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 19/22] userdiff: fix leaking memory for configured diff drivers Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 20/22] builtin/log: fix leak when showing converted blob contents Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 21/22] diff: free state populated via options Patrick Steinhardt
2024-08-14  6:52   ` [PATCH v4 22/22] builtin/diff: free symmetric diff members Patrick Steinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=39b2921e3e6aeb7bc5d6d08ed9861d26b738a42c.1723540931.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=james@jamesliu.io \
    --cc=karthik.188@gmail.com \
    --cc=me@ttaylorr.com \
    --cc=phillip.wood123@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).