The branch stable/14 has been updated by des:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=7f412c6f88edc3aafb53a83f1743ad49da4679a9

commit 7f412c6f88edc3aafb53a83f1743ad49da4679a9
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 14:24:19 +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          | 16 ++++++++--------
 usr.bin/diff/diffreg.c          |  3 +++
 usr.bin/diff/tests/diff_test.sh | 19 +++++++++++++++++++
 3 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/usr.bin/diff/diffdir.c b/usr.bin/diff/diffdir.c
index 9ca9b5e1774e..7ea07645d751 100644
--- a/usr.bin/diff/diffdir.c
+++ b/usr.bin/diff/diffdir.c
@@ -42,8 +42,6 @@ static void diffit(struct dirent *, char *, size_t, struct 
dirent *,
        char *, size_t, int);
 static void print_only(const char *, size_t, const char *);
 
-#define d_status       d_type          /* we need to store status for -l */
-
 struct inode {
        dev_t dev;
        ino_t ino;
@@ -238,6 +236,8 @@ static void
 diffit(struct dirent *dp, char *path1, size_t plen1, struct dirent *dp2,
        char *path2, size_t plen2, int flags)
 {
+       int rc;
+
        flags |= D_HEADER;
        strlcpy(path1 + plen1, dp->d_name, PATH_MAX - plen1);
 
@@ -259,7 +259,6 @@ diffit(struct dirent *dp, char *path1, size_t plen1, struct 
dirent *dp2,
                        flags |= D_EMPTY1;
                        memset(&stb1, 0, sizeof(stb1));
                }
-
                if (lstat(path2, &stb2) != 0) {
                        if (!Nflag || errno != ENOENT) {
                                warn("%s", path2);
@@ -316,7 +315,6 @@ diffit(struct dirent *dp, char *path1, size_t plen1, struct 
dirent *dp2,
                        flags |= D_EMPTY1;
                        memset(&stb1, 0, sizeof(stb1));
                }
-
                if (stat(path2, &stb2) != 0) {
                        if (!Nflag || errno != ENOENT) {
                                warn("%s", path2);
@@ -329,6 +327,8 @@ diffit(struct dirent *dp, char *path1, size_t plen1, struct 
dirent *dp2,
                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);
@@ -338,12 +338,12 @@ diffit(struct dirent *dp, char *path1, size_t plen1, 
struct dirent *dp2,
                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 d4d7d6e1c9d7..954e0542b576 100644
--- a/usr.bin/diff/diffreg.c
+++ b/usr.bin/diff/diffreg.c
@@ -347,6 +347,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 b499bb709cdd..d28eeba9604d 100755
--- a/usr.bin/diff/tests/diff_test.sh
+++ b/usr.bin/diff/tests/diff_test.sh
@@ -423,6 +423,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
@@ -453,4 +471,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