Re: git: 590126789c84 - main - diff: Don't compare a file or directory to itself
Date: Thu, 05 Feb 2026 15:38:50 UTC
Am Tage des Herren Thu, 05 Feb 2026 14:45:49 +0000
Dag-Erling Smørgrav <des@FreeBSD.org> schrieb:
> The branch main has been updated by des:
>
> 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(-)
>
> diff --git a/usr.bin/diff/diffdir.c b/usr.bin/diff/diffdir.c
> index a55a2bec70ee..bd8ef73e785a 100644
> --- a/usr.bin/diff/diffdir.c
> +++ b/usr.bin/diff/diffdir.c
> @@ -41,8 +41,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;
> @@ -258,7 +256,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);
> @@ -315,7 +312,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);
> @@ -328,6 +324,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);
> @@ -337,12 +335,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 91ae5ee6591a..95c724817ede 100644
> --- a/usr.bin/diff/diffreg.c
> +++ b/usr.bin/diff/diffreg.c
> @@ -372,6 +372,9 @@ diffreg_stone(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 a3f76602cf37..0c8147c7ed18 100755
> --- a/usr.bin/diff/tests/diff_test.sh
> +++ b/usr.bin/diff/tests/diff_test.sh
> @@ -432,6 +432,24 @@ prleak_body()
> atf_check diff -Astone -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
> @@ -462,4 +480,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
> }
>
Buildworld seems to fail with newly added diff code:
[...]
--- all_subdir_usr.bin ---
342 | rc = diffreg(path1, path2, flags, 0);
| ^
/usr/src/usr.bin/diff/diffdir.c:343:15: error: use of undeclared identifier 'rc'
343 | print_status(rc, path1, path2, "");
Kind regards,
oh
--
A FreeBSD user