Skip to content

Commit 5f3296c

Browse files
dschogitster
authored andcommitted
difftool: address a couple of resource/memory leaks
This change plugs a couple of memory leaks and makes sure that the file descriptor is closed in run_dir_diff(). Spotted by Coverity. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5b34ba4 commit 5f3296c

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

builtin/difftool.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ static void changed_files(struct hashmap *result, const char *index_path,
226226
hashmap_entry_init(entry, strhash(buf.buf));
227227
hashmap_add(result, entry);
228228
}
229+
fclose(fp);
229230
if (finish_command(&diff_files))
230231
die("diff-files did not exit properly");
231232
strbuf_release(&index_env);
@@ -439,8 +440,10 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
439440
}
440441

441442
if (lmode && status != 'C') {
442-
if (checkout_path(lmode, &loid, src_path, &lstate))
443-
return error("could not write '%s'", src_path);
443+
if (checkout_path(lmode, &loid, src_path, &lstate)) {
444+
ret = error("could not write '%s'", src_path);
445+
goto finish;
446+
}
444447
}
445448

446449
if (rmode && !S_ISLNK(rmode)) {
@@ -456,9 +459,12 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
456459
hashmap_add(&working_tree_dups, entry);
457460

458461
if (!use_wt_file(workdir, dst_path, &roid)) {
459-
if (checkout_path(rmode, &roid, dst_path, &rstate))
460-
return error("could not write '%s'",
461-
dst_path);
462+
if (checkout_path(rmode, &roid, dst_path,
463+
&rstate)) {
464+
ret = error("could not write '%s'",
465+
dst_path);
466+
goto finish;
467+
}
462468
} else if (!is_null_oid(&roid)) {
463469
/*
464470
* Changes in the working tree need special
@@ -473,10 +479,12 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
473479
ADD_CACHE_JUST_APPEND);
474480

475481
add_path(&rdir, rdir_len, dst_path);
476-
if (ensure_leading_directories(rdir.buf))
477-
return error("could not create "
478-
"directory for '%s'",
479-
dst_path);
482+
if (ensure_leading_directories(rdir.buf)) {
483+
ret = error("could not create "
484+
"directory for '%s'",
485+
dst_path);
486+
goto finish;
487+
}
480488
add_path(&wtdir, wtdir_len, dst_path);
481489
if (symlinks) {
482490
if (symlink(wtdir.buf, rdir.buf)) {
@@ -497,13 +505,15 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
497505
}
498506
}
499507

508+
fclose(fp);
509+
fp = NULL;
500510
if (finish_command(&child)) {
501511
ret = error("error occurred running diff --raw");
502512
goto finish;
503513
}
504514

505515
if (!i)
506-
return 0;
516+
goto finish;
507517

508518
/*
509519
* Changes to submodules require special treatment.This loop writes a
@@ -626,6 +636,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
626636
exit_cleanup(tmpdir, rc);
627637

628638
finish:
639+
if (fp)
640+
fclose(fp);
641+
629642
free(lbase_dir);
630643
free(rbase_dir);
631644
strbuf_release(&ldir);

0 commit comments

Comments
 (0)