Date: Thu, 05 Feb 2026 15:27:33 +0000 From: Dag-Erling=?utf-8?Q? Sm=C3=B8rg?=rav <des@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: 77dedead8539 - stable/14 - libc: Improve POSIX conformance of dirfd() Message-ID: <6984b6e5.3fcba.488124be@gitrepo.freebsd.org>
index | next in thread | raw e-mail
The branch stable/14 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=77dedead8539c76d1f452c2c0073ee13ed7cf54a commit 77dedead8539c76d1f452c2c0073ee13ed7cf54a Author: Dag-Erling Smørgrav <des@FreeBSD.org> AuthorDate: 2026-02-02 15:46:57 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> CommitDate: 2026-02-05 14:48:36 +0000 libc: Improve POSIX conformance of dirfd() POSIX states that dirfd() should set errno to EINVAL and return -1 if dirp does not refer to a valid directory stream. Our interpretation is that this applies if dirp is null or the file descriptor associated with it is negative. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D55025 (cherry picked from commit 5074d5c9845e142883cdbb9ad212be66e57615d0) libc: Fix missing include Although not needed on FreeBSD due to namespace pollution, we should technically #include <stddef.h> to secure a definition of NULL. Fixes: 5074d5c9845e ("libc: Improve POSIX conformance of dirfd()") (cherry picked from commit 1c00d5a3b234ef937d848956027e9de5ea8010f9) --- lib/libc/gen/directory.3 | 14 +++++++++++++- lib/libc/gen/dirfd.c | 6 ++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/libc/gen/directory.3 b/lib/libc/gen/directory.3 index 3a121dcafb8c..ee78202ddc3e 100644 --- a/lib/libc/gen/directory.3 +++ b/lib/libc/gen/directory.3 @@ -258,7 +258,9 @@ The function returns 0 on success and -1 on failure. The .Fn fdclosedir -function returns an open file descriptor on success and -1 on failure. +and +.Fn dirfd +functions return an open file descriptor on success and -1 on failure. .Sh ERRORS The .Fn opendir @@ -329,6 +331,16 @@ function may also fail and set .Va errno for any of the errors specified for the routine .Xr close 2 . +.Pp +The +.Fn dirfd +function will fail if: +.Bl -tag -width Er +.It Bq Er EINVAL +The +.Fa dirp +argument does not refer to a valid directory stream. +.El .Sh SEE ALSO .Xr close 2 , .Xr lseek 2 , diff --git a/lib/libc/gen/dirfd.c b/lib/libc/gen/dirfd.c index ba8f52845b4e..984e69b9ae9a 100644 --- a/lib/libc/gen/dirfd.c +++ b/lib/libc/gen/dirfd.c @@ -28,6 +28,8 @@ #include "namespace.h" #include <dirent.h> +#include <errno.h> +#include <stddef.h> #include "un-namespace.h" #include "gen-private.h" @@ -35,5 +37,9 @@ int dirfd(DIR *dirp) { + if (dirp == NULL || _dirfd(dirp) < 0) { + errno = EINVAL; + return (-1); + } return (_dirfd(dirp)); }home | help
Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?6984b6e5.3fcba.488124be>
