Subject: bug#32249: closed (Re: [bug-diffutils] bug#32249: [BUGFIX] cmp -b mangles result)

From: [email-masked] (GNU bug Tracking System)
Date: Tue, 24 Jul 2018 17:21:02 +0000

Your bug report

#32249: [BUGFIX] cmp -b mangles result

which was filed against the diffutils package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
32249: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=32249
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: [bug-diffutils] bug#32249: [BUGFIX] cmp -b mangles result
From: Paul Eggert <[email-masked]>
To: mancha <[email-masked]>, [email-masked]
Date: Tue, 24 Jul 2018 10:20:21 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

Thanks for the bug report. I suspect that fix won't work in general, 
though, and I installed the attached fix instead.
>From 131c2b6b081f5208bdcadae6614866054fc0535b Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Tue, 24 Jul 2018 10:18:28 -0700
Subject: [PATCH] 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.
---
 src/cmp.c |  2 ++
 tests/cmp | 10 ++++++++++
 2 files changed, 12 insertions(+)

diff --git a/src/cmp.c b/src/cmp.c
index 04638e3..2811392 100644
--- a/src/cmp.c
+++ b/src/cmp.c
@@ -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;
 }
 
diff --git a/tests/cmp b/tests/cmp
index 160c1ea..ca0fe5e 100755
--- a/tests/cmp
+++ b/tests/cmp
@@ -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
-- 
2.17.1

Subject: [BUGFIX] cmp -b mangles result
From: mancha <[email-masked]>
To: [email-masked]
Date: Mon, 23 Jul 2018 06:48:26 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi. count_newlines is passed the wrong buffer length (off-by-one) so it
overwrites first differing byte in first file's buffer. Attached patch
fixes.

Actual:

$ cmp -b <(echo bad) <(echo bug)
[snip] differ: byte 2, line 1 is  12 ^J 165 u

Expected:

$ cmp -b <(echo bad) <(echo bug)
[snip] differ: byte 2, line 1 is 141 a 165 u

Cheers!

--mancha
--- a/src/cmp.c
+++ b/src/cmp.c
@@ -466,7 +466,7 @@ cmp (void)
       byte_number += first_diff;
       if (comparison_type == type_first_diff && first_diff != 0)
 	{
-	  line_number += count_newlines (buf0, first_diff);
+	  line_number += count_newlines (buf0, first_diff + 1);
 	  at_line_start = buf0[first_diff - 1] == '\n';
 	}