[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
Sat Nov 7 06:45:12 PST 2026


https://github.com/FireBurn created https://github.com/llvm/llvm-project/pull/239980

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

>From 6332cb2b0a0821b98f485a00ec48b641334b4b92 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   | 3 ++-
 libc/src/stdio/generic/putc.cpp    | 3 ++-
 libc/src/stdio/generic/putchar.cpp | 3 ++-
 libc/test/src/stdio/fputc_test.cpp | 9 ++++++++-
 libc/test/src/stdio/putc_test.cpp  | 4 +++-
 5 files changed, 17 insertions(+), 5 deletions(-)

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));



More information about the libc-commits mailing list