path: root/apply.c
diff options
authorRené Scharfe <>2017-11-16 18:50:31 (GMT)
committerJunio C Hamano <>2017-11-17 01:42:08 (GMT)
commit4855de123391daab82407b44de03ba5647e97694 (patch)
tree0f96616c43311d6431fa224571c51f0430514f57 /apply.c
parent9752ad0bb79f680bca48db7adc45338b298304b0 (diff)
apply: update line lengths for --inaccurate-eof
Some diff implementations don't report missing newlines at the end of files. Applying such a patch can cause a newline character to be added inadvertently. The option --inaccurate-eof of git apply can be used to remove trailing newlines if needed. apply_one_fragment() cuts it off from the buffers for preimage and postimage. Before it does, it builds an array with the lengths of each line for both. Make sure to update the length of the last line in these line info structures as well to keep them consistent with their respective buffer. Without this fix the added test fails; git apply dies and reports: fatal: BUG: caller miscounted postlen: asked 1, orig = 1, used = 2 That sanity check is only called if whitespace changes are ignored. Reported-by: Mahmoud Al-Qudsi <> Signed-off-by: Rene Scharfe <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'apply.c')
1 files changed, 2 insertions, 0 deletions
diff --git a/apply.c b/apply.c
index 0e2caea..e0ad05b 100644
--- a/apply.c
+++ b/apply.c
@@ -2941,6 +2941,8 @@ static int apply_one_fragment(struct apply_state *state,
newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {
strbuf_setlen(&newlines, newlines.len - 1);
+ preimage.line_allocated[ - 1].len--;
+ postimage.line_allocated[ - 1].len--;
leading = frag->leading;