[libc-commits] [libc] [libc] Ignore unknown characters after the fopen mode (PR #236968)

Mike Lothian via libc-commits libc-commits at lists.llvm.org
Fri Nov 6 02:54:44 PST 2026


https://github.com/FireBurn updated https://github.com/llvm/llvm-project/pull/236968

>From 4edd22a6c96ebb7c8f5300fe01173242b92ce4be Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Sat, 10 Oct 2026 04:32:40 +0100
Subject: [PATCH] [libc] Ignore unknown characters after the fopen mode

FileMode treated any character it did not know as an invalid mode, and
the characters after it could undo that, so "rt" failed while "rtb"
succeeded. glibc and musl ignore the characters they do not know after
the first, which must still be r, w or a, and programs written against
them pass "rt" for a text mode.

Assisted-by: Claude
---
 libc/src/__support/File/file_mode.h           |  4 +-
 .../src/__support/File/file_mode_test.cpp     | 61 ++++++++++++++++++-
 2 files changed, 62 insertions(+), 3 deletions(-)

diff --git a/libc/src/__support/File/file_mode.h b/libc/src/__support/File/file_mode.h
index 1c4c8b7b7e3986..1630456995a99b 100644
--- a/libc/src/__support/File/file_mode.h
+++ b/libc/src/__support/File/file_mode.h
@@ -66,7 +66,9 @@ class FileMode {
         file_mode_ |= static_cast<Mode>(Extension::CLOSE_ON_EXEC);
         break;
       default:
-        file_mode_ = 0;
+        // C leaves a mode string with other characters undefined. glibc and
+        // musl ignore them, and programs rely on that for the 't' in "rt".
+        break;
       }
     }
 
diff --git a/libc/test/src/__support/File/file_mode_test.cpp b/libc/test/src/__support/File/file_mode_test.cpp
index 6048b49c8b27a5..38e44657046741 100644
--- a/libc/test/src/__support/File/file_mode_test.cpp
+++ b/libc/test/src/__support/File/file_mode_test.cpp
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 ///
-/// file_mode_test.cpp
-/// This file contains possible test cases for FileMode class.
+/// \file
+/// Test cases for the FileMode class.
 ///
 //===----------------------------------------------------------------------===//
 
@@ -230,6 +230,63 @@ TEST(LlvmLibcFileModeTest, CloseOnExecCombinations) {
   EXPECT_TRUE(write_all_modifiers.write_allowed());
 }
 
+TEST(LlvmLibcFileModeTest, UnknownCharactersAreIgnored) {
+  constexpr FileMode read_text("rt");
+  EXPECT_TRUE(read_text.is_valid());
+  EXPECT_TRUE(read_text.is_read());
+  EXPECT_FALSE(read_text.is_binary_format());
+
+  constexpr FileMode write_text("wt");
+  EXPECT_TRUE(write_text.is_valid());
+  EXPECT_TRUE(write_text.is_write());
+
+  constexpr FileMode append_text("at");
+  EXPECT_TRUE(append_text.is_valid());
+  EXPECT_TRUE(append_text.is_append());
+
+  constexpr FileMode update_text("r+t");
+  EXPECT_TRUE(update_text.is_valid());
+  EXPECT_TRUE(update_text.is_read());
+  EXPECT_TRUE(update_text.is_update());
+
+  constexpr FileMode write_unknown("wzb");
+  EXPECT_TRUE(write_unknown.is_valid());
+  EXPECT_TRUE(write_unknown.is_write());
+  EXPECT_TRUE(write_unknown.is_binary_format());
+}
+
+TEST(LlvmLibcFileModeTest, UnknownCharactersDoNotDependOnOrder) {
+  // A character that is not known must not undo what the characters after it
+  // set, so "rtb" and "rbt" agree.
+  constexpr FileMode text_then_binary("rtb");
+  EXPECT_TRUE(text_then_binary.is_valid());
+  EXPECT_TRUE(text_then_binary.is_binary_format());
+
+  constexpr FileMode binary_then_text("rbt");
+  EXPECT_TRUE(binary_then_text.is_valid());
+  EXPECT_TRUE(binary_then_text.is_binary_format());
+
+  constexpr FileMode text_with_cloexec("rte");
+  EXPECT_TRUE(text_with_cloexec.is_valid());
+  EXPECT_TRUE(text_with_cloexec.is_close_on_exec());
+}
+
+TEST(LlvmLibcFileModeTest, FirstCharacterMustBeAMainMode) {
+  constexpr FileMode text_first("tr");
+  EXPECT_FALSE(text_first.is_valid());
+
+  constexpr FileMode exclusive_first("xw");
+  EXPECT_FALSE(exclusive_first.is_valid());
+
+  constexpr FileMode empty("");
+  EXPECT_FALSE(empty.is_valid());
+}
+
+TEST(LlvmLibcFileModeTest, UnknownCharactersDoNotAddAMainMode) {
+  constexpr FileMode two_main_modes("rtw");
+  EXPECT_FALSE(two_main_modes.is_valid());
+}
+
 TEST(LlvmLibcFileModeTest, InvalidCombinations) {
   // 1. Read and Write as main modes
   constexpr FileMode read_and_write("rw");



More information about the libc-commits mailing list