[libc-commits] [libc] 33e199f - [libc] Return the written character from putc, fputc and putchar (#239980)

via libc-commits libc-commits at lists.llvm.org
Sun Nov 8 19:13:57 PST 2026


Author: Mike Lothian
Date: 2026-11-08T19:13:52-08:00
New Revision: 33e199f13ced6d221f1b6c15b658babe8576549f

URL: https://github.com/llvm/llvm-project/commit/33e199f13ced6d221f1b6c15b658babe8576549f
DIFF: https://github.com/llvm/llvm-project/commit/33e199f13ced6d221f1b6c15b658babe8576549f.diff

LOG: [libc] Return the written character from putc, fputc and putchar (#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.

Assisted-by: Claude

Added: 
    

Modified: 
    libc/src/stdio/generic/fputc.cpp
    libc/src/stdio/generic/putc.cpp
    libc/src/stdio/generic/putchar.cpp
    libc/test/src/stdio/fputc_test.cpp
    libc/test/src/stdio/putc_test.cpp

Removed: 
    


################################################################################
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