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

[PATCH] unleak paths allocated in "diff --no-index"

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 2, 2022, 21:27 UTC
Message-ID
<xmqqilm579hc.fsf@gitster.g>

"git diff --no-index" codepath starts with the two elements in argv[] and munges them into two paths to be compared, stored in a separate path[] arrays. The munging is implemented in a rather haphazard way, sometimes overwriting old version with a new copy, and sometimes a constant string assigned to path[], making it impossible to release the resources properly:

 * A single dash "-" from the command line is a special signal that
   the standard input is used for the side to be compared, and is
   internally replaced with a copy of string "-" at a known address.
 * When run in a subdirectory, full paths to the two paths are
   allocated and placed in path[].
 * After the above happens, when comparing a file with a directory,
   the directory side is replaced with the path to a file in the
   directory with the same name as the file.

This was perfectly fine for just two strings that are pathnames used during the lifetime of the program and cleaned up upon program exit, but it gets in the way when leak sanitizer is in effect. The third step can be losing the full path that was allocated in the second step, but it is not easy to tell if its input is an allocated piece of memory to begin with.

Loosen the earlier two steps a bit so that elements of the path[] array that come to the directory/file comparison code are either the singleton "-" or an allocated piece of memory. Use that knowledge in the third step to release an allocated piece of memory when it replaces the path to a directory with the path to a file in that directory, and also at the end to release the two elements of the path[] array as needed.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * Because we never pass NULL to the new helper, !path check is not
   strictly necessary, but it is conventional for free()-like
   functions to take NULL and safely become a no-op.
 diff-no-index.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
diff --git c/diff-no-index.c w/diff-no-index.c
index 9a8b09346b..2770b7d15a 100644
--- c/diff-no-index.c
+++ w/diff-no-index.c
@@ -208,6 +208,14 @@ static void append_basename(struct strbuf *path, const char *dir, const char *fi
 	strbuf_addstr(path, tail ? tail + 1 : file);
 }
 
+static void free_allocated_path(const char *path)
+{
+	if (!path ||
+	    (path == file_from_standard_input))
+		return;
+	free((char *)path);
+}
+
 /*
  * DWIM "diff D F" into "diff D/F F" and "diff F D" into "diff F D/F"
  * Note that we append the basename of F to D/, so "diff a/b/file D"
@@ -226,9 +234,11 @@ static void fixup_paths(const char **path, struct strbuf *replacement)
 		return;
 	if (isdir0) {
 		append_basename(replacement, path[0], path[1]);
+		free_allocated_path(path[0]);
 		path[0] = replacement->buf;
 	} else {
 		append_basename(replacement, path[1], path[0]);
+		free_allocated_path(path[1]);
 		path[1] = replacement->buf;
 	}
 }
@@ -274,6 +284,8 @@ int diff_no_index(struct rev_info *revs,
 			p = file_from_standard_input;
 		else if (prefix)
 			p = prefix_filename(prefix, p);
+		else
+			p = xstrdup(p);
 		paths[i] = p;
 	}
 
@@ -294,13 +306,18 @@ int diff_no_index(struct rev_info *revs,
 	setup_diff_pager(&revs->diffopt);
 	revs->diffopt.flags.exit_with_status = 1;
 
-	if (queue_diff(&revs->diffopt, paths[0], paths[1]))
+	if (queue_diff(&revs->diffopt, paths[0], paths[1])) {
+		free_allocated_path(paths[0]);
+		free_allocated_path(paths[1]);
 		return 1;
+	}
 	diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
 	diffcore_std(&revs->diffopt);
 	diff_flush(&revs->diffopt);
 
 	strbuf_release(&replacement);
+	free_allocated_path(paths[0]);
+	free_allocated_path(paths[1]);
 
 	/*
 	 * The return code for --no-index imitates diff(1):
Next: Junio C Hamano
Message 1 of 17 in “unleak paths allocated in "diff --no-index"”
  1. unleak paths allocated in "diff --no-index"Junio C Hamano, Sep 2, 2022
  2. diff --no-index: unleak paths[] elementsJunio C Hamano, Sep 2, 2022
  3. René ScharfeSep 3, 2022
  4. Junio C HamanoSep 5, 2022
  5. 1/2 diff-no-index: release strbuf on queue errorRené Scharfe, Sep 6, 2022
  6. René ScharfeSep 7, 2022
  7. 2/2 diff-no-index: release prefixed filenamesRené Scharfe, Sep 6, 2022
  8. Johannes SchindelinSep 7, 2022
  9. René ScharfeSep 7, 2022
  10. 1/2 diff-no-index: release strbuf on queue errorRené Scharfe, Sep 7, 2022
  11. 2/2 diff-no-index: release prefixed filenamesRené Scharfe, Sep 7, 2022
  12. 3/2 diff-no-index: simplify argv index calculationRené Scharfe, Sep 7, 2022
  13. Junio C HamanoSep 7, 2022
  14. Johannes SchindelinSep 5, 2022
  15. Ævar Arnfjörð BjarmasonSep 5, 2022
  16. Johannes SchindelinSep 7, 2022
  17. Ævar Arnfjörð BjarmasonSep 7, 2022

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.