git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 11/23] diff: fix leaking orderfile option

From
Patrick Steinhardt <ps@pks.im>
Date
Sep 16, 2024, 11:45 UTC
Message-ID
<85c0f9e5f598cc65d1c03f6622900bb950a31159.1726484308.git.ps@pks.im>
In-Reply-To
<cover.1726484308.git.ps@pks.im>

The `orderfile` diff option is being assigned via `OPT_FILENAME()`, which assigns an allocated string to the variable. We never free it though, causing a memory leak.

Change the type of the string to `char *` and free it to plug the leak. This also requires us to use `xstrdup()` to assign the global config to it in case it is set.

This leak is being hit in t7621, but plugging it alone does not make the test suite pass.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 combine-diff.c | 3 +--
 diff.c         | 7 +++++--
 diff.h         | 2 +-
 3 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/combine-diff.c b/combine-diff.c
index 829a44e4167..f6b624dc288 100644
--- a/combine-diff.c
+++ b/combine-diff.c
@@ -1393,9 +1393,8 @@ static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
 {
 	struct combine_diff_path *paths = NULL;
 	int i, num_parent = parents->nr;
-
 	int output_format = opt->output_format;
-	const char *orderfile = opt->orderfile;
+	char *orderfile = opt->orderfile;
 
 	opt->output_format = DIFF_FORMAT_NO_OUTPUT;
 	/* tell diff_tree to emit paths in sorted (=tree) order */
diff --git a/diff.c b/diff.c
index 472479eb101..6555b8a32c1 100644
--- a/diff.c
+++ b/diff.c
@@ -441,8 +441,10 @@ int git_diff_ui_config(const char *var, const char *value,
 	}
 	if (!strcmp(var, "diff.wordregex"))
 		return git_config_string(&diff_word_regex_cfg, var, value);
-	if (!strcmp(var, "diff.orderfile"))
+	if (!strcmp(var, "diff.orderfile")) {
+		FREE_AND_NULL(diff_order_file_cfg);
 		return git_config_pathname(&diff_order_file_cfg, var, value);
+	}
 
 	if (!strcmp(var, "diff.ignoresubmodules")) {
 		if (!value)
@@ -4775,7 +4777,7 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)
 	if (diff_indent_heuristic)
 		DIFF_XDL_SET(options, INDENT_HEURISTIC);
 
-	options->orderfile = diff_order_file_cfg;
+	options->orderfile = xstrdup_or_null(diff_order_file_cfg);
 
 	if (!options->flags.ignore_submodule_set)
 		options->flags.ignore_untracked_in_submodules = 1;
@@ -6727,6 +6729,7 @@ void diff_free(struct diff_options *options)
 		FREE_AND_NULL(options->objfind);
 	}
 
+	FREE_AND_NULL(options->orderfile);
 	for (size_t i = 0; i < options->anchors_nr; i++)
 		free(options->anchors[i]);
 	FREE_AND_NULL(options->anchors);
diff --git a/diff.h b/diff.h
index 9901c8ca8c8..b95d3c1e830 100644
--- a/diff.h
+++ b/diff.h
@@ -235,7 +235,7 @@ enum diff_submodule_format {
  * diffcore library with.
  */
 struct diff_options {
-	const char *orderfile;
+	char *orderfile;
 
 	/*
 	 * "--rotate-to=<file>" would start showing at <file> and when
-- 
2.46.0.551.gc5ee8f2d1c.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 20 of 62 in “Memory leak fixes (pt.7)”
  1. 00/23 Memory leak fixes (pt.7)Patrick Steinhardt, Sep 16, 2024
  2. 01/23 builtin/help: fix dangling reference to `html_path`Patrick Steinhardt, Sep 16, 2024
  3. Justin ToblerSep 16, 2024
  4. 02/23 builtin/help: fix leaking `html_path` when reading config multiple timesPatrick Steinhardt, Sep 16, 2024
  5. 03/23 git: fix leaking argv when handling builtinsPatrick Steinhardt, Sep 16, 2024
  6. 04/23 submodule: fix leaking update strategyPatrick Steinhardt, Sep 16, 2024
  7. 05/23 builtin/submodule--helper: clear child process when not running itPatrick Steinhardt, Sep 16, 2024
  8. 06/23 builtin/submodule--helper: fix leaking error bufferPatrick Steinhardt, Sep 16, 2024
  9. 07/23 t/helper: fix leaking subrepo in nested submodule config helperPatrick Steinhardt, Sep 16, 2024
  10. 08/23 builtin/submodule--helper: fix leaking remote ref on errorsPatrick Steinhardt, Sep 16, 2024
  11. Justin ToblerSep 16, 2024
  12. Patrick SteinhardtSep 17, 2024
  13. Junio C HamanoSep 25, 2024
  14. Patrick SteinhardtSep 26, 2024
  15. 09/23 dir: fix off by one errors for ignored and untracked entriesPatrick Steinhardt, Sep 16, 2024
  16. Junio C HamanoSep 20, 2024
  17. 10/23 builtin/pull: fix leaking "ff" optionPatrick Steinhardt, Sep 16, 2024
  18. Junio C HamanoSep 20, 2024
  19. Patrick SteinhardtSep 24, 2024
  20. 11/23 diff: fix leaking orderfile optionPatrick Steinhardt, Sep 16, 2024
  21. 12/23 parse-options: free previous value of `OPTION_FILENAME`Patrick Steinhardt, Sep 16, 2024
  22. Junio C HamanoSep 20, 2024
  23. 13/23 diffcore-order: fix leaking buffer when parsing orderfilesPatrick Steinhardt, Sep 16, 2024
  24. 14/23 builtin/repack: fix leaking configurationPatrick Steinhardt, Sep 16, 2024
  25. Junio C HamanoSep 20, 2024
  26. 15/23 builtin/difftool: plug several trivial memory leaksPatrick Steinhardt, Sep 16, 2024
  27. 16/23 trace2: destroy context stored in thread-local storagePatrick Steinhardt, Sep 16, 2024
  28. 17/23 submodule: fix leaking submodule ODB pathsPatrick Steinhardt, Sep 16, 2024
  29. 18/23 grep: fix leaking grep patternPatrick Steinhardt, Sep 16, 2024
  30. 19/23 promisor-remote: fix leaking partial clone filterPatrick Steinhardt, Sep 16, 2024
  31. 20/23 builtin/maintenance: fix leaking config stringPatrick Steinhardt, Sep 16, 2024
  32. Junio C HamanoSep 20, 2024
  33. 21/23 builtin/maintenance: fix leak in `get_schedule_cmd()`Patrick Steinhardt, Sep 16, 2024
  34. 22/23 revision: fix leaking parents when simplifying commitsPatrick Steinhardt, Sep 16, 2024
  35. Junio C HamanoSep 19, 2024
  36. 23/23 diffcore-break: fix leaking filespecs when merging broken pairsPatrick Steinhardt, Sep 16, 2024
  37. Junio C HamanoSep 19, 2024
  38. Patrick SteinhardtSep 24, 2024
  39. 00/23 Memory leak fixes (pt.7)Patrick Steinhardt, Sep 26, 2024
  40. 01/23 builtin/help: fix dangling reference to `html_path`Patrick Steinhardt, Sep 26, 2024
  41. 02/23 builtin/help: fix leaking `html_path` when reading config multiple timesPatrick Steinhardt, Sep 26, 2024
  42. 03/23 git: fix leaking argv when handling builtinsPatrick Steinhardt, Sep 26, 2024
  43. 04/23 submodule: fix leaking update strategyPatrick Steinhardt, Sep 26, 2024
  44. 05/23 builtin/submodule--helper: clear child process when not running itPatrick Steinhardt, Sep 26, 2024
  45. 06/23 builtin/submodule--helper: fix leaking error bufferPatrick Steinhardt, Sep 26, 2024
  46. 07/23 t/helper: fix leaking subrepo in nested submodule config helperPatrick Steinhardt, Sep 26, 2024
  47. 08/23 builtin/submodule--helper: fix leaking remote ref on errorsPatrick Steinhardt, Sep 26, 2024
  48. 09/23 dir: fix off by one errors for ignored and untracked entriesPatrick Steinhardt, Sep 26, 2024
  49. 10/23 builtin/pull: fix leaking "ff" optionPatrick Steinhardt, Sep 26, 2024
  50. 11/23 diff: fix leaking orderfile optionPatrick Steinhardt, Sep 26, 2024
  51. 12/23 parse-options: free previous value of `OPTION_FILENAME`Patrick Steinhardt, Sep 26, 2024
  52. 13/23 diffcore-order: fix leaking buffer when parsing orderfilesPatrick Steinhardt, Sep 26, 2024
  53. 14/23 builtin/repack: fix leaking configurationPatrick Steinhardt, Sep 26, 2024
  54. 15/23 builtin/difftool: plug several trivial memory leaksPatrick Steinhardt, Sep 26, 2024
  55. 16/23 trace2: destroy context stored in thread-local storagePatrick Steinhardt, Sep 26, 2024
  56. 17/23 submodule: fix leaking submodule ODB pathsPatrick Steinhardt, Sep 26, 2024
  57. 18/23 grep: fix leaking grep patternPatrick Steinhardt, Sep 26, 2024
  58. 19/23 promisor-remote: fix leaking partial clone filterPatrick Steinhardt, Sep 26, 2024
  59. 20/23 builtin/maintenance: fix leaking config stringPatrick Steinhardt, Sep 26, 2024
  60. 21/23 builtin/maintenance: fix leak in `get_schedule_cmd()`Patrick Steinhardt, Sep 26, 2024
  61. 22/23 revision: fix leaking parents when simplifying commitsPatrick Steinhardt, Sep 26, 2024
  62. 23/23 diffcore-break: fix leaking filespecs when merging broken pairsPatrick Steinhardt, Sep 26, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.