[libc-commits] [libc] [libc] Add printf error handling (PR #162876)

Marcell Leleszi via libc-commits libc-commits at lists.llvm.org
Thu Oct 23 04:16:51 PDT 2025


================
@@ -42,13 +42,24 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
       buffer, BUFF_SIZE, &stdout_write_hook, nullptr);
   printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb);
 
-  int retval = printf_core::printf_main(&writer, format, args);
+  auto retval = printf_core::printf_main(&writer, format, args);
+  if (!retval.has_value()) {
+    libc_errno = retval.error(); // TODO map
+    return -1;
+  }
 
   int flushval = wb.overflow_write("");
-  if (flushval != printf_core::WRITE_OK)
-    retval = flushval;
+  if (flushval != printf_core::WRITE_OK) {
+    libc_errno = -flushval; // TODO map
+    return -1;
+  }
 
-  return retval;
+  if (retval.value() > cpp::numeric_limits<int>::max()) {
+    libc_errno = EOVERFLOW;
----------------
mleleszi wrote:

Got it, mapped it to ERANGE for baremetal

https://github.com/llvm/llvm-project/pull/162876


More information about the libc-commits mailing list