diff-no-index: release strbuf on queue error

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 <johannes.schindelin@gmx.de> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Sep 7, 2022 at 13:36 UTC fffe7d81a402ced9ede961f55cfc617fe08e71ef
1 file changed +7 -4
diff-no-index.c
+7 -4
@@ -243,6 +243,7 @@ int diff_no_index(struct rev_info *revs,
243 int argc, const char **argv)
244 {
245 int i, no_index;
246 + int ret = 1;
247 const char *paths[2];
248 struct strbuf replacement = STRBUF_INIT;
249 const char *prefix = revs->prefix;
@@ -295,16 +296,18 @@ int diff_no_index(struct rev_info *revs,
296 revs->diffopt.flags.exit_with_status = 1;
297
298 if (queue_diff(&revs->diffopt, paths[0], paths[1]))
298 - return 1;
299 + goto out;
300 diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
301 diffcore_std(&revs->diffopt);
302 diff_flush(&revs->diffopt);
303
303 - strbuf_release(&replacement);
304 -
304 /*
305 * The return code for --no-index imitates diff(1):
306 * 0 = no changes, 1 = changes, else error
307 */
309 - return diff_result_code(&revs->diffopt, 0);
308 + ret = diff_result_code(&revs->diffopt, 0);
309 +
310 +out:
311 + strbuf_release(&replacement);
312 + return ret;
313 }