Skip to content

Commit 995bb08

Browse files
committed
Add test for nullptr write error
1 parent 56d6968 commit 995bb08

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

libc/src/stdio/printf_core/core_structs.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,6 @@ template <typename T> LIBC_INLINE constexpr TypeDesc type_desc_from_type() {
144144

145145
// This is the value to be returned by conversions when no error has occurred.
146146
constexpr int WRITE_OK = 0;
147-
// These are the printf return values for when an error has occurred. They are
148-
// all negative, and should be distinct.
149-
// constexpr int FILE_WRITE_ERROR = -1;
150-
// constexpr int FILE_STATUS_ERROR = -2;
151-
// constexpr int NULLPTR_WRITE_ERROR = -3;
152-
// constexpr int INT_CONVERSION_ERROR = -4;
153-
// constexpr int FIXED_POINT_CONVERSION_ERROR = -5;
154-
// constexpr int ALLOCATION_ERROR = -6;
155147
} // namespace printf_core
156148
} // namespace LIBC_NAMESPACE_DECL
157149

libc/test/src/stdio/fprintf_test.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,20 @@ TEST(LlvmLibcFPrintfTest, CharsWrittenOverflow) {
112112
ASSERT_ERRNO_EQ(EOVERFLOW);
113113

114114
EXPECT_EQ(printf_test::fclose(file), 0);
115-
}
115+
}
116+
117+
#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
118+
TEST(LlvmLibcFPrintfTest, NullPtrCheck) {
119+
const char *FILENAME = APPEND_LIBC_TEST("fprintf_nullptr.test");
120+
auto FILE_PATH = libc_make_test_file_path(FILENAME);
121+
122+
::FILE *file = printf_test::fopen(FILE_PATH, "w");
123+
ASSERT_FALSE(file == nullptr);
124+
125+
int ret = LIBC_NAMESPACE::fprintf(file, "hello %n", (int *)nullptr);
126+
EXPECT_LT(ret, 0);
127+
ASSERT_ERRNO_EQ(EINVAL);
128+
129+
ASSERT_EQ(printf_test::fclose(file), 0);
130+
}
131+
#endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS

0 commit comments

Comments
 (0)