[libc-commits] [libc] [libc] Accept 't' in the fopen mode and reject unknown characters (PR #236968)
Mike Lothian via libc-commits
libc-commits at lists.llvm.org
Wed Nov 11 19:01:15 PST 2026
https://github.com/FireBurn updated https://github.com/llvm/llvm-project/pull/236968
>From c53ffcd77fbfee02910ad8b76c68695a7fb00b0f 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 1/2] [libc] Accept 't' in the fopen mode and reject unknown
characters
FileMode set the mode to zero on a character it did not know, but the
characters after it could set bits again, so "rt" failed while "rtb"
succeeded. Programs written for Windows pass "rt" for a text mode, which
is the default on Linux.
Accept 't' and ignore it, as 'e' is accepted although Linux specific, and
make an unknown character invalidate the mode wherever it is.
Assisted-by: Claude
---
libc/src/__support/File/file_mode.h | 12 +++-
.../src/__support/File/file_mode_test.cpp | 69 ++++++++++++++++++-
2 files changed, 77 insertions(+), 4 deletions(-)
diff --git a/libc/src/__support/File/file_mode.h b/libc/src/__support/File/file_mode.h
index 1c4c8b7b7e3986..600f80baefaafc 100644
--- a/libc/src/__support/File/file_mode.h
+++ b/libc/src/__support/File/file_mode.h
@@ -38,6 +38,7 @@ class FileMode {
// we will consider |mode| as incorrect and set the file's mode to zero
// meaning the file's mode is in an invalid state.;
int main_mode_count = 0;
+ bool unknown_character = false;
for (; *mode != '\0'; ++mode) {
switch (*mode) {
@@ -65,12 +66,19 @@ class FileMode {
case 'e':
file_mode_ |= static_cast<Mode>(Extension::CLOSE_ON_EXEC);
break;
+ case 't':
+ // Text mode is the default on Linux. On Windows it asks for newline
+ // translation, so programs written for it pass "rt".
+ break;
default:
- file_mode_ = 0;
+ unknown_character = true;
+ break;
}
}
- if (main_mode_count != 1)
+ // An unknown character makes the mode invalid wherever it is, and must not
+ // be undone by the characters that follow it.
+ if (unknown_character || main_mode_count != 1)
file_mode_ = 0;
}
diff --git a/libc/test/src/__support/File/file_mode_test.cpp b/libc/test/src/__support/File/file_mode_test.cpp
index 6048b49c8b27a5..c614fac38728d8 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,71 @@ TEST(LlvmLibcFileModeTest, CloseOnExecCombinations) {
EXPECT_TRUE(write_all_modifiers.write_allowed());
}
+TEST(LlvmLibcFileModeTest, TextFlagIsAccepted) {
+ 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());
+}
+
+TEST(LlvmLibcFileModeTest, TextFlagDoesNotDependOnOrder) {
+ 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, UnknownCharactersAreRejected) {
+ constexpr FileMode unknown_last("rz");
+ EXPECT_FALSE(unknown_last.is_valid());
+
+ constexpr FileMode unknown_in_the_middle("r?b");
+ EXPECT_FALSE(unknown_in_the_middle.is_valid());
+
+ // What follows an unknown character does not make the mode valid again.
+ constexpr FileMode unknown_then_binary("wzb");
+ EXPECT_FALSE(unknown_then_binary.is_valid());
+
+ constexpr FileMode unknown_then_cloexec("r?e");
+ EXPECT_FALSE(unknown_then_cloexec.is_valid());
+}
+
+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, TextFlagDoesNotAddAMainMode) {
+ 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");
>From e10bde51acb9f471a0419b75512ac374ae2405f8 Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Thu, 12 Nov 2026 03:01:03 +0000
Subject: [PATCH 2/2] [libc] Track the 't' flag in FileMode
FileMode records the flag and exposes it as is_text_format, rather than
ignoring it, as it does for 'b'. It still changes nothing on Linux.
Assisted-by: Claude
---
libc/src/__support/File/file_mode.h | 9 ++++++++-
libc/test/src/__support/File/file_mode_test.cpp | 10 ++++++++++
2 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/libc/src/__support/File/file_mode.h b/libc/src/__support/File/file_mode.h
index 600f80baefaafc..5d79b56ae442c6 100644
--- a/libc/src/__support/File/file_mode.h
+++ b/libc/src/__support/File/file_mode.h
@@ -69,6 +69,7 @@ class FileMode {
case 't':
// Text mode is the default on Linux. On Windows it asks for newline
// translation, so programs written for it pass "rt".
+ file_mode_ |= static_cast<Mode>(Extension::TEXT);
break;
default:
unknown_character = true;
@@ -123,6 +124,10 @@ class FileMode {
return (file_mode_ & static_cast<Mode>(CreateType::EXCLUSIVE)) != 0;
}
+ constexpr bool is_text_format() const {
+ return (file_mode_ & static_cast<Mode>(Extension::TEXT)) != 0;
+ }
+
constexpr bool is_close_on_exec() const {
return (file_mode_ & static_cast<Mode>(Extension::CLOSE_ON_EXEC)) != 0;
}
@@ -157,9 +162,11 @@ class FileMode {
EXCLUSIVE = 0x100,
};
- // POSIX extensions to the ISO C file modes.
+ // POSIX extensions to the ISO C file modes, and the 't' of the Windows
+ // runtime, which asks for text mode explicitly.
enum class Extension : Mode {
CLOSE_ON_EXEC = 0x1000,
+ TEXT = 0x2000,
};
// This property tracks the mode for the particular file instance (i.e
diff --git a/libc/test/src/__support/File/file_mode_test.cpp b/libc/test/src/__support/File/file_mode_test.cpp
index c614fac38728d8..ef2bc429694a5a 100644
--- a/libc/test/src/__support/File/file_mode_test.cpp
+++ b/libc/test/src/__support/File/file_mode_test.cpp
@@ -234,30 +234,40 @@ TEST(LlvmLibcFileModeTest, TextFlagIsAccepted) {
constexpr FileMode read_text("rt");
EXPECT_TRUE(read_text.is_valid());
EXPECT_TRUE(read_text.is_read());
+ EXPECT_TRUE(read_text.is_text_format());
EXPECT_FALSE(read_text.is_binary_format());
+ constexpr FileMode read_plain("r");
+ EXPECT_FALSE(read_plain.is_text_format());
+ EXPECT_FALSE(read_plain == read_text);
+
constexpr FileMode write_text("wt");
EXPECT_TRUE(write_text.is_valid());
EXPECT_TRUE(write_text.is_write());
+ EXPECT_TRUE(write_text.is_text_format());
constexpr FileMode append_text("at");
EXPECT_TRUE(append_text.is_valid());
EXPECT_TRUE(append_text.is_append());
+ EXPECT_TRUE(append_text.is_text_format());
constexpr FileMode update_text("r+t");
EXPECT_TRUE(update_text.is_valid());
EXPECT_TRUE(update_text.is_read());
EXPECT_TRUE(update_text.is_update());
+ EXPECT_TRUE(update_text.is_text_format());
}
TEST(LlvmLibcFileModeTest, TextFlagDoesNotDependOnOrder) {
constexpr FileMode text_then_binary("rtb");
EXPECT_TRUE(text_then_binary.is_valid());
EXPECT_TRUE(text_then_binary.is_binary_format());
+ EXPECT_TRUE(text_then_binary.is_text_format());
constexpr FileMode binary_then_text("rbt");
EXPECT_TRUE(binary_then_text.is_valid());
EXPECT_TRUE(binary_then_text.is_binary_format());
+ EXPECT_TRUE(binary_then_text.is_text_format());
constexpr FileMode text_with_cloexec("rte");
EXPECT_TRUE(text_with_cloexec.is_valid());
More information about the libc-commits
mailing list