diff does not show a unified diff when one of the file is empty

This commit is contained in:
Than Ngo 2025-03-27 15:15:37 +01:00
commit 1159f22114
4 changed files with 119 additions and 28 deletions

View file

@ -0,0 +1,82 @@
From 6ce0ebd033c395265c262ae3aab6477a49d4c2f1 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Thu, 20 Feb 2025 21:59:55 -0800
Subject: [PATCH] diff: don't treat empty files as a different file type
Reported by Kate Deplaix <kit-ty-kate@outlook.com> in
<https://lists.gnu.org/r/bug-diffutils/2025-02/msg00005.html>.
* src/diff.c (compare_prepped_files): Don't rely on string
file type, as that might not agree with our idea of a file type.
---
src/diff.c | 26 +++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)
diff --git a/src/diff.c b/src/diff.c
index fa6100e..6e1bbc5 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -1223,14 +1223,14 @@ compare_prepped_files (struct comparison const *parent,
the type is unusual, then simply report their type.
However, at the top level do this only if one file is a symlink
and the other is not. */
- if (toplevel
- ? (!S_ISLNK (cmp->file[0].stat.st_mode)
- != !S_ISLNK (cmp->file[1].stat.st_mode))
- : (cmp->file[0].filetype != cmp->file[1].filetype
- || ! (S_ISREG (cmp->file[0].stat.st_mode)
- || S_ISLNK (cmp->file[0].stat.st_mode)
- || S_ISCHR (cmp->file[0].stat.st_mode)
- || S_ISBLK (cmp->file[0].stat.st_mode))))
+ mode_t mode0 = cmp->file[0].stat.st_mode;
+ mode_t mode1 = cmp->file[1].stat.st_mode;
+ if (toplevel ? !S_ISLNK (mode0) != !S_ISLNK (mode1)
+ : S_ISREG (mode0) ? !S_ISREG (mode1)
+ : S_ISLNK (mode0) ? !S_ISLNK (mode1)
+ : S_ISCHR (mode0) ? !S_ISCHR (mode1)
+ : S_ISBLK (mode0) ? !S_ISBLK (mode1)
+ : true)
{
/* POSIX 1003.1-2017 says any message will do, so long as it
contains the file names. */
@@ -1244,7 +1244,7 @@ compare_prepped_files (struct comparison const *parent,
}
/* If both files are symlinks, compare symlink contents. */
- if (S_ISLNK (cmp->file[0].stat.st_mode))
+ if (S_ISLNK (mode0))
{
/* We get here only if we are not dereferencing symlinks. */
dassert (no_dereference_symlinks);
@@ -1295,7 +1295,7 @@ compare_prepped_files (struct comparison const *parent,
and report file types of all other non-regular files.
POSIX 1003.1-2017 says any message will do,
so long as it contains the file names. */
- if (!toplevel && !S_ISREG (cmp->file[0].stat.st_mode))
+ if (!toplevel && !S_ISREG (mode0))
{
if (cmp->file[0].stat.st_rdev == cmp->file[1].stat.st_rdev)
return EXIT_SUCCESS;
@@ -1311,7 +1311,7 @@ compare_prepped_files (struct comparison const *parent,
for (int i = 0; i < n_num; i++)
sprintf (numbuf[i], "%"PRIdMAX, num[i]);
- message ((S_ISCHR (cmp->file[0].stat.st_mode)
+ message ((S_ISCHR (mode0)
? ("Character special files %s (%s, %s)"
" and %s (%s, %s) differ\n")
: ("Block special files %s (%s, %s)"
@@ -1323,8 +1323,8 @@ compare_prepped_files (struct comparison const *parent,
}
if (files_can_be_treated_as_binary
- && S_ISREG (cmp->file[0].stat.st_mode)
- && S_ISREG (cmp->file[1].stat.st_mode)
+ && S_ISREG (mode0)
+ && S_ISREG (mode1)
&& cmp->file[0].stat.st_size != cmp->file[1].stat.st_size
&& 0 <= cmp->file[0].stat.st_size
&& 0 <= cmp->file[1].stat.st_size)
--
2.34.1

View file

@ -0,0 +1,30 @@
From 03e529dd69d50c247a217b9b659659538dfa397a Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.funk1@gmail.com>
Date: Thu, 27 Feb 2025 20:15:55 -0800
Subject: [PATCH] diff: fix allocation size computation that could cause bad
writes
Reported by Nick Smallbone <nick@smallbone.se> in:
<https://lists.gnu.org/r/bug-diffutils/2025-02/msg00012.html>.
* src/io.c (find_and_hash_each_line): Fix size computation.
---
src/io.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/io.c b/src/io.c
index a62c529..adb4f50 100644
--- a/src/io.c
+++ b/src/io.c
@@ -1012,7 +1012,7 @@ find_and_hash_each_line (struct file_data *current)
linbuf += linbuf_base;
linbuf = xpalloc (linbuf, &n, 1, -1, sizeof *linbuf);
linbuf -= linbuf_base;
- alloc_lines = n - linbuf_base;
+ alloc_lines = linbuf_base + n;
}
linbuf[line] = p;
--
2.48.1

View file

@ -1,26 +0,0 @@
diff --git a/NEWS b/NEWS
index 73d98d6..bfc4035 100644
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,9 @@ GNU diffutils NEWS -*- outline -*-
file to a nonempty regular file.
[bug#76452 introduced in 3.11]
+ diff -y no longer crashes when given nontrivial differences.
+ [bug#76613 introduced in 3.11]
+
* Noteworthy changes in release 3.11 (2025-02-02) [stable]
diff --git a/src/io.c b/src/io.c
index a62c529..eba4aba 100644
--- a/src/io.c
+++ b/src/io.c
@@ -1012,7 +1012,7 @@ find_and_hash_each_line (struct file_data *current)
linbuf += linbuf_base;
linbuf = xpalloc (linbuf, &n, 1, -1, sizeof *linbuf);
linbuf -= linbuf_base;
- alloc_lines = n - linbuf_base;
+ alloc_lines = linbuf_base + n;
}
linbuf[line] = p;

View file

@ -1,11 +1,13 @@
Summary: GNU collection of diff utilities
Name: diffutils
Version: 3.11
Release: 2%{?dist}
Release: 3%{?dist}
URL: https://www.gnu.org/software/diffutils/diffutils.html
Source: https://ftp.gnu.org/gnu/diffutils/diffutils-%{version}.tar.xz
# upstream fixes
Patch10: diffutils-3.11-allocation-typo-leading-to-crashes.patch
Patch10: 0001-diff-don-t-treat-empty-files-as-a-different-file-typ.patch
Patch11: 0001-diff-fix-allocation-size-computation-that-could-caus.patch
License: GPL-3.0-or-later
Provides: bundled(gnulib)
BuildRequires: gcc
@ -55,6 +57,9 @@ make check
%{_infodir}/diffutils.info*
%changelog
* Thu Mar 27 2025 Than Ngo <than@redhat.com> - 3.11-3
- diff does not show a unified diff when one of the file is empty
* Thu Mar 27 2025 Than Ngo <than@redhat.com> - 3.11-2
- Backported upstream patch, Fixed allocation typo leading to crash