diff options
author | Patrick Steinhardt <ps@pks.im> | 2019-07-05 09:35:43 +0200 |
---|---|---|
committer | Patrick Steinhardt <ps@pks.im> | 2019-07-05 09:35:43 +0200 |
commit | dedf70ad2b9f3dd5a5b299f64476285653cf62b7 (patch) | |
tree | e9d703ddd8763459141c67138b413d72386ff2b1 /tests/patch/parse.c | |
parent | c4c1500a8bc99066cbb4340717d91020c02a75ae (diff) | |
download | libgit2-dedf70ad2b9f3dd5a5b299f64476285653cf62b7.tar.gz |
patch_parse: do not depend on parsed buffer's lifetime
When parsing a patch from a buffer, we let the patch lines point into
the original buffer. While this is efficient use of resources, this also
ties the lifetime of the parsed patch to the parsed buffer. As this
behaviour is not documented anywhere in our API it is very surprising to
its users.
Untie the lifetime by duplicating the lines into the parsed patch. Add a
test that verifies that lifetimes are indeed independent of each other.
Diffstat (limited to 'tests/patch/parse.c')
-rw-r--r-- | tests/patch/parse.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/tests/patch/parse.c b/tests/patch/parse.c index a40ad7b23..7eb987920 100644 --- a/tests/patch/parse.c +++ b/tests/patch/parse.c @@ -108,3 +108,23 @@ void test_patch_parse__files_with_whitespaces_succeeds(void) cl_git_pass(git_patch_from_buffer(&patch, PATCH_NAME_WHITESPACE, strlen(PATCH_NAME_WHITESPACE), NULL)); git_patch_free(patch); } + +void test_patch_parse__lifetime_of_patch_does_not_depend_on_buffer(void) +{ + git_buf diff = GIT_BUF_INIT, rendered = GIT_BUF_INIT; + git_patch *patch; + + cl_git_pass(git_buf_sets(&diff, PATCH_ORIGINAL_TO_CHANGE_MIDDLE)); + cl_git_pass(git_patch_from_buffer(&patch, diff.ptr, diff.size, NULL)); + git_buf_dispose(&diff); + + cl_git_pass(git_patch_to_buf(&rendered, patch)); + cl_assert_equal_s(PATCH_ORIGINAL_TO_CHANGE_MIDDLE, rendered.ptr); + git_buf_dispose(&rendered); + + cl_git_pass(git_patch_to_buf(&rendered, patch)); + cl_assert_equal_s(PATCH_ORIGINAL_TO_CHANGE_MIDDLE, rendered.ptr); + git_buf_dispose(&rendered); + + git_patch_free(patch); +} |