[libc-commits] [libc] 38bd595 - [libc] Accept 't' in the fopen mode and reject unknown characters (#236968)
via libc-commits
libc-commits at lists.llvm.org
Wed Nov 18 17:56:25 PST 2026
Author: Mike Lothian
Date: 2026-11-18T17:56:20-08:00
New Revision: 38bd595f05c18c193adfafb4e258bf26b71265dc
URL: https://github.com/llvm/llvm-project/commit/38bd595f05c18c193adfafb4e258bf26b71265dc
DIFF: https://github.com/llvm/llvm-project/commit/38bd595f05c18c193adfafb4e258bf26b71265dc.diff
LOG: [libc] Accept 't' in the fopen mode and reject unknown characters (#236968)
`fopen(path, "rt")` failed, because FileMode treated any character it
did not know as an invalid mode. 't' is the text mode flag of the
Windows C runtime and is the default on Linux, so programs written for
Windows pass it.
This accepts 't' and ignores it, as 'e' is accepted although it is Linux
specific. It also fixes the parser being order dependent: an unknown
character cleared the mode, but a later known one set bits again, so
"rt" failed while "rtb" succeeded. An unknown character now makes the
mode invalid wherever it is.
AI disclosure: this patch was written with Claude Code. I reviewed the
change. The new FileMode tests cover 't' in every position, unknown
characters in the middle and at the end, and the order dependence.
Assisted-by: Claude
Added:
Modified:
libc/src/__support/File/file_mode.h
libc/test/src/__support/File/file_mode_test.cpp
Removed:
################################################################################
diff --git a/libc/src/__support/File/file_mode.h b/libc/src/__support/File/file_mode.h
index 1c4c8b7b7e3986..5d79b56ae442c6 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,20 @@ 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".
+ file_mode_ |= static_cast<Mode>(Extension::TEXT);
+ 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;
}
@@ -115,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;
}
@@ -149,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 6048b49c8b27a5..ef2bc429694a5a 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,81 @@ 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_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());
+ 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");
More information about the libc-commits
mailing list