Skip to content

Commit 4b58abe

Browse files
committed
Fix tests
1 parent 69e9362 commit 4b58abe

File tree

5 files changed

+44
-26
lines changed

5 files changed

+44
-26
lines changed

libc/src/stdio/printf_core/core_structs.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ template <typename T> LIBC_INLINE constexpr TypeDesc type_desc_from_type() {
134134

135135
// This is the value to be returned by conversions when no error has occurred.
136136
constexpr int WRITE_OK = 0;
137-
// These are the printf return values for when an error has occurred. They are
138-
// all negative, and should be distinct.
137+
// These are the internal error return values used by the printf engine when an
138+
// error has occurred. They are all large negative, distinct values starting
139+
// from -1000 to not overlap with system errors.
139140
constexpr int FILE_WRITE_ERROR = -1001;
140141
constexpr int FILE_STATUS_ERROR = -1002;
141142
constexpr int NULLPTR_WRITE_ERROR = -1003;
@@ -144,18 +145,22 @@ constexpr int FIXED_POINT_CONVERSION_ERROR = -1005;
144145
constexpr int ALLOCATION_ERROR = -1006;
145146
constexpr int SHORT_WRITE_ERROR = -1007;
146147

147-
LIBC_INLINE static int internal_error_to_errno(int internal_errno) {
148-
if (internal_errno < 1001) {
149-
return internal_errno;
148+
LIBC_INLINE static int internal_error_to_errno(int internal_error) {
149+
// System error occured, return error as is.
150+
if (internal_error < 1001) {
151+
return internal_error;
150152
}
151153

152-
switch (-internal_errno) {
154+
// Map internal error to errno.
155+
switch (-internal_error) {
153156
case WRITE_OK:
154157
return 0;
155158
case FILE_WRITE_ERROR:
156159
return EIO;
157160
case FILE_STATUS_ERROR:
158161
return EIO;
162+
case SHORT_WRITE_ERROR:
163+
return EIO;
159164
case NULLPTR_WRITE_ERROR:
160165
return EINVAL;
161166
case INT_CONVERSION_ERROR:
@@ -164,8 +169,6 @@ LIBC_INLINE static int internal_error_to_errno(int internal_errno) {
164169
return EINVAL;
165170
case ALLOCATION_ERROR:
166171
return ENOMEM;
167-
case SHORT_WRITE_ERROR:
168-
return EIO;
169172
default:
170173
LIBC_ASSERT(
171174
false &&

libc/src/stdio/printf_core/vfprintf_internal.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ LIBC_INLINE void funlockfile(::FILE *f) { ::funlockfile(f); }
5151

5252
LIBC_INLINE FileIOResult fwrite_unlocked(const void *ptr, size_t size,
5353
size_t nmemb, ::FILE *f) {
54-
return {::fwrite_unlocked(ptr, size, nmemb, f), 0}; // todo err
54+
return {::fwrite_unlocked(ptr, size, nmemb, f), errno};
5555
}
5656
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
5757
} // namespace internal
@@ -68,15 +68,14 @@ LIBC_INLINE int file_write_hook(cpp::string_view new_str, void *fp) {
6868
if (write_result.has_error())
6969
return -write_result.error;
7070

71-
// On short write no system error is returned, so return custom error.
72-
if (write_result.value != new_str.size())
73-
return SHORT_WRITE_ERROR;
74-
7571
// Return custom error in case error wasn't set on FileIOResult for some
7672
// reason (like using LIBC_COPT_STDIO_USE_SYSTEM_FILE)
77-
if (internal::ferror_unlocked(target_file)) {
73+
if (internal::ferror_unlocked(target_file))
7874
return FILE_WRITE_ERROR;
79-
}
75+
76+
// On short write no system error is returned, so return custom error.
77+
if (write_result.value != new_str.size())
78+
return SHORT_WRITE_ERROR;
8079

8180
return WRITE_OK;
8281
}

libc/test/src/stdio/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,12 @@ if(LLVM_LIBC_FULL_BUILD)
169169
libc.src.stdio.ferror
170170
libc.src.stdio.fopen
171171
libc.src.stdio.fread
172-
libc.src.stdio.fopencookie
173172
)
173+
if(${LIBC_TARGET_OS} STREQUAL "linux")
174+
list(APPEND fprintf_test_deps
175+
libc.src.stdio.fopencookie
176+
)
177+
endif()
174178
# This is to be used for tests which write to libc's platform streams
175179
# under full build but write to system-lib's streams otherwise.
176180
set(hermetic_test_only HERMETIC_TEST_ONLY)

libc/test/src/stdio/fprintf_test.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "src/stdio/fclose.h"
1111
#include "src/stdio/ferror.h"
1212
#include "src/stdio/fopen.h"
13-
#include "src/stdio/fopencookie.h"
1413
#include "src/stdio/fread.h"
1514
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
1615

@@ -21,18 +20,21 @@
2120
#include "test/UnitTest/ErrnoSetterMatcher.h"
2221
#include "test/UnitTest/Test.h"
2322

23+
#if defined(LIBC_TARGET_OS_IS_LINUX) && \
24+
!defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
25+
#include "src/stdio/fopencookie.h"
26+
#endif
27+
2428
namespace printf_test {
2529
#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
2630
using LIBC_NAMESPACE::fclose;
2731
using LIBC_NAMESPACE::ferror;
2832
using LIBC_NAMESPACE::fopen;
29-
using LIBC_NAMESPACE::fopencookie;
3033
using LIBC_NAMESPACE::fread;
3134
#else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
3235
using ::fclose;
3336
using ::ferror;
3437
using ::fopen;
35-
using ::fopencookie;
3638
using ::fread;
3739
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
3840
} // namespace printf_test
@@ -86,11 +88,13 @@ TEST(LlvmLibcFPrintfTest, WriteToFile) {
8688
written =
8789
LIBC_NAMESPACE::fprintf(file, "Writing to a read only file should fail.");
8890
EXPECT_LT(written, 0);
89-
ASSERT_ERRNO_EQ(EIO);
91+
ASSERT_ERRNO_EQ(EBADF);
9092

9193
ASSERT_EQ(printf_test::fclose(file), 0);
9294
}
9395

96+
#if !defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE) && \
97+
defined(LIBC_TARGET_OS_IS_LINUX)
9498
TEST(LlvmLibcFPrintfTest, CharsWrittenOverflow) {
9599
struct NoopStream {};
96100
auto noop_write = [](void *, const char *, size_t size) -> ssize_t {
@@ -99,7 +103,7 @@ TEST(LlvmLibcFPrintfTest, CharsWrittenOverflow) {
99103

100104
NoopStream stream;
101105
cookie_io_functions_t funcs = {nullptr, +noop_write, nullptr, nullptr};
102-
::FILE *file = printf_test::fopencookie(&stream, "w", funcs);
106+
::FILE *file = LIBC_NAMESPACE::fopencookie(&stream, "w", funcs);
103107
ASSERT_NE(file, nullptr);
104108

105109
// Trigger an overflow in the return value of fprintf by writing more than
@@ -113,6 +117,8 @@ TEST(LlvmLibcFPrintfTest, CharsWrittenOverflow) {
113117

114118
EXPECT_EQ(printf_test::fclose(file), 0);
115119
}
120+
#endif // #if !defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE) &&
121+
// defined(LIBC_TARGET_OS_IS_LINUX)
116122

117123
#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
118124
TEST(LlvmLibcFPrintfTest, NullPtrCheck) {

libc/test/src/stdio/vfprintf_test.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "src/stdio/fclose.h"
1515
#include "src/stdio/ferror.h"
1616
#include "src/stdio/fopen.h"
17-
#include "src/stdio/fopencookie.h"
1817
#include "src/stdio/fread.h"
1918
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
2019

@@ -24,18 +23,21 @@
2423
#include "test/UnitTest/ErrnoSetterMatcher.h"
2524
#include "test/UnitTest/Test.h"
2625

26+
#if defined(LIBC_TARGET_OS_IS_LINUX) && \
27+
!defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
28+
#include "src/stdio/fopencookie.h"
29+
#endif
30+
2731
namespace printf_test {
2832
#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
2933
using LIBC_NAMESPACE::fclose;
3034
using LIBC_NAMESPACE::ferror;
3135
using LIBC_NAMESPACE::fopen;
32-
using LIBC_NAMESPACE::fopencookie;
3336
using LIBC_NAMESPACE::fread;
3437
#else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
3538
using ::fclose;
3639
using ::ferror;
3740
using ::fopen;
38-
using ::fopencookie;
3941
using ::fread;
4042
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
4143
} // namespace printf_test
@@ -97,11 +99,13 @@ TEST(LlvmLibcVFPrintfTest, WriteToFile) {
9799

98100
written = call_vfprintf(file, "Writing to a read only file should fail.");
99101
EXPECT_LT(written, 0);
100-
ASSERT_ERRNO_EQ(EIO);
102+
ASSERT_ERRNO_EQ(EBADF);
101103

102104
ASSERT_EQ(printf_test::fclose(file), 0);
103105
}
104106

107+
#if !defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE) && \
108+
defined(LIBC_TARGET_OS_IS_LINUX)
105109
TEST(LlvmLibcVFPrintfTest, CharsWrittenOverflow) {
106110
struct NoopStream {};
107111
auto noop_write = [](void *, const char *, size_t size) -> ssize_t {
@@ -110,7 +114,7 @@ TEST(LlvmLibcVFPrintfTest, CharsWrittenOverflow) {
110114

111115
NoopStream stream;
112116
cookie_io_functions_t funcs = {nullptr, +noop_write, nullptr, nullptr};
113-
::FILE *file = printf_test::fopencookie(&stream, "w", funcs);
117+
::FILE *file = LIBC_NAMESPACE::fopencookie(&stream, "w", funcs);
114118
ASSERT_NE(file, nullptr);
115119

116120
// Trigger an overflow in the return value of vfprintf by writing more than
@@ -124,3 +128,5 @@ TEST(LlvmLibcVFPrintfTest, CharsWrittenOverflow) {
124128

125129
EXPECT_EQ(printf_test::fclose(file), 0);
126130
}
131+
#endif // !defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE) &&
132+
// defined(LIBC_TARGET_OS_IS_LINUX)

0 commit comments

Comments
 (0)