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';
 	}