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.
This commit is contained in:
Paul Eggert 2014-09-03 15:58:03 -07:00
parent 7bdd6479ce
commit d2fd9d4683
2 changed files with 10 additions and 2 deletions

View File

@ -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)

View File

@ -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