[libc-commits] [libc] [libc] Return the written character from putc, fputc and putchar (PR #239980)
via libc-commits
libc-commits at lists.llvm.org
Sat Nov 7 06:45:49 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Mike Lothian (FireBurn)
<details>
<summary>Changes</summary>
All three returned zero on success. C requires the character written,
taken as an unsigned char and converted to int. A caller comparing the
result against the character it passed reads zero as a failed write.
m4's configure probes ftello by writing a byte after a read reaches end
of file and checking that putc returns it. It concluded LLVM-libc had a
Solaris bug it does not have, then failed to compile the workaround it
selected.
The existing putc test asserted the zero return, pinning the bug in
place. It now asserts what C requires, and the fputc test checks that
a byte above 0x7f is returned unsigned rather than sign extended.
Assisted-by: Claude
---
Full diff: https://github.com/llvm/llvm-project/pull/239980.diff
5 Files Affected:
- (modified) libc/src/stdio/generic/fputc.cpp (+2-1)
- (modified) libc/src/stdio/generic/putc.cpp (+2-1)
- (modified) libc/src/stdio/generic/putchar.cpp (+2-1)
- (modified) libc/test/src/stdio/fputc_test.cpp (+8-1)
- (modified) libc/test/src/stdio/putc_test.cpp (+3-1)
``````````diff
diff --git a/libc/src/stdio/generic/fputc.cpp b/libc/src/stdio/generic/fputc.cpp
index 6639f0687c87a1..6fc7af5de8cffa 100644
--- a/libc/src/stdio/generic/fputc.cpp
+++ b/libc/src/stdio/generic/fputc.cpp
@@ -28,7 +28,8 @@ LLVM_LIBC_FUNCTION(int, fputc, (int c, ::FILE *stream)) {
// The stream should be in an error state in this case.
return EOF;
}
- return 0;
+ // C requires the character written, as an unsigned char, not a success code.
+ return static_cast<int>(uc);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/generic/putc.cpp b/libc/src/stdio/generic/putc.cpp
index 83bc3d4131e762..bdfc071b7430a6 100644
--- a/libc/src/stdio/generic/putc.cpp
+++ b/libc/src/stdio/generic/putc.cpp
@@ -28,7 +28,8 @@ LLVM_LIBC_FUNCTION(int, putc, (int c, ::FILE *stream)) {
// The stream should be in an error state in this case.
return EOF;
}
- return 0;
+ // C requires the character written, as an unsigned char, not a success code.
+ return static_cast<int>(uc);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/generic/putchar.cpp b/libc/src/stdio/generic/putchar.cpp
index 33ab178372c239..8d2f33cf3672dd 100644
--- a/libc/src/stdio/generic/putchar.cpp
+++ b/libc/src/stdio/generic/putchar.cpp
@@ -30,7 +30,8 @@ LLVM_LIBC_FUNCTION(int, putchar, (int c)) {
// The stream should be in an error state in this case.
return EOF;
}
- return 0;
+ // C requires the character written, as an unsigned char, not a success code.
+ return static_cast<int>(uc);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/stdio/fputc_test.cpp b/libc/test/src/stdio/fputc_test.cpp
index a309dbaad4e65d..06fbdf88622bc5 100644
--- a/libc/test/src/stdio/fputc_test.cpp
+++ b/libc/test/src/stdio/fputc_test.cpp
@@ -21,7 +21,7 @@ TEST(LlvmLibcPutcTest, PrintOut) {
if (!c)
break;
result = LIBC_NAMESPACE::putchar(c);
- EXPECT_GE(result, 0);
+ EXPECT_EQ(result, static_cast<int>(static_cast<unsigned char>(c)));
}
constexpr char more[] = "A simple string written to stderr\n";
@@ -33,3 +33,10 @@ TEST(LlvmLibcPutcTest, PrintOut) {
EXPECT_GE(result, 0);
}
}
+
+TEST(LlvmLibcPutcTest, ReturnsTheByteUnsigned) {
+ // A byte above 0x7f comes back as an unsigned char, never sign extended.
+ EXPECT_EQ(LIBC_NAMESPACE::fputc(
+ 0xff, reinterpret_cast<FILE *>(LIBC_NAMESPACE::stderr)),
+ 0xff);
+}
diff --git a/libc/test/src/stdio/putc_test.cpp b/libc/test/src/stdio/putc_test.cpp
index e881a0e2d0108b..0bd9d050920716 100644
--- a/libc/test/src/stdio/putc_test.cpp
+++ b/libc/test/src/stdio/putc_test.cpp
@@ -22,7 +22,9 @@ TEST(LlvmLibcPutcTest, WriteToFile) {
constexpr char simple[] = "simple letters";
for (size_t i = 0; i < sizeof(simple); ++i) {
- ASSERT_EQ(LIBC_NAMESPACE::putc(simple[i], file), 0);
+ // C requires the character written, taken as an unsigned char.
+ ASSERT_EQ(LIBC_NAMESPACE::putc(simple[i], file),
+ static_cast<int>(static_cast<unsigned char>(simple[i])));
}
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
``````````
</details>
https://github.com/llvm/llvm-project/pull/239980
More information about the libc-commits
mailing list