The branch stable/13 has been updated by des:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=9c99eacbc33b357a0f80603b2e17c927a689fa78

commit 9c99eacbc33b357a0f80603b2e17c927a689fa78
Author:     Dag-Erling Smørgrav <[email protected]>
AuthorDate: 2026-02-05 14:39:57 +0000
Commit:     Dag-Erling Smørgrav <[email protected]>
CommitDate: 2026-02-10 15:36:24 +0000

    diff: Don't compare a file or directory to itself
    
    While here, stop abusing struct dirent for something we don't even need
    to store.
    
    PR:             254455
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    thj, kevans
    Differential Revision:  https://reviews.freebsd.org/D55113
    
    (cherry picked from commit 590126789c841d80655869bc075c8980c173dd1c)
    
    diff: Fix build
    
    rc must be defined first.
    
    Fixes:          590126789c84
    MFC after:      1 week
    X-MFC with:     590126789c84
    
    (cherry picked from commit ee44ab936e84bacaa49847d36aabdf280f9fecce)
---
 usr.bin/diff/diffdir.c          | 14 ++++++++------
 usr.bin/diff/diffreg.c          |  3 +++
 usr.bin/diff/tests/diff_test.sh | 19 +++++++++++++++++++
 3 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/usr.bin/diff/diffdir.c b/usr.bin/diff/diffdir.c
index 627e5dd9b983..200613927715 100644
--- a/usr.bin/diff/diffdir.c
+++ b/usr.bin/diff/diffdir.c
@@ -40,8 +40,6 @@
 static int selectfile(const struct dirent *);
 static void diffit(struct dirent *, char *, size_t, char *, size_t, int);
 
-#define d_status       d_type          /* we need to store status for -l */
-
 struct inode {
        dev_t dev;
        ino_t ino;
@@ -236,6 +234,8 @@ static void
 diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2,
     int flags)
 {
+       int rc;
+
        flags |= D_HEADER;
        strlcpy(path1 + plen1, dp->d_name, PATH_MAX - plen1);
        if (stat(path1, &stb1) != 0) {
@@ -260,6 +260,8 @@ diffit(struct dirent *dp, char *path1, size_t plen1, char 
*path2, size_t plen2,
        if (stb1.st_mode == 0)
                stb1.st_mode = stb2.st_mode;
 
+       if (stb1.st_dev == stb2.st_dev && stb1.st_ino == stb2.st_ino)
+               return;
        if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
                if (rflag)
                        diffdir(path1, path2, flags);
@@ -269,12 +271,12 @@ diffit(struct dirent *dp, char *path1, size_t plen1, char 
*path2, size_t plen2,
                return;
        }
        if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
-               dp->d_status = D_SKIPPED1;
+               rc = D_SKIPPED1;
        else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
-               dp->d_status = D_SKIPPED2;
+               rc = D_SKIPPED2;
        else
-               dp->d_status = diffreg(path1, path2, flags, 0);
-       print_status(dp->d_status, path1, path2, "");
+               rc = diffreg(path1, path2, flags, 0);
+       print_status(rc, path1, path2, "");
 }
 
 /*
diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c
index 78f3569a0ecc..a44c504304e6 100644
--- a/usr.bin/diff/diffreg.c
+++ b/usr.bin/diff/diffreg.c
@@ -338,6 +338,9 @@ diffreg(char *file1, char *file2, int flags, int capsicum)
                goto closem;
        }
 
+       if (stb1.st_dev == stb2.st_dev && stb1.st_ino == stb2.st_ino)
+               goto closem;
+
        if (lflag)
                pr = start_pr(file1, file2);
 
diff --git a/usr.bin/diff/tests/diff_test.sh b/usr.bin/diff/tests/diff_test.sh
index 181463ca9be7..5e1c36d14ed2 100755
--- a/usr.bin/diff/tests/diff_test.sh
+++ b/usr.bin/diff/tests/diff_test.sh
@@ -348,6 +348,24 @@ prleak_body()
        atf_check diff -rul a b
 }
 
+same_head()
+{
+       atf_set "descr" "Don't diff a file or directory with itself"
+}
+same_body()
+{
+       local n=256
+       mkdir a
+       for hi in $(jot -w%02x $n 0) ; do
+               mkdir a/$hi
+               for lo in $(jot -w%02x $n 0) ; do
+                       echo "$hi$lo" >a/$hi/$lo
+               done
+       done
+       ln -s a b
+       atf_check timeout 1s diff -rqs a b
+}
+
 atf_init_test_cases()
 {
        atf_add_test_case simple
@@ -374,4 +392,5 @@ atf_init_test_cases()
        atf_add_test_case bigc
        atf_add_test_case bigu
        atf_add_test_case prleak
+       atf_add_test_case same
 }

Reply via email to