From d2fd9d4683ef60c259a3b426f71cef1b89ff383d Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 3 Sep 2014 15:58:03 -0700 Subject: [PATCH 2/3] diff: fix bug with diff -B and incomplete lines Reported by Navin Kabra via Eric Blake in: http://bugs.gnu.org/18402 * src/util.c (analyze_hunk): Don't mishandle incomplete lines at end of file. * tests/no-newline-at-eof: Test for the bug. --- src/util.c | 6 ++++-- tests/no-newline-at-eof | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/util.c b/src/util.c index 016057d..44ce61f 100644 --- a/src/util.c +++ b/src/util.c @@ -817,7 +817,8 @@ analyze_hunk (struct change *hunk, for (i = next->line0; i <= l0 && trivial; i++) { char const *line = linbuf0[i]; - char const *newline = linbuf0[i + 1] - 1; + char const *lastbyte = linbuf0[i + 1] - 1; + char const *newline = lastbyte + (*lastbyte != '\n'); size_t len = newline - line; char const *p = line; if (skip_white_space) @@ -837,7 +838,8 @@ analyze_hunk (struct change *hunk, for (i = next->line1; i <= l1 && trivial; i++) { char const *line = linbuf1[i]; - char const *newline = linbuf1[i + 1] - 1; + char const *lastbyte = linbuf1[i + 1] - 1; + char const *newline = lastbyte + (*lastbyte != '\n'); size_t len = newline - line; char const *p = line; if (skip_white_space) diff --git a/tests/no-newline-at-eof b/tests/no-newline-at-eof index 14d5f49..f503718 100755 --- a/tests/no-newline-at-eof +++ b/tests/no-newline-at-eof @@ -50,4 +50,10 @@ compare exp2 out || fail=1 # expect empty stderr compare /dev/null err || fail=1 +# Test for Bug#18402. +printf a > a +printf b > b +diff -B a b > out 2>err +test $? = 1 || fail=1 + Exit $fail -- 1.9.3