From nobody Thu Jun 4 14:40:25 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4gWS0T3jJJz6gPnT for ; Thu, 04 Jun 2026 14:40:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R13" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4gWS0T2fZYz3ph4 for ; Thu, 04 Jun 2026 14:40:25 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1780584025; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=3D5E7ALMW8H2wxZpQDOpSc4NO9Oa17RbSLPcR7p1Onw=; b=Md/6duHMX+SyVQ/ZT2ut1MupPG3CRMZjDmYgonRYPA94oqtdpjwwEUS1rQYBAbXNLtULbj 8fcrdIc35zsSmViRAnD0xcxtVo9bCGXT+IwS5g2Ji3QYr1/P4cpmTvECs+Wocorsk+owJL y/zVVc/joQoE4Eo6rdWcNoj/NnapEXFAZKrCBqAQe2di2sDTEAQpwZRxdwC1EOZUyA2lG5 8oG1zCOnXHoyWVTZWsBsh+PvKAVWM3sh85ATVVvFXxLMpJ1Ofu54kGgtunbnrxqz/GW4Z6 gibtzRiAm86QojLq4WJKkhUxys+t6Pqd1Mem45e0MQ2BfJxr/QoVCYmlvPLO3g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1780584025; a=rsa-sha256; cv=none; b=XMJsm8zxsBhk9jHysctI5UCoWgh0uZ12NF5REmF+gRaujhNHk5bNOaVk7ARBTCQkDIG/wn KTbdHvBGIYxsYoviMwNv0Ma2tT+5ZDL/+qdcvpZFUrLbx6sLs9GULDWRxOdLvLecQt4TQV M6hMb7FbXD4KLiUMg0D9cA/lo0ZIbSTWyEJRdb4vhyiH1wvtTeuAsZRiRj7nw9EZR4B99s yoACcNmiiTRGUkVKeKeP8XxNDlwkeHQ4WNsVKKmc5wg7+c1U6F9Zz2BsisdW+2RozHo4LJ JVQJwpr0qpWCjEi4YtJ4xjRjW33YxHaGiQ2NCl0oWHprqFKYGZLsc9SD3mbPsg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1780584025; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=3D5E7ALMW8H2wxZpQDOpSc4NO9Oa17RbSLPcR7p1Onw=; b=EuYNNykzo59J+SzXUq4aU45xibIUy2n5E4/Nz1iaKBh1n8rLqGmSz5Pjgv/2YrPY0c73z/ Oo/TjNjcyWu4EGvpayHZUUaUpsHYAcJpI5au6lMATR3My/b/8OhzeavcQ6Om7wOQVAmExR 8PSINg221qOUfh/knugU64cmtd75Ra7c99I0CSs6SgYMIEERIf+4O7hdTwylKISi1mo+ex DnU19wvi3ccDRetkn9IfYpq5aojrRCevB/NolQw1BqmU/cIqrlNmoPY8G9MvzKI/JQ06JY kC8Q4mIpqmqwPlXaXJlNydh810ajyFsyEyeqbs979y+cDm5Y8v6NARB1fpIsTQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4gWS0T2Fyjz12pH for ; Thu, 04 Jun 2026 14:40:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3b263 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 04 Jun 2026 14:40:25 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dag-Erling=?utf-8?Q? Sm=C3=B8rg?=rav Subject: git: e4dd3505c2d1 - stable/15 - fts: Check link count before using it List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: e4dd3505c2d12f1c1a4e626b18f2d4e75273af85 Auto-Submitted: auto-generated Date: Thu, 04 Jun 2026 14:40:25 +0000 Message-Id: <6a218e59.3b263.43be2985@gitrepo.freebsd.org> The branch stable/15 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=e4dd3505c2d12f1c1a4e626b18f2d4e75273af85 commit e4dd3505c2d12f1c1a4e626b18f2d4e75273af85 Author: Dag-Erling Smørgrav AuthorDate: 2026-05-29 17:45:06 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2026-06-04 14:40:17 +0000 fts: Check link count before using it * Check the range of the link count before trying to use it. * Rewrite the comment explaining what the link count is used for. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D57324 (cherry picked from commit b2b95249ae0e24a6e24ad4286da56f1aff7a6db0) --- lib/libc/gen/fts.c | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/lib/libc/gen/fts.c b/lib/libc/gen/fts.c index 4aa386d777cd..e8063ecb646e 100644 --- a/lib/libc/gen/fts.c +++ b/lib/libc/gen/fts.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -114,6 +115,19 @@ static const char *ufslike_filesystems[] = { 0 }; +/* + * POSIX provides nlink_t but unfortunately not NLINK_MAX. + */ +#define NLINK_MAX \ + _Generic((nlink_t)0, \ + int16_t: INT16_MAX, \ + uint16_t: UINT16_MAX, \ + int32_t: INT32_MAX, \ + uint32_t: UINT32_MAX, \ + int64_t: INT64_MAX, \ + uint64_t: UINT64_MAX, \ + default: 0) + static FTS * __fts_open(FTS *sp, char * const *argv) { @@ -736,7 +750,7 @@ fts_build(FTS *sp, int type) int cderrno, descend, oflag, saved_errno, nostat, doadjust, readdir_errno; long level; - long nlinks; /* has to be signed because -1 is a magic value */ + int64_t nlinks; /* has to be signed because -1 is a magic value */ size_t dnamlen, len, maxlen, nitems; /* Set current node pointer. */ @@ -759,16 +773,36 @@ fts_build(FTS *sp, int type) } /* - * Nlinks is the number of possible entries of type directory in the - * directory if we're cheating on stat calls, 0 if we're not doing - * any stat calls at all, -1 if we're doing stats on everything. + * In the FTS_PHYSICAL | FTS_NOSTAT case, we want to avoid calling + * fstat() unnecessarily, but we still need to call it for + * subdirectories. The current directory's link count provides an + * upper bound on the number of subdirectories we may encounter + * (including . and .. in the FTS_SEEDOT case). We initialize + * nlinks to the current directory's link count, then decrement it + * every time we encounter a directory, so when we hit zero we can + * save some time by not calling fstat() on subsequent entries. + * + * If FTS_NOSTAT is not set, or the link count is less than two + * (which should not be possible) or equal to NLINK_MAX (which + * suggests that the actual value could be higher), or the current + * filesystem is not known to provide reliable link counts, we + * initialize nlinks to -1 and fstat() everything. + * + * In the rare case where we don't need to stat anything, even + * subdirectories, we initialize nlinks to 0 regardless of the + * actual link count. + * + * Note that we ignore the FTS_NOSTAT flag in the FTS_LOGICAL + * case, although we could choose to only stat symbolic links. + * Implementing this is left as an exercise for the reader. */ if (type == BNAMES) { nlinks = 0; /* Be quiet about nostat, GCC. */ nostat = 0; } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { - if (fts_ufslinks(sp, cur)) + if (cur->fts_nlink >= 2 && cur->fts_nlink < NLINK_MAX && + cur->fts_nlink <= INT64_MAX && fts_ufslinks(sp, cur)) nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); else nlinks = -1;