Skip to content

Commit 630127e

Browse files
author
Max Kostyukevich
committed
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.
1 parent 0f40e68 commit 630127e

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static int apply_hunk(
219219
case GIT_DIFF_LINE_CONTEXT_EOFNL:
220220
case GIT_DIFF_LINE_DEL_EOFNL:
221221
case GIT_DIFF_LINE_ADD_EOFNL:
222-
prev = i ? git_array_get(patch->lines, i - 1) : NULL;
222+
prev = i ? git_array_get(patch->lines, linenum - 1) : NULL;
223223
if (prev && prev->content[prev->content_len - 1] == '\n')
224224
prev->content_len -= 1;
225225
break;

0 commit comments

Comments
 (0)