[libc-commits] [libc] [libc] Fix fileno() to return EBADF for non-Linux File streams (PR #227760)

via libc-commits libc-commits at lists.llvm.org
Sat Nov 7 03:57:12 PST 2026


https://github.com/linisha15 updated https://github.com/llvm/llvm-project/pull/227760

>From 881985a4ded06ba9371d4b857ee83fd1a5c1ea65 Mon Sep 17 00:00:00 2001
From: linisha <linisha232 at gmail.com>
Date: Wed, 30 Sep 2026 21:13:10 +0530
Subject: [PATCH 1/2] [libc] Fix fileno() to return EBADF for non-Linux File
 streams

---
 libc/src/__support/File/file.h           |  9 +++++++++
 libc/src/__support/File/linux/file.cpp   | 11 +++++++++++
 libc/src/__support/File/linux/file.h     |  6 ++++++
 libc/test/src/stdio/CMakeLists.txt       |  2 ++
 libc/test/src/stdio/fmemopen_test.cpp    | 13 +++++++++++++
 libc/test/src/stdio/fopencookie_test.cpp | 18 ++++++++++++++++++
 6 files changed, 59 insertions(+)

diff --git a/libc/src/__support/File/file.h b/libc/src/__support/File/file.h
index 84dde0560f467f..71a5813ee3d5c8 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.
+  ReadFunc *get_read_func() const { return platform_read; }
+  WriteFunc *get_write_func() const { return platform_write; }
+  SeekFunc *get_seek_func() const { return platform_seek; }
+  CloseFunc *get_close_func() const { return platform_close; }
+
   // This is a convenience RAII class to lock and unlock file objects.
   class FileLock {
     File *file;
diff --git a/libc/src/__support/File/linux/file.cpp b/libc/src/__support/File/linux/file.cpp
index cc6a1fcbd31688..b0a3828a5d55a0 100644
--- a/libc/src/__support/File/linux/file.cpp
+++ b/libc/src/__support/File/linux/file.cpp
@@ -280,7 +280,18 @@ int LinuxFile::reopen_unlocked(const char *path, const char *mode) {
   return 0;
 }
 
+bool is_linux_file(const File *f) {
+  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;
+}
+
 int get_fileno(File *f) {
+  if (!is_linux_file(f)) {
+    libc_errno = EBADF;
+    return -1;
+  }
   auto *lf = reinterpret_cast<LinuxFile *>(f);
   return lf->get_fd();
 }
diff --git a/libc/src/__support/File/linux/file.h b/libc/src/__support/File/linux/file.h
index 539957b8b22c8c..d7aaa3a83dc202 100644
--- a/libc/src/__support/File/linux/file.h
+++ b/libc/src/__support/File/linux/file.h
@@ -18,6 +18,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/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index 28b07cd984e9ea..2f00bdc8ad9dbc 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -161,6 +161,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
@@ -189,6 +190,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 4c6dadd7ff70a4..676fa77f95015c 100644
--- a/libc/test/src/stdio/fopencookie_test.cpp
+++ b/libc/test/src/stdio/fopencookie_test.cpp
@@ -11,6 +11,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"
@@ -247,3 +248,20 @@ TEST_F(LlvmLibcFOpenCookieTest, WriteUpdateCookieTest) {
   ASSERT_EQ(LIBC_NAMESPACE::fclose(f), 0);
   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);
+}

>From 9b34f5bb0cdf7c1d0297dae58ab93251893a7188 Mon Sep 17 00:00:00 2001
From: linisha <linisha232 at gmail.com>
Date: Sat, 7 Nov 2026 17:14:08 +0530
Subject: [PATCH 2/2] [libc] Address review: get_fileno returns ErrorOr, handle
 fd==-1 and popen

---
 libc/src/__support/File/file.h         | 10 +++++-----
 libc/src/__support/File/linux/file.cpp | 24 +++++++++++++++---------
 libc/src/stdio/generic/CMakeLists.txt  |  1 +
 libc/src/stdio/generic/fileno.cpp      |  8 +++++++-
 4 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/libc/src/__support/File/file.h b/libc/src/__support/File/file.h
index 71a5813ee3d5c8..1a83616a2477e9 100644
--- a/libc/src/__support/File/file.h
+++ b/libc/src/__support/File/file.h
@@ -65,10 +65,10 @@ class File {
   // 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.
-  ReadFunc *get_read_func() const { return platform_read; }
-  WriteFunc *get_write_func() const { return platform_write; }
-  SeekFunc *get_seek_func() const { return platform_seek; }
-  CloseFunc *get_close_func() const { return platform_close; }
+  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 {
@@ -350,7 +350,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 b0a3828a5d55a0..f8205e2edc4509 100644
--- a/libc/src/__support/File/linux/file.cpp
+++ b/libc/src/__support/File/linux/file.cpp
@@ -281,19 +281,25 @@ int LinuxFile::reopen_unlocked(const char *path, const char *mode) {
 }
 
 bool is_linux_file(const File *f) {
-  return f->get_read_func() == &linux_file_read &&
-         f->get_write_func() == &linux_file_write &&
-         f->get_seek_func() == &linux_file_seek &&
+  // 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;
 }
 
-int get_fileno(File *f) {
-  if (!is_linux_file(f)) {
-    libc_errno = EBADF;
-    return -1;
-  }
+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/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt
index 5f45d15ac8f26f..9e8bf17898b5ea 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
 )
 
 add_generic_entrypoint_object(
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



More information about the libc-commits mailing list