Skip to content

Commit 98e4015

Browse files
simarkgitster
authored andcommitted
builtin/difftool: intialize some hashmap variables
When running a dir-diff command that produces no diff, variables `wt_modified` and `tmp_modified` are used while uninitialized, causing: $ /home/smarchi/src/git/git-difftool --dir-diff master free(): invalid pointer [1] 334004 IOT instruction (core dumped) /home/smarchi/src/git/git-difftool --dir-diff master $ valgrind --track-origins=yes /home/smarchi/src/git/git-difftool --dir-diff master ... Invalid free() / delete / delete[] / realloc() at 0x48478EF: free (vg_replace_malloc.c:989) by 0x422CAC: hashmap_clear_ (hashmap.c:208) by 0x283830: run_dir_diff (difftool.c:667) by 0x284103: cmd_difftool (difftool.c:801) by 0x238E0F: run_builtin (git.c:484) by 0x2392B9: handle_builtin (git.c:750) by 0x2399BC: cmd_main (git.c:921) by 0x356FEF: main (common-main.c:64) Address 0x1ffefff180 is on thread 1's stack in frame #2, created by run_dir_diff (difftool.c:358) ... If taking any `goto finish` path before these variables are initialized, `hashmap_clear_and_free()` operates on uninitialized data, sometimes causing a crash. This regression was introduced in 7f795a1 (builtin/difftool: plug several trivial memory leaks, 2024-09-26). Fix it by initializing those variables with the `HASHMAP_INIT` macro. Add a test comparing the main branch to itself, resulting in no diff. Signed-off-by: Simon Marchi <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b31fb63 commit 98e4015

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

builtin/difftool.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
376376
struct checkout lstate, rstate;
377377
int err = 0;
378378
struct child_process cmd = CHILD_PROCESS_INIT;
379-
struct hashmap wt_modified, tmp_modified;
379+
struct hashmap wt_modified = HASHMAP_INIT(path_entry_cmp, NULL);
380+
struct hashmap tmp_modified = HASHMAP_INIT(path_entry_cmp, NULL);
380381
int indices_loaded = 0;
381382

382383
workdir = repo_get_work_tree(the_repository);
@@ -601,9 +602,6 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
601602
* in the common case of --symlinks and the difftool updating
602603
* files through the symlink.
603604
*/
604-
hashmap_init(&wt_modified, path_entry_cmp, NULL, wtindex.cache_nr);
605-
hashmap_init(&tmp_modified, path_entry_cmp, NULL, wtindex.cache_nr);
606-
607605
for (i = 0; i < wtindex.cache_nr; i++) {
608606
struct hashmap_entry dummy;
609607
const char *name = wtindex.cache[i]->name;

t/t7800-difftool.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,10 @@ run_dir_diff_test 'difftool --dir-diff syncs worktree without unstaged change' '
666666
test_cmp expect file
667667
'
668668

669+
run_dir_diff_test 'difftool --dir-diff with no diff' '
670+
git difftool -d main main
671+
'
672+
669673
write_script modify-file <<\EOF
670674
echo "new content" >file
671675
EOF

0 commit comments

Comments
 (0)