From fffe7d81a402ced9ede961f55cfc617fe08e71ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Wed, 7 Sep 2022 13:36:53 +0200 Subject: [PATCH 1/3] diff-no-index: release strbuf on queue error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The strbuf is small and we are about to exit, so we could leave its cleanup to the OS. If we release it explicitly at all, however, then we should do it on early exit as well. Move the strbuf_release call to a new cleanup section at the end and make sure all execution paths go through it. Suggested-by: Johannes Schindelin Signed-off-by: René Scharfe Signed-off-by: Junio C Hamano --- diff-no-index.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/diff-no-index.c b/diff-no-index.c index 9a8b09346b..a3683d8a04 100644 --- a/diff-no-index.c +++ b/diff-no-index.c @@ -243,6 +243,7 @@ int diff_no_index(struct rev_info *revs, int argc, const char **argv) { int i, no_index; + int ret = 1; const char *paths[2]; struct strbuf replacement = STRBUF_INIT; const char *prefix = revs->prefix; @@ -295,16 +296,18 @@ int diff_no_index(struct rev_info *revs, revs->diffopt.flags.exit_with_status = 1; if (queue_diff(&revs->diffopt, paths[0], paths[1])) - return 1; + goto out; diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/"); diffcore_std(&revs->diffopt); diff_flush(&revs->diffopt); - strbuf_release(&replacement); - /* * The return code for --no-index imitates diff(1): * 0 = no changes, 1 = changes, else error */ - return diff_result_code(&revs->diffopt, 0); + ret = diff_result_code(&revs->diffopt, 0); + +out: + strbuf_release(&replacement); + return ret; } From 07a6f94a6d06947d325881460a3798dda0a98cf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Wed, 7 Sep 2022 13:37:02 +0200 Subject: [PATCH 2/3] diff-no-index: release prefixed filenames MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano --- diff-no-index.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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; } From 2b43dd0eb5f3f035b9bd9d019ec405dd55ec15e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Wed, 7 Sep 2022 13:45:42 +0200 Subject: [PATCH 3/3] diff-no-index: simplify argv index calculation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since 16bb3d714d (diff --no-index: use parse_options() instead of diff_opt_parse(), 2019-03-24) argc must be 2 if we reach the loop, i.e. argc - 2 == 0. Remove that inconsequential term. Signed-off-by: René Scharfe Signed-off-by: Junio C Hamano --- diff-no-index.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff-no-index.c b/diff-no-index.c index a18f6c3c63..18edbdf4b5 100644 --- a/diff-no-index.c +++ b/diff-no-index.c @@ -267,7 +267,7 @@ int diff_no_index(struct rev_info *revs, } FREE_AND_NULL(options); for (i = 0; i < 2; i++) { - const char *p = argv[argc - 2 + i]; + const char *p = argv[i]; if (!strcmp(p, "-")) /* * stdin should be spelled as "-"; if you have