diff mbox series

[v2,2/2] diff-no-index: release prefixed filenames

Message ID 49a40ff2-757a-5d7b-4146-7e8b35884ae8@web.de (mailing list archive)
State New, archived
Headers show
Series None | expand

Commit Message

René Scharfe Sept. 7, 2022, 11:37 a.m. UTC
Callers of prefix_filename() are responsible for freeing its result.
Remember the returned strings and release them to appease leak checkers.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
Changes:
* Use ARRAY_SIZE twice instead of hard-code 2.
* Clarify message (s/Remember them/Remember the returned strings/).

 diff-no-index.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

--
2.37.2
diff mbox series

Patch

diff --git a/diff-no-index.c b/diff-no-index.c
index a3683d8a04..a18f6c3c63 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -245,6 +245,7 @@  int diff_no_index(struct rev_info *revs,
 	int i, no_index;
 	int ret = 1;
 	const char *paths[2];
+	char *to_free[ARRAY_SIZE(paths)] = { 0 };
 	struct strbuf replacement = STRBUF_INIT;
 	const char *prefix = revs->prefix;
 	struct option no_index_options[] = {
@@ -274,7 +275,7 @@  int diff_no_index(struct rev_info *revs,
 			 */
 			p = file_from_standard_input;
 		else if (prefix)
-			p = prefix_filename(prefix, p);
+			p = to_free[i] = prefix_filename(prefix, p);
 		paths[i] = p;
 	}

@@ -308,6 +309,8 @@  int diff_no_index(struct rev_info *revs,
 	ret = diff_result_code(&revs->diffopt, 0);

 out:
+	for (i = 0; i < ARRAY_SIZE(to_free); i++)
+		free(to_free[i]);
 	strbuf_release(&replacement);
 	return ret;
 }