[libc-commits] [libc] [libc] Return the written character from putc, fputc and putchar (PR #239980)
Mike Lothian via libc-commits
libc-commits at lists.llvm.org
Sun Nov 8 18:49:41 PST 2026
https://github.com/FireBurn updated https://github.com/llvm/llvm-project/pull/239980
>From 1238a0b5f0f7e7e374a0cfb8acce5b75bbe90d32 Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Fri, 4 Sep 2026 14:20:09 +0100
Subject: [PATCH] [libc] Return the written character from putc, fputc and
putchar
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
---
libc/src/stdio/generic/fputc.cpp | 2 +-
libc/src/stdio/generic/putc.cpp | 2 +-
libc/src/stdio/generic/putchar.cpp | 2 +-
libc/test/src/stdio/fputc_test.cpp | 9 ++++++++-
libc/test/src/stdio/putc_test.cpp | 3 ++-
5 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/libc/src/stdio/generic/fputc.cpp b/libc/src/stdio/generic/fputc.cpp
index 6639f0687c87a1..f6525dc5a57bd9 100644
--- a/libc/src/stdio/generic/fputc.cpp
+++ b/libc/src/stdio/generic/fputc.cpp
@@ -28,7 +28,7 @@ LLVM_LIBC_FUNCTION(int, fputc, (int c, ::FILE *stream)) {
// The stream should be in an error state in this case.
return EOF;
}
- return 0;
+ 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..b68542ff2eac1e 100644
--- a/libc/src/stdio/generic/putc.cpp
+++ b/libc/src/stdio/generic/putc.cpp
@@ -28,7 +28,7 @@ LLVM_LIBC_FUNCTION(int, putc, (int c, ::FILE *stream)) {
// The stream should be in an error state in this case.
return EOF;
}
- return 0;
+ 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..fe2e348e738158 100644
--- a/libc/src/stdio/generic/putchar.cpp
+++ b/libc/src/stdio/generic/putchar.cpp
@@ -30,7 +30,7 @@ LLVM_LIBC_FUNCTION(int, putchar, (int c)) {
// The stream should be in an error state in this case.
return EOF;
}
- return 0;
+ 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..da6dda09a55818 100644
--- a/libc/test/src/stdio/putc_test.cpp
+++ b/libc/test/src/stdio/putc_test.cpp
@@ -22,7 +22,8 @@ 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);
+ EXPECT_EQ(LIBC_NAMESPACE::putc(simple[i], file),
+ static_cast<int>(static_cast<unsigned char>(simple[i])));
}
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
More information about the libc-commits
mailing list