summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Kostyukevich <maxim.kostyukevich@mera.com>2019-08-20 03:08:32 +0300
committerPatrick Steinhardt <ps@pks.im>2020-03-26 16:20:11 +0100
commit0126e3fc1317cf7a1ef0fd5ad1af460be547701e (patch)
tree3df666f20fc4b38bb1d0799ed6c36e8db2608cf6
parentae9b333a498e49efbda92985c72be52e4036e7d0 (diff)
downloadlibgit2-0126e3fc1317cf7a1ef0fd5ad1af460be547701e.tar.gz
apply: Fix a patch corruption related to EOFNL handling
Use of apply's API can lead to an improper patch application and a corruption of the modified file. The issue is caused by mishandling of the end of file changes if there are several hunks to apply. The new line character is added to a line from a wrong hunk. The solution is to modify apply_hunk() to add the newline character at the end of a line from a right hunk.
-rw-r--r--src/apply.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/apply.c b/src/apply.c
index 3ec63f2fb..982d9af32 100644
--- a/src/apply.c
+++ b/src/apply.c
@@ -210,7 +210,7 @@ static int apply_hunk(
case GIT_DIFF_LINE_CONTEXT_EOFNL:
case GIT_DIFF_LINE_DEL_EOFNL:
case GIT_DIFF_LINE_ADD_EOFNL:
- prev = i ? git_array_get(patch->lines, i - 1) : NULL;
+ prev = i ? git_array_get(patch->lines, linenum - 1) : NULL;
if (prev && prev->content[prev->content_len - 1] == '\n')
prev->content_len -= 1;
break;