[libc-commits] [libc] d828ef6 - [libc] Fix fileno() to return EBADF for non-Linux File streams (#227760)
via libc-commits
libc-commits at lists.llvm.org
Wed Nov 18 15:46:48 PST 2026
Author: Linisha
Date: 2026-11-18T15:46:43-08:00
New Revision: d828ef6b859175a4c910978e4a74862ecba20179
URL: https://github.com/llvm/llvm-project/commit/d828ef6b859175a4c910978e4a74862ecba20179
DIFF: https://github.com/llvm/llvm-project/commit/d828ef6b859175a4c910978e4a74862ecba20179.diff
LOG: [libc] Fix fileno() to return EBADF for non-Linux File streams (#227760)
## Summary
`fileno()` currently assumes every `FILE*` is backed by a `LinuxFile`
and unconditionally casts to read its file descriptor. For streams
created by `fopencookie()` or `fmemopen()` (backed by
`CookieFile`/`MemoryFile`, neither of which has a file descriptor
member), this reads unrelated memory and returns a bogus "descriptor"
instead of following POSIX, which requires `fileno()` to fail with
`EBADF` when "the stream is not associated with a file."
## Fix
`File` has no RTTI/type tag by design (some `LinuxFile` instances, e.g.
`stdin`/`stdout`/`stderr`, are `constexpr` globals). Instead of adding
one, this adds `is_linux_file()`, which positively identifies a
`LinuxFile` by checking that all four of its registered I/O callbacks
(`read`/`write`/`seek`/`close`) are the known `linux_file_*` functions —
every `LinuxFile` always registers exactly these, and no other `File`
subclass does. `get_fileno()` now checks this first and returns `-1`
with `errno = EBADF` for anything else. `CookieFile`/`MemoryFile` are
untouched — no fake file descriptors added, per the issue's request.
## Testing
Added `FilenoFails` to `fmemopen_test.cpp` and `fopencookie_test.cpp`,
asserting `fileno()` returns `-1`/`EBADF` for those stream types.
Verified the new logic compiles cleanly against the real
headers/toolchain and behaves correctly (a genuine `LinuxFile` still
returns its real fd; a non-`LinuxFile` stand-in correctly gets
`-1`/`EBADF`) via a standalone harness linked against the actual
compiled objects — this environment couldn't complete a full
`LLVM_LIBC_FULL_BUILD=ON` configure (hit memory limits during CMake's
generate step). CI's `libc-fullbuild-tests` workflow should run the
added tests directly on the PR.
Fixes #223550.
This issue is investigated by help of ai.
Added:
Modified:
libc/src/__support/File/file.h
libc/src/__support/File/linux/file.cpp
libc/src/__support/File/linux/file.h
libc/src/stdio/generic/CMakeLists.txt
libc/src/stdio/generic/fileno.cpp
libc/test/src/stdio/CMakeLists.txt
libc/test/src/stdio/fmemopen_test.cpp
libc/test/src/stdio/fopencookie_test.cpp
Removed:
################################################################################
diff --git a/libc/src/__support/File/file.h b/libc/src/__support/File/file.h
index 99ad58ffaeb292..19b83e0e2fe10c 100644
--- a/libc/src/__support/File/file.h
+++ b/libc/src/__support/File/file.h
@@ -61,6 +61,15 @@ class File {
using SeekFunc = ErrorOr<off_t>(File *, off_t, int);
using CloseFunc = int(File *);
+ // Exposes the platform I/O callbacks so a platform backend can positively
+ // identify which concrete File subclass an object is (e.g. distinguishing
+ // a LinuxFile from a CookieFile/MemoryFile) without needing RTTI. See
+ // is_linux_file() in the Linux backend for the concrete use.
+ LIBC_INLINE ReadFunc *get_read_func() const { return platform_read; }
+ LIBC_INLINE WriteFunc *get_write_func() const { return platform_write; }
+ LIBC_INLINE SeekFunc *get_seek_func() const { return platform_seek; }
+ LIBC_INLINE CloseFunc *get_close_func() const { return platform_close; }
+
// This is a convenience RAII class to lock and unlock file objects.
class FileLock {
File *file;
@@ -380,7 +389,7 @@ int reopenfile_unlocked(File *f, const char *path, const char *mode);
// The platform_file library should implement it if it relevant for that
// platform.
-int get_fileno(File *f);
+ErrorOr<int> get_fileno(File *f);
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/File/linux/file.cpp b/libc/src/__support/File/linux/file.cpp
index 1c0510edbf3fa3..8e5f189559c71c 100644
--- a/libc/src/__support/File/linux/file.cpp
+++ b/libc/src/__support/File/linux/file.cpp
@@ -296,9 +296,26 @@ int LinuxFile::reopen_unlocked(const char *path, const char *mode) {
return 0;
}
-int get_fileno(File *f) {
+bool is_linux_file(const File *f) {
+ // Any LinuxFile (including subtypes like the popen stream, which only swaps
+ // the close callback) registers at least one of these linux_file_* callbacks,
+ // and no non-Linux File (CookieFile/MemoryFile) uses any of them. So a match
+ // on any single callback positively identifies a LinuxFile that has an fd.
+ return f->get_read_func() == &linux_file_read ||
+ f->get_write_func() == &linux_file_write ||
+ f->get_seek_func() == &linux_file_seek ||
+ f->get_close_func() == &linux_file_close;
+}
+
+ErrorOr<int> get_fileno(File *f) {
+ if (!is_linux_file(f))
+ return Error(EBADF);
auto *lf = reinterpret_cast<LinuxFile *>(f);
- return lf->get_fd();
+ int fd = lf->get_fd();
+ // freopen can fail and leave an otherwise-normal LinuxFile with fd == -1.
+ if (fd == -1)
+ return Error(EBADF);
+ return fd;
}
int reopenfile(File *f, const char *path, const char *mode) {
diff --git a/libc/src/__support/File/linux/file.h b/libc/src/__support/File/linux/file.h
index 41fc54fac0ebb9..97a675eaf9625b 100644
--- a/libc/src/__support/File/linux/file.h
+++ b/libc/src/__support/File/linux/file.h
@@ -32,6 +32,12 @@ FileIOResult linux_file_read(File *, void *, size_t);
ErrorOr<off_t> linux_file_seek(File *, off_t, int);
int linux_file_close(File *);
+// Returns true iff |f| is actually a LinuxFile, as opposed to some other
+// File subclass (e.g. CookieFile, MemoryFile) that doesn't have a real
+// file descriptor. Used because File has no RTTI/type tag: every LinuxFile
+// always registers these exact four callbacks, and no other subclass does.
+bool is_linux_file(const File *f);
+
class LinuxFile : public File {
int fd;
diff --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt
index 52f34bc1744dd1..78d24e7dd75566 100644
--- a/libc/src/stdio/generic/CMakeLists.txt
+++ b/libc/src/stdio/generic/CMakeLists.txt
@@ -92,6 +92,7 @@ add_generic_entrypoint_object(
libc.hdr.types.FILE
libc.src.__support.File.file
libc.src.__support.File.platform_file
+ libc.src.__support.libc_errno
)
# since this isn't an entrypoint we need to do these checks manually.
diff --git a/libc/src/stdio/generic/fileno.cpp b/libc/src/stdio/generic/fileno.cpp
index 8fa73d17e1fe10..44b6368af80c6d 100644
--- a/libc/src/stdio/generic/fileno.cpp
+++ b/libc/src/stdio/generic/fileno.cpp
@@ -10,12 +10,18 @@
#include "hdr/types/FILE.h"
#include "src/__support/File/file.h"
+#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, fileno, (::FILE * stream)) {
- return get_fileno(reinterpret_cast<LIBC_NAMESPACE::File *>(stream));
+ auto result = get_fileno(reinterpret_cast<LIBC_NAMESPACE::File *>(stream));
+ if (!result.has_value()) {
+ libc_errno = result.error();
+ return -1;
+ }
+ return result.value();
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index 0c199d8726a4ca..5852f635904c0c 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -159,6 +159,7 @@ add_libc_test(
libc.src.stdio.ferror
libc.src.stdio.fflush
libc.src.stdio.fgetc
+ libc.src.stdio.fileno
libc.src.stdio.fmemopen
libc.src.stdio.fread
libc.src.stdio.fseek
@@ -188,6 +189,7 @@ add_libc_test(
libc.src.stdio.feof
libc.src.stdio.ferror
libc.src.stdio.fflush
+ libc.src.stdio.fileno
libc.src.stdio.fopencookie
libc.src.stdio.fread
libc.src.stdio.fseek
diff --git a/libc/test/src/stdio/fmemopen_test.cpp b/libc/test/src/stdio/fmemopen_test.cpp
index 1736580914a453..344a1f2d6c7bc4 100644
--- a/libc/test/src/stdio/fmemopen_test.cpp
+++ b/libc/test/src/stdio/fmemopen_test.cpp
@@ -21,6 +21,7 @@
#include "src/stdio/ferror.h"
#include "src/stdio/fflush.h"
#include "src/stdio/fgetc.h"
+#include "src/stdio/fileno.h"
#include "src/stdio/fmemopen.h"
#include "src/stdio/fread.h"
#include "src/stdio/fseek.h"
@@ -246,6 +247,18 @@ TEST_F(LlvmLibcFMemOpenTest, InternalBuffer) {
}
}
+TEST_F(LlvmLibcFMemOpenTest, FilenoFails) {
+ char storage[8];
+ ::FILE *f = LIBC_NAMESPACE::fmemopen(storage, sizeof(storage), "r+");
+ ASSERT_TRUE(f != nullptr);
+ scope_exit close([&] { EXPECT_EQ(0, LIBC_NAMESPACE::fclose(f)); });
+
+ // A memory stream has no underlying file descriptor: POSIX requires
+ // fileno() to fail with EBADF rather than return a bogus value derived
+ // from unrelated internal state.
+ ASSERT_THAT(LIBC_NAMESPACE::fileno(f), Fails(EBADF));
+}
+
TEST_F(LlvmLibcFMemOpenTest, ZeroCapacity) {
char guard = 'X';
void *buffers[] = {&guard, nullptr};
diff --git a/libc/test/src/stdio/fopencookie_test.cpp b/libc/test/src/stdio/fopencookie_test.cpp
index 3133847eb9a324..b4cfd42b07c078 100644
--- a/libc/test/src/stdio/fopencookie_test.cpp
+++ b/libc/test/src/stdio/fopencookie_test.cpp
@@ -16,6 +16,7 @@
#include "src/stdio/feof.h"
#include "src/stdio/ferror.h"
#include "src/stdio/fflush.h"
+#include "src/stdio/fileno.h"
#include "src/stdio/fopencookie.h"
#include "src/stdio/fread.h"
#include "src/stdio/fseek.h"
@@ -255,6 +256,23 @@ TEST_F(LlvmLibcFOpenCookieTest, WriteUpdateCookieTest) {
free(ss);
}
+TEST_F(LlvmLibcFOpenCookieTest, FilenoFails) {
+ auto *ss = reinterpret_cast<StringStream *>(malloc(sizeof(StringStream)));
+ ss->buf = nullptr;
+ ss->bufsize = ss->offset = ss->endpos = 0;
+
+ ::FILE *f = LIBC_NAMESPACE::fopencookie(ss, "r", STRING_STREAM_FUNCS);
+ ASSERT_TRUE(f != nullptr);
+
+ // A cookie stream has no underlying file descriptor: POSIX requires
+ // fileno() to fail with EBADF.
+ ASSERT_EQ(LIBC_NAMESPACE::fileno(f), -1);
+ ASSERT_ERRNO_EQ(EBADF);
+
+ ASSERT_EQ(0, LIBC_NAMESPACE::fclose(f));
+ free(ss);
+}
+
ssize_t failing_read(void *, char *, size_t) { return -1; }
ssize_t failing_write(void *, const char *, size_t) { return -1; }
More information about the libc-commits
mailing list