diff options
author | Russell Belfer <rb@github.com> | 2013-01-08 17:11:11 -0800 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2013-01-08 17:11:11 -0800 |
commit | de590550172c68bf374f9d12636b06295c5fe048 (patch) | |
tree | 3bf0c7ca4b554784de72063b25933775308fb512 /tests-clar/diff/workdir.c | |
parent | f7e4a7c2d5e314fe50fee851263e4ed6cfaa49ae (diff) | |
download | libgit2-de590550172c68bf374f9d12636b06295c5fe048.tar.gz |
Resolve crash with diff against empty file
It is not legal inside our `p_mmap` function to mmap a zero length
file. This adds a test that exercises that case inside diff and
fixes the code path where we would try to do that.
The fix turns out not to be a lot of code since our default file
content is already initialized to "" which works in this case.
Fixes #1210
Diffstat (limited to 'tests-clar/diff/workdir.c')
-rw-r--r-- | tests-clar/diff/workdir.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c index 1b1b616a4..5c89b95e7 100644 --- a/tests-clar/diff/workdir.c +++ b/tests-clar/diff/workdir.c @@ -881,3 +881,46 @@ void test_diff_workdir__checks_options_version(void) err = giterr_last(); cl_assert_equal_i(GITERR_INVALID, err->klass); } + +void test_diff_workdir__can_diff_empty_file(void) +{ + git_diff_list *diff; + git_tree *tree; + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + struct stat st; + git_diff_patch *patch; + + g_repo = cl_git_sandbox_init("attr_index"); + + tree = resolve_commit_oid_to_tree(g_repo, "3812cfef3661"); /* HEAD */ + + /* baseline - make sure there are no outstanding diffs */ + + cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts)); + cl_assert_equal_i(2, (int)git_diff_num_deltas(diff)); + git_diff_list_free(diff); + + /* empty contents of file */ + + cl_git_rewritefile("attr_index/README.txt", ""); + cl_git_pass(git_path_lstat("attr_index/README.txt", &st)); + cl_assert_equal_i(0, (int)st.st_size); + + cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts)); + cl_assert_equal_i(3, (int)git_diff_num_deltas(diff)); + /* diffs are: .gitattributes, README.txt, sub/sub/.gitattributes */ + cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 1)); + git_diff_patch_free(patch); + git_diff_list_free(diff); + + /* remove a file altogether */ + + cl_git_pass(p_unlink("attr_index/README.txt")); + cl_assert(!git_path_exists("attr_index/README.txt")); + + cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts)); + cl_assert_equal_i(3, (int)git_diff_num_deltas(diff)); + cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 1)); + git_diff_patch_free(patch); + git_diff_list_free(diff); +} |