From nobody Fri May 29 17:45:18 2026 X-Original-To: dev-commits-src-main@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 4gRrNZ6436z6fstM for ; Fri, 29 May 2026 17:45:18 +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 4gRrNZ518hz3kRK for ; Fri, 29 May 2026 17:45:18 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1780076718; 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=O4wFSCGKBeMfSHMIEvVBWpmRowXOWubmSm9oMbpEMzs=; b=cmmBZSHhGCddYUeeI+F7/+ydg+eBjFJOzYOx0/jRtKKVHZ3ELwxGOxalISu7VW1kzMNhB3 1CWbyykOCu2TepxPOa4hdt/MCApRX2o+ulnoUb+f/EB3Vn/AvQY2l+HHHCiJuaaD9A4DTZ UKWym0fq3egfA0BcjEFCKT2hoU5NXSxcNMjei+/k/LIRHeqAvAQbTifu+wlKrpya+H5TaI oivw1T6qOj8v93aQ5MYybvyz3yQFZ1HDbCiaW4nWZ44VXj++o2muVDK/yxRls4wMX7Dh08 ckXcPaLudriA6bRLFbXJ4zpu5bPG1UvWyYbZHzTAZms//d6Jj/zDLRSWJwnDqA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1780076718; a=rsa-sha256; cv=none; b=iEo2oNArriDTWri4A1Jl+4XS8Makh9zThcCFdnpX29YMtfu3sahkietJvyAqoVIdWMBaRf htv8FvxSSKxzow0V6ZTg0RCh2EHd1CaFTasS9QzhAK9e3cnSKqSCvMYsxPqVlrlGYnk97B XLpbXYCbyD5a7q9tGORaU/wopUfjNx6nkJ6tKr9yT0yEbJHnwSuOixHD9mU4dwV4JLHHu9 oOhlGwgGsFjRO9rJZ5fOMDM6uueMFjzVvC9QJ/aaI2GlISF2B43pTDtQg2D1aILreoH8JB V95cyqcWphiteKIg8fubpO/HD0Qdh3f0Yx0zCh+sBZi/zwKAtT2Wf6hJn65OGg== 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=1780076718; 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=O4wFSCGKBeMfSHMIEvVBWpmRowXOWubmSm9oMbpEMzs=; b=yVfTlAzbMjxEc8N2qaFLVOnqbH0GvBielqbEfNdZnu/we49o+DFW9lLwpdUCcslHtxUHxr BfXwB49bkskGgFEhJJhDlbdx+CfKhkKWAJAULa/k3MioTuIFbA1He+ISqTJ5Qj8dnvcw9J uLN709I4/ON4hLVy6AHe8jRFWHhlJ75geNY8rJr7kX/W2Dggqoyr0N43Mt6f581fpsOXOE IAgYlBIeH2YD6u45cVWeK526a6Ks4yYp6N6D/gMUBSvzD2D1tpGHN2czx9YU8XwKnnSVeA RnzBJSnYDlmwYCQen5hS40an14jD+bTlhJKdxfFs44pL6m/fxIM3tRL8C+AgbA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4gRrNZ3v6szkFH for ; Fri, 29 May 2026 17:45:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3fbb7 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 29 May 2026 17:45:18 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling=?utf-8?Q? Sm=C3=B8rg?=rav Subject: git: b2b95249ae0e - main - fts: Check link count before using it List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: b2b95249ae0e24a6e24ad4286da56f1aff7a6db0 Auto-Submitted: auto-generated Date: Fri, 29 May 2026 17:45:18 +0000 Message-Id: <6a19d0ae.3fbb7.5b206217@gitrepo.freebsd.org> The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=b2b95249ae0e24a6e24ad4286da56f1aff7a6db0 commit b2b95249ae0e24a6e24ad4286da56f1aff7a6db0 Author: Dag-Erling Smørgrav AuthorDate: 2026-05-29 17:45:06 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2026-05-29 17:45:06 +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 --- 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;