Thanks for the bug report. I installed the attached patch to fix the bug and
help prevent its recurrence.
From 99e0ab04b5b77e57609c1f5341799bccf4202559 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Fri, 20 Nov 2026 17:43:30 -0800
Subject: [PATCH] cmp: fix exit status with -l
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Problem reported by Ben (AlphaTrade <[email protected]>) in:
https://lists.gnu.org/r/bug-diffutils/2026-11/msg00008.html
https://bugs.gnu.org/82289
* src/cmp.c (cmp): Initialize ‘differing’ before loop starts,
not each time through the loop.
* tests/cmp: Add a regression test for this.
---
src/cmp.c | 3 +--
tests/cmp | 7 +++++++
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/src/cmp.c b/src/cmp.c
index a4af764..97656c3 100644
--- a/src/cmp.c
+++ b/src/cmp.c
@@ -406,6 +406,7 @@ cmp (void)
word *buffer1 = buffer[1];
char *buf0 = (char *) buffer0;
char *buf1 = (char *) buffer1;
+ int differing = 0;
/* For -l, the print width of the offset, a positive number.
Otherwise, the negative of the comparison type.
@@ -528,8 +529,6 @@ cmp (void)
at_line_start = buf0[first_diff - 1] == '\n';
}
- int differing = 0;
-
if (first_diff < smaller)
{
switch (offset_width)
diff --git a/tests/cmp b/tests/cmp
index 76aabfa..dcdc03f 100755
--- a/tests/cmp
+++ b/tests/cmp
@@ -251,4 +251,11 @@ if timeout 0.1 true && truncate -s 14T a && truncate -s 15T b; then
fi
rm -f a b
+printf '\1' >b
+if truncate -s 1G a b; then
+ returns_ 1 cmp -l a b >out || fail=1
+ test "$(echo $(cat out))" = '1 0 1' || fail=1
+fi
+rm -f a b
+
Exit $fail
--
2.53.0