Bug 254455 - BSD diff(1) compares same files and dirs
Summary: BSD diff(1) compares same files and dirs
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 12.2-STABLE
Hardware: Any Any
: --- Affects Some People
Assignee: Dag-Erling Smørgrav
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-03-21 09:31 UTC by Andre Albsmeier
Modified: 2026-02-11 06:16 UTC (History)
2 users (show)

See Also:
des: mfc-stable15+
des: mfc-stable14+
des: mfc-stable13+


Attachments
patch (1.35 KB, patch)
2021-03-21 09:31 UTC, Andre Albsmeier
no flags Details | Diff
patches for diff (1.40 KB, patch)
2025-02-28 14:14 UTC, Mark Linimon
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andre Albsmeier 2021-03-21 09:31:47 UTC
Created attachment 223468 [details]
patch

I was wondering why a "diff -rq" on FreeBSD-12 (BSD diff) ran endlessly while on FreeBSD-11 (GNU diff) it terminated in 0.6 seconds.

This is because BSD diff compares files and dirs even if they are the same. I checked what GNU diff does in this case and added the corresponding code to diffdir() and diffreg(). The style is probably considered bad ;-).
Comment 1 Mark Linimon freebsd_committer freebsd_triage 2025-02-28 14:14:57 UTC
Created attachment 258071 [details]
patches for diff

^Triage: rebase patch.
Comment 2 Dag-Erling Smørgrav freebsd_committer freebsd_triage 2026-02-04 13:12:27 UTC
Does this still occur with the new diff(1) implementation in 15 and 16?
Comment 3 Andre Albsmeier 2026-02-04 16:02:41 UTC
I don't have access to machines running 15 or 16 so I can't test. I tried to build the new diff on a 14-STABLE box but failed due to missing fdscandir() in libc.

But it probably can be tested easily by anyone on 15 or 16 by comparing a big dir with itself -- e.g. somthing like

diff -rq /usr/src /usr/src

If this does not return immediately, the problem is still there.
Comment 4 commit-hook freebsd_committer freebsd_triage 2026-02-05 14:46:53 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=590126789c841d80655869bc075c8980c173dd1c

commit 590126789c841d80655869bc075c8980c173dd1c
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-02-05 14:39:57 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-02-05 14:39:57 +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

 usr.bin/diff/diffdir.c          | 14 ++++++--------
 usr.bin/diff/diffreg.c          |  3 +++
 usr.bin/diff/tests/diff_test.sh | 19 +++++++++++++++++++
 3 files changed, 28 insertions(+), 8 deletions(-)
Comment 5 commit-hook freebsd_committer freebsd_triage 2026-02-10 15:28:03 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=5fdcdf4a7ed53bb1e49cd627a0ebeaf3751a7f0b

commit 5fdcdf4a7ed53bb1e49cd627a0ebeaf3751a7f0b
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-02-05 14:39:57 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-02-10 14:24:08 +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(-)
Comment 6 commit-hook freebsd_committer freebsd_triage 2026-02-10 15:28:08 UTC
A commit in branch stable/14 references this bug:

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

commit 7f412c6f88edc3aafb53a83f1743ad49da4679a9
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-02-05 14:39:57 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
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(-)
Comment 7 commit-hook freebsd_committer freebsd_triage 2026-02-10 15:37:19 UTC
A commit in branch stable/13 references this bug:

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

commit 9c99eacbc33b357a0f80603b2e17c927a689fa78
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-02-05 14:39:57 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
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(-)
Comment 8 commit-hook freebsd_committer freebsd_triage 2026-02-11 06:16:07 UTC
A commit in branch releng/14.4 references this bug:

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

commit b4139147bbb7e368316bee86ffac0fdb79319839
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-02-05 14:39:57 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2026-02-11 06:14:30 +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.

    Approved by:    re (cperciva)
    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)
    (cherry picked from commit 7f412c6f88edc3aafb53a83f1743ad49da4679a9)

 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(-)