Skip to content

Commit

Permalink
stash: fix segmentation fault when files were added with intent
Browse files Browse the repository at this point in the history
After `git add -N <file>`, the index is in a special state. A state for
which the built-in stash was not prepared, as it failed to initialize
the `rev` structure in that case before using `&rev.pending`.  If
`reset_tree()` returns a non-zero value, `stash_working_tree()`
calls `object_array_clear()` with `&rev.pending`.  If `rev` is not
initialized, this causes a segmentation fault.

Prevent this by initializing `rev` before calling `reset_tree()`.

This fixes #2006.

[jes: modified the commit message in preparation for sending this patch
to the Git mailing list.]

Signed-off-by: Matthew Kraai <mkraai@its.jnj.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
kraai authored and dscho committed Feb 14, 2019
1 parent 59e7bbc commit 43aef18
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion builtin/stash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1050,14 +1050,15 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
struct strbuf diff_output = STRBUF_INIT;
struct index_state istate = { NULL };

init_revisions(&rev, NULL);

set_alternate_index_output(stash_index_path.buf);
if (reset_tree(&info->i_tree, 0, 0)) {
ret = -1;
goto done;
}
set_alternate_index_output(NULL);

init_revisions(&rev, NULL);
rev.prune_data = ps;
rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
rev.diffopt.format_callback = add_diff_to_buf;
Expand Down
8 changes: 8 additions & 0 deletions t/t3903-stash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ test_expect_success 'stash an added file' '
test new = "$(cat file3)"
'

test_expect_success 'stash --intent-to-add file' '
git reset --hard &&
echo new >file4 &&
git add --intent-to-add file4 &&
test_when_finished "git rm -f file4" &&
test_must_fail git stash
'

test_expect_success 'stash rm then recreate' '
git reset --hard &&
git rm file &&
Expand Down

0 comments on commit 43aef18

Please sign in to comment.