cmp: fix bug in -b diagnostic

Problem reported by mancha (Bug#32249).
* src/cmp.c (count_newlines): Restore old value of sentinel.
* tests/cmp: Test for the bug.
This commit is contained in:
Paul Eggert 2018-07-24 10:18:28 -07:00
parent 612db4defa
commit ffd63aedad
2 changed files with 12 additions and 0 deletions

View File

@ -639,9 +639,11 @@ count_newlines (char *buf, size_t bufsize)
size_t count = 0;
char *p;
char *lim = buf + bufsize;
char ch = *lim;
*lim = '\n';
for (p = buf; (p = rawmemchr (p, '\n')) != lim; p++)
count++;
*lim = ch;
return count;
}

View File

@ -208,4 +208,14 @@ done >out1
compare exp1 out1 || fail=1
printf 'bad\n' >bad
printf 'bug\n' >bug
echo LC_ALL=C cmp -b bad bug
LC_ALL=C cmp -b bad bug
test $? -eq 1 || fail=1
case `LC_ALL=C cmp -b bad bug` in
'bad bug differ: byte 2, line 1 is '*' a '*' u') ;;
*) echo 'expected cmp -b to report a and u'; fail=1;;
esac
Exit $fail