From nobody Fri Jun 20 16:14:25 2025 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 4bP2c14kJhz5yDVn; Fri, 20 Jun 2025 16:14: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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bP2c13xY8z3ScL; Fri, 20 Jun 2025 16:14:25 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1750436065; 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=u1V42BJBvzUSOzHfmyzM2mk2qMCzYHAucWNLbQSDzNk=; b=UmEG/698SlSHBRDKeC1DIn6rH73Dae0JuKerC5QjhGZlSVQPfFT4IFT8b7S9d8pCMgX5lV 9U6DEHBqhw+3icVR7cUdKRcuBkhLoU/hFAe1yR8wnkIcTe4dNDNKCJib6DaSw9piS/SQiU K1EpYhjELt3OMfDHAL9Dqg8OMqU7kIcokeD/IaI7IxdrZLgCSAcj64A7cl0Mo77VgeDSDM GOb7Vr/I+f8x1PJT4g1dudjLtvRx8xzQNlIMdRKidDr4NDxTnn4seaEF63mArRVHMbXVtU xHdmSaOKNFv0aIVo3xOQ/5VAJRM/fbxyKzxESeXp0Lukp65GuqufsCy+jqV/oQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1750436065; 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=u1V42BJBvzUSOzHfmyzM2mk2qMCzYHAucWNLbQSDzNk=; b=sRmeh7u1id3OTcSbjlnICib0KfuaOB5CXOV6xEafxWdwBGHXDqm87tA7KrzQT9IF5uNYCc oqFSxjq9tgwKHhro5XxHS3KOGrWDMkZbWwjpZo6cQDQOZLj41QHLxCjXU9QJy/x7cAtq2e xodmR31DV10+8T9gOg31Q5heQyKTWjGfFHiE6HrySirPEBlTx1OwuHCwG2n6gASqb81EnY 9VtM1pJbYkgpl2Zxy2zlW9FMD+fYt6iTdZmlfVvvnDo6InZQy5dEMuzJHHSnl1HDDllH8S Zegt8hHX3a1YG5frKfRUjxp/TVf9ZwI1Sh7xxwj6CHK/kok2XH3ALyWVvwnNRw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1750436065; a=rsa-sha256; cv=none; b=vYMD4xTqodj4IMCEROxSeOyfK8SHOFwsN7RdzlyPl1NRKZ/d0/5+NDZINzSrmrexZMftVv jWiSJ5OTm/PAFN3EzoyS6YxDDcalnDiDXUQVjg9JarfIZIATOH33cDut06dmh4BJAHQ6W4 BS5LYj+E5gCqc/PhoJNM6KXnWCyyqg3FLCdSE1JawqdUXJZ/rchVazwMCFzjTLI7QQ7/G6 lvQqoBezpyLLbrzbtRh2rTQZKST2oqK2zJleQafg7uJ3CzcQ4LniAWkykiqWIghe+L20cq TcvakKfKwtZNsCo+ZTBoTHdsh2NPLi0j0gFC5rSvF5Bh8DGTNSMOGhK4PqgSRg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4bP2c13TdFzsj2; Fri, 20 Jun 2025 16:14:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 55KGEPIQ023860; Fri, 20 Jun 2025 16:14:25 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 55KGEPdn023857; Fri, 20 Jun 2025 16:14:25 GMT (envelope-from git) Date: Fri, 20 Jun 2025 16:14:25 GMT Message-Id: <202506201614.55KGEPdn023857@gitrepo.freebsd.org> 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=B8rgrav?= Subject: git: c187b673bb79 - main - scandir: Fix behavior when no entries match. 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 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: c187b673bb79905b5d5db1eb34d5a893c4464eeb Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=c187b673bb79905b5d5db1eb34d5a893c4464eeb commit c187b673bb79905b5d5db1eb34d5a893c4464eeb Author: Dag-Erling Smørgrav AuthorDate: 2025-06-20 16:13:48 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2025-06-20 16:13:48 +0000 scandir: Fix behavior when no entries match. In the previous commit, I removed the initial initialization of the `names` array, not realizing that `scandir()` is expected to return a non-null (but empty) array of entries if no entries matched. Restore the historical behavior, document it, and add a test. Fixes: deeebfdecab5 Sponsored by: Klara, Inc. Reviewed by: kevans, allanjude, markj Differential Revision: https://reviews.freebsd.org/D50949 --- lib/libc/gen/scandir.3 | 5 +++-- lib/libc/gen/scandir.c | 6 +++++- lib/libc/tests/gen/scandir_test.c | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/libc/gen/scandir.3 b/lib/libc/gen/scandir.3 index f74bd1f23613..21df9316d511 100644 --- a/lib/libc/gen/scandir.3 +++ b/lib/libc/gen/scandir.3 @@ -25,7 +25,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd June 19, 2025 +.Dd June 20, 2025 .Dt SCANDIR 3 .Os .Sh NAME @@ -102,7 +102,8 @@ entries using It returns the number of entries in the array. A pointer to the array of directory entries is stored in the location referenced by -.Fa namelist . +.Fa namelist +(even if no entries were selected). .Pp The .Fa select diff --git a/lib/libc/gen/scandir.c b/lib/libc/gen/scandir.c index 172937392ddc..d8033a985862 100644 --- a/lib/libc/gen/scandir.c +++ b/lib/libc/gen/scandir.c @@ -74,9 +74,13 @@ scandir_dirp(DIR *dirp, struct dirent ***namelist, #endif { struct dirent *d, *p = NULL, **names = NULL, **names2; - size_t arraysz = 0, numitems = 0; + size_t arraysz = 32, numitems = 0; int serrno; + names = malloc(arraysz * sizeof(*names)); + if (names == NULL) + return (-1); + while ((d = readdir(dirp)) != NULL) { if (select != NULL && !SELECT(d)) continue; /* just selected names */ diff --git a/lib/libc/tests/gen/scandir_test.c b/lib/libc/tests/gen/scandir_test.c index 54848c0572ca..dcd73fe94df5 100644 --- a/lib/libc/tests/gen/scandir_test.c +++ b/lib/libc/tests/gen/scandir_test.c @@ -103,10 +103,32 @@ ATF_TC_BODY(scandirat_test, tc) ATF_REQUIRE_EQ(0, close(fd)); } +static int +scandir_none(const struct dirent *ent __unused) +{ + return (0); +} + +ATF_TC(scandir_none); +ATF_TC_HEAD(scandir_none, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test scandir() when no entries are selected"); +} +ATF_TC_BODY(scandir_none, tc) +{ + struct dirent **namelist = NULL; + + ATF_REQUIRE_EQ(0, scandir(".", &namelist, scandir_none, alphasort)); + ATF_REQUIRE(namelist); + free(namelist); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, scandir_test); ATF_TP_ADD_TC(tp, fscandir_test); ATF_TP_ADD_TC(tp, scandirat_test); + ATF_TP_ADD_TC(tp, scandir_none); return (atf_no_error()); }