Skip to content

Commit 1ff595d

Browse files
Denton-Lgitster
authored andcommitted
stash show: fix segfault with --{include,only}-untracked
When `git stash show --include-untracked` or `git stash show --only-untracked` is run on a stash that doesn't include an untracked entry, a segfault occurs. This happens because we do not check whether the untracked entry is actually present and just attempt to blindly dereference it. Ensure that the untracked entry is present before actually attempting to dereference it. Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aa2b05d commit 1ff595d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

builtin/stash.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,10 +900,14 @@ static int show_stash(int argc, const char **argv, const char *prefix)
900900
diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
901901
break;
902902
case UNTRACKED_ONLY:
903-
diff_root_tree_oid(&info.u_tree, "", &rev.diffopt);
903+
if (info.has_u)
904+
diff_root_tree_oid(&info.u_tree, "", &rev.diffopt);
904905
break;
905906
case UNTRACKED_INCLUDE:
906-
diff_include_untracked(&info, &rev.diffopt);
907+
if (info.has_u)
908+
diff_include_untracked(&info, &rev.diffopt);
909+
else
910+
diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
907911
break;
908912
}
909913
log_tree_diff_flush(&rev);

t/t3905-stash-include-untracked.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,19 @@ test_expect_success 'stash show --include-untracked errors on duplicate files' '
405405
test_i18ngrep "worktree and untracked commit have duplicate entries: tracked" err
406406
'
407407

408+
test_expect_success 'stash show --{include,only}-untracked on stashes without untracked entries' '
409+
git reset --hard &&
410+
git clean -xf &&
411+
>tracked &&
412+
git add tracked &&
413+
git stash &&
414+
415+
git stash show >expect &&
416+
git stash show --include-untracked >actual &&
417+
test_cmp expect actual &&
418+
419+
git stash show --only-untracked >actual &&
420+
test_must_be_empty actual
421+
'
422+
408423
test_done

0 commit comments

Comments
 (0)