[libc-commits] [libc] [libc] Add printf error handling (PR #162876)
via libc-commits
libc-commits at lists.llvm.org
Fri Oct 10 10:17:08 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Marcell Leleszi (mleleszi)
<details>
<summary>Changes</summary>
[#<!-- -->159474](https://github.com/llvm/llvm-project/issues/159474)
- All printf variants set errno and consistently return -1 on error, instead of returning various predefined error codes
- Return value overflow handling is added
Internal changes:
- Writer tracks number of chars written as size_t
- negative errno values are propagated out of Writer instead of predefined error codes
---
Patch is 42.23 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/162876.diff
30 Files Affected:
- (modified) libc/src/stdio/asprintf.cpp (+11-2)
- (modified) libc/src/stdio/baremetal/printf.cpp (+15-4)
- (modified) libc/src/stdio/baremetal/vprintf.cpp (+15-4)
- (modified) libc/src/stdio/generic/fprintf.cpp (+11-2)
- (modified) libc/src/stdio/generic/printf.cpp (+11-2)
- (modified) libc/src/stdio/generic/vfprintf.cpp (+11-2)
- (modified) libc/src/stdio/generic/vprintf.cpp (+11-2)
- (modified) libc/src/stdio/printf_core/core_structs.h (+12-8)
- (modified) libc/src/stdio/printf_core/fixed_converter.h (+2-1)
- (modified) libc/src/stdio/printf_core/int_converter.h (+2-1)
- (modified) libc/src/stdio/printf_core/printf_main.h (+3-4)
- (modified) libc/src/stdio/printf_core/vasprintf_internal.h (+11-9)
- (modified) libc/src/stdio/printf_core/vfprintf_internal.h (+11-6)
- (modified) libc/src/stdio/printf_core/write_int_converter.h (+4-3)
- (modified) libc/src/stdio/printf_core/writer.h (+4-4)
- (modified) libc/src/stdio/snprintf.cpp (+12-2)
- (modified) libc/src/stdio/sprintf.cpp (+12-2)
- (modified) libc/src/stdio/vasprintf.cpp (+10-1)
- (modified) libc/src/stdio/vsnprintf.cpp (+12-2)
- (modified) libc/src/stdio/vsprintf.cpp (+11-2)
- (modified) libc/src/stdlib/strfromd.cpp (+5-1)
- (modified) libc/src/stdlib/strfromf.cpp (+5-1)
- (modified) libc/src/stdlib/strfroml.cpp (+5-1)
- (modified) libc/src/time/strftime_core/strftime_main.h (+2-1)
- (modified) libc/test/src/stdio/CMakeLists.txt (+3)
- (modified) libc/test/src/stdio/fprintf_test.cpp (+48)
- (modified) libc/test/src/stdio/printf_core/converter_test.cpp (+15-15)
- (modified) libc/test/src/stdio/printf_core/writer_test.cpp (+16-16)
- (modified) libc/test/src/stdio/vfprintf_test.cpp (+31)
- (modified) libc/test/src/stdlib/StrfromTest.h (+18-2)
``````````diff
diff --git a/libc/src/stdio/asprintf.cpp b/libc/src/stdio/asprintf.cpp
index f8cfb74ce48ea..13c1ee68ef0f2 100644
--- a/libc/src/stdio/asprintf.cpp
+++ b/libc/src/stdio/asprintf.cpp
@@ -22,8 +22,17 @@ LLVM_LIBC_FUNCTION(int, asprintf,
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
- int ret = printf_core::vasprintf_internal(buffer, format, args);
- return ret;
+ auto ret_val = printf_core::vasprintf_internal(buffer, format, args);
+ if (ret_val.has_error()) {
+ libc_errno = ret_val.error;
+ return -1;
+ }
+ if (ret_val.value > cpp::numeric_limits<int>::max()) {
+ libc_errno = EOVERFLOW;
+ return -1;
+ }
+
+ return static_cast<int>(ret_val.value);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/baremetal/printf.cpp b/libc/src/stdio/baremetal/printf.cpp
index 7253c6549a4e4..a2e308ed54a57 100644
--- a/libc/src/stdio/baremetal/printf.cpp
+++ b/libc/src/stdio/baremetal/printf.cpp
@@ -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_error()) {
+ libc_errno = retval.error;
+ return -1;
+ }
int flushval = wb.overflow_write("");
- if (flushval != printf_core::WRITE_OK)
- retval = flushval;
+ if (flushval != printf_core::WRITE_OK) {
+ libc_errno = -flushval;
+ return -1;
+ }
- return retval;
+ if (retval.value > cpp::numeric_limits<int>::max()) {
+ libc_errno = EOVERFLOW;
+ return -1;
+ }
+
+ return static_cast<int>(retval.value);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/baremetal/vprintf.cpp b/libc/src/stdio/baremetal/vprintf.cpp
index ab02533f14911..003d7d70707fb 100644
--- a/libc/src/stdio/baremetal/vprintf.cpp
+++ b/libc/src/stdio/baremetal/vprintf.cpp
@@ -40,13 +40,24 @@ LLVM_LIBC_FUNCTION(int, vprintf,
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_error()) {
+ libc_errno = retval.error;
+ return -1;
+ }
int flushval = wb.overflow_write("");
- if (flushval != printf_core::WRITE_OK)
- retval = flushval;
+ if (flushval != printf_core::WRITE_OK) {
+ libc_errno = -flushval;
+ return -1;
+ }
- return retval;
+ if (retval.value > cpp::numeric_limits<int>::max()) {
+ libc_errno = EOVERFLOW;
+ return -1;
+ }
+
+ return static_cast<int>(retval.value);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/generic/fprintf.cpp b/libc/src/stdio/generic/fprintf.cpp
index 087aeadfc52c5..632264f944f44 100644
--- a/libc/src/stdio/generic/fprintf.cpp
+++ b/libc/src/stdio/generic/fprintf.cpp
@@ -27,8 +27,17 @@ LLVM_LIBC_FUNCTION(int, fprintf,
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
- int ret_val = printf_core::vfprintf_internal(stream, format, args);
- return ret_val;
+ auto ret_val = printf_core::vfprintf_internal(stream, format, args);
+ if (ret_val.has_error()) {
+ libc_errno = ret_val.error;
+ return -1;
+ }
+ if (ret_val.value > cpp::numeric_limits<int>::max()) {
+ libc_errno = EOVERFLOW;
+ return -1;
+ }
+
+ return static_cast<int>(ret_val.value);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/generic/printf.cpp b/libc/src/stdio/generic/printf.cpp
index bb7c7c86f843f..482a65cd5a2fa 100644
--- a/libc/src/stdio/generic/printf.cpp
+++ b/libc/src/stdio/generic/printf.cpp
@@ -31,9 +31,18 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
- int ret_val = printf_core::vfprintf_internal(
+ auto ret_val = printf_core::vfprintf_internal(
reinterpret_cast<::FILE *>(PRINTF_STDOUT), format, args);
- return ret_val;
+ if (ret_val.has_error()) {
+ libc_errno = ret_val.error;
+ return -1;
+ }
+ if (ret_val.value > cpp::numeric_limits<int>::max()) {
+ libc_errno = EOVERFLOW;
+ return -1;
+ }
+
+ return static_cast<int>(ret_val.value);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/generic/vfprintf.cpp b/libc/src/stdio/generic/vfprintf.cpp
index 01f4265f118a6..6b39011123084 100644
--- a/libc/src/stdio/generic/vfprintf.cpp
+++ b/libc/src/stdio/generic/vfprintf.cpp
@@ -24,8 +24,17 @@ LLVM_LIBC_FUNCTION(int, vfprintf,
internal::ArgList args(vlist); // This holder class allows for easier copying
// and pointer semantics, as well as handling
// destruction automatically.
- int ret_val = printf_core::vfprintf_internal(stream, format, args);
- return ret_val;
+ auto ret_val = printf_core::vfprintf_internal(stream, format, args);
+ if (ret_val.has_error()) {
+ libc_errno = ret_val.error;
+ return -1;
+ }
+ if (ret_val.value > cpp::numeric_limits<int>::max()) {
+ libc_errno = EOVERFLOW;
+ return -1;
+ }
+
+ return static_cast<int>(ret_val.value);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/generic/vprintf.cpp b/libc/src/stdio/generic/vprintf.cpp
index 08d71515646ed..09ba2ce0d32a9 100644
--- a/libc/src/stdio/generic/vprintf.cpp
+++ b/libc/src/stdio/generic/vprintf.cpp
@@ -29,9 +29,18 @@ LLVM_LIBC_FUNCTION(int, vprintf,
internal::ArgList args(vlist); // This holder class allows for easier copying
// and pointer semantics, as well as handling
// destruction automatically.
- int ret_val = printf_core::vfprintf_internal(
+ auto ret_val = printf_core::vfprintf_internal(
reinterpret_cast<::FILE *>(PRINTF_STDOUT), format, args);
- return ret_val;
+ if (ret_val.has_error()) {
+ libc_errno = ret_val.error;
+ return -1;
+ }
+ if (ret_val.value > cpp::numeric_limits<int>::max()) {
+ libc_errno = EOVERFLOW;
+ return -1;
+ }
+
+ return static_cast<int>(ret_val.value);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/printf_core/core_structs.h b/libc/src/stdio/printf_core/core_structs.h
index e27f77b6b594a..3cb76ed32776f 100644
--- a/libc/src/stdio/printf_core/core_structs.h
+++ b/libc/src/stdio/printf_core/core_structs.h
@@ -22,6 +22,18 @@
namespace LIBC_NAMESPACE_DECL {
namespace printf_core {
+struct PrintfResult {
+ size_t value;
+ int error;
+
+ constexpr PrintfResult(size_t val) : value(val), error(0) {}
+ constexpr PrintfResult(size_t val, int error) : value(val), error(error) {}
+
+ constexpr bool has_error() { return error != 0; }
+
+ constexpr operator size_t() { return value; }
+};
+
// These length modifiers match the length modifiers in the format string, which
// is why they are formatted differently from the rest of the file.
enum class LengthModifier { hh, h, l, ll, j, z, t, L, w, wf, none };
@@ -132,14 +144,6 @@ template <typename T> LIBC_INLINE constexpr TypeDesc type_desc_from_type() {
// This is the value to be returned by conversions when no error has occurred.
constexpr int WRITE_OK = 0;
-// These are the printf return values for when an error has occurred. They are
-// all negative, and should be distinct.
-constexpr int FILE_WRITE_ERROR = -1;
-constexpr int FILE_STATUS_ERROR = -2;
-constexpr int NULLPTR_WRITE_ERROR = -3;
-constexpr int INT_CONVERSION_ERROR = -4;
-constexpr int FIXED_POINT_CONVERSION_ERROR = -5;
-constexpr int ALLOCATION_ERROR = -6;
} // namespace printf_core
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/printf_core/fixed_converter.h b/libc/src/stdio/printf_core/fixed_converter.h
index e8a3314967562..77384b1891174 100644
--- a/libc/src/stdio/printf_core/fixed_converter.h
+++ b/libc/src/stdio/printf_core/fixed_converter.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FIXED_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FIXED_CONVERTER_H
+#include "hdr/errno_macros.h"
#include "include/llvm-libc-macros/stdfix-macros.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/ctype_utils.h"
@@ -59,7 +60,7 @@ LIBC_INLINE constexpr uint32_t const_ten_exp(uint32_t exponent) {
READ_FX_BITS(unsigned LENGTH_MODIFIER accum); \
} else { \
LIBC_ASSERT(false && "Invalid conversion name passed to convert_fixed"); \
- return FIXED_POINT_CONVERSION_ERROR; \
+ return -EINVAL; \
} \
} while (false)
diff --git a/libc/src/stdio/printf_core/int_converter.h b/libc/src/stdio/printf_core/int_converter.h
index 11234c32ce997..554436c9091a8 100644
--- a/libc/src/stdio/printf_core/int_converter.h
+++ b/libc/src/stdio/printf_core/int_converter.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_INT_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_INT_CONVERTER_H
+#include "hdr/errno_macros.h"
#include "src/__support/CPP/span.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/ctype_utils.h"
@@ -91,7 +92,7 @@ LIBC_INLINE int convert_int(Writer<write_mode> *writer,
cpp::array<char, details::num_buf_size()> buf;
auto str = details::num_to_strview(num, buf, to_conv.conv_name);
if (!str)
- return INT_CONVERSION_ERROR;
+ return -ERANGE;
size_t digits_written = str->size();
diff --git a/libc/src/stdio/printf_core/printf_main.h b/libc/src/stdio/printf_core/printf_main.h
index 57f29858d5298..f187b43a9cd8f 100644
--- a/libc/src/stdio/printf_core/printf_main.h
+++ b/libc/src/stdio/printf_core/printf_main.h
@@ -22,8 +22,8 @@ namespace LIBC_NAMESPACE_DECL {
namespace printf_core {
template <WriteMode write_mode>
-int printf_main(Writer<write_mode> *writer, const char *__restrict str,
- internal::ArgList &args) {
+PrintfResult printf_main(Writer<write_mode> *writer, const char *__restrict str,
+ internal::ArgList &args) {
Parser<internal::ArgList> parser(str, args);
int result = 0;
for (FormatSection cur_section = parser.get_next_section();
@@ -33,9 +33,8 @@ int printf_main(Writer<write_mode> *writer, const char *__restrict str,
result = convert(writer, cur_section);
else
result = writer->write(cur_section.raw_string);
-
if (result < 0)
- return result;
+ return {0, -result};
}
return writer->get_chars_written();
diff --git a/libc/src/stdio/printf_core/vasprintf_internal.h b/libc/src/stdio/printf_core/vasprintf_internal.h
index 283d8df2810fb..f84f60bec2fc6 100644
--- a/libc/src/stdio/printf_core/vasprintf_internal.h
+++ b/libc/src/stdio/printf_core/vasprintf_internal.h
@@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
+#include "hdr/errno_macros.h"
#include "hdr/func/free.h"
#include "hdr/func/malloc.h"
#include "hdr/func/realloc.h"
@@ -29,7 +30,7 @@ LIBC_INLINE int resize_overflow_hook(cpp::string_view new_str, void *target) {
if (new_buff == nullptr) {
if (wb->buff != wb->init_buff)
free(wb->buff);
- return printf_core::ALLOCATION_ERROR;
+ return -ENOMEM;
}
if (isBuffOnStack)
inline_memcpy(new_buff, wb->buff, wb->buff_cur);
@@ -42,27 +43,28 @@ LIBC_INLINE int resize_overflow_hook(cpp::string_view new_str, void *target) {
constexpr size_t DEFAULT_BUFFER_SIZE = 200;
-LIBC_INLINE int vasprintf_internal(char **ret, const char *__restrict format,
- internal::ArgList args) {
+LIBC_INLINE PrintfResult vasprintf_internal(char **ret,
+ const char *__restrict format,
+ internal::ArgList args) {
char init_buff_on_stack[DEFAULT_BUFFER_SIZE];
printf_core::WriteBuffer<Mode<WriteMode::RESIZE_AND_FILL_BUFF>::value> wb(
init_buff_on_stack, DEFAULT_BUFFER_SIZE, resize_overflow_hook);
printf_core::Writer writer(wb);
auto ret_val = printf_core::printf_main(&writer, format, args);
- if (ret_val < 0) {
+ if (ret_val.has_error()) {
*ret = nullptr;
- return -1;
+ return ret_val;
}
if (wb.buff == init_buff_on_stack) {
- *ret = static_cast<char *>(malloc(ret_val + 1));
+ *ret = static_cast<char *>(malloc(ret_val.value + 1));
if (ret == nullptr)
- return printf_core::ALLOCATION_ERROR;
- inline_memcpy(*ret, wb.buff, ret_val);
+ return {0, ENOMEM};
+ inline_memcpy(*ret, wb.buff, ret_val.value);
} else {
*ret = wb.buff;
}
- (*ret)[ret_val] = '\0';
+ (*ret)[ret_val.value] = '\0';
return ret_val;
}
} // namespace printf_core
diff --git a/libc/src/stdio/printf_core/vfprintf_internal.h b/libc/src/stdio/printf_core/vfprintf_internal.h
index 630de9d9d43dd..c50cacf2709a7 100644
--- a/libc/src/stdio/printf_core/vfprintf_internal.h
+++ b/libc/src/stdio/printf_core/vfprintf_internal.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_VFPRINTF_INTERNAL_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_VFPRINTF_INTERNAL_H
+#include "hdr/errno_macros.h"
#include "src/__support/File/file.h"
#include "src/__support/arg_list.h"
#include "src/__support/macros/attributes.h" // For LIBC_INLINE
@@ -63,23 +64,27 @@ LIBC_INLINE int file_write_hook(cpp::string_view new_str, void *fp) {
size_t written = internal::fwrite_unlocked(new_str.data(), sizeof(char),
new_str.size(), target_file);
if (written != new_str.size() || internal::ferror_unlocked(target_file))
- return FILE_WRITE_ERROR;
+ return -EIO;
return WRITE_OK;
}
-LIBC_INLINE int vfprintf_internal(::FILE *__restrict stream,
- const char *__restrict format,
- internal::ArgList &args) {
+LIBC_INLINE PrintfResult vfprintf_internal(::FILE *__restrict stream,
+ const char *__restrict format,
+ internal::ArgList &args) {
constexpr size_t BUFF_SIZE = 1024;
char buffer[BUFF_SIZE];
printf_core::WriteBuffer<Mode<WriteMode::FLUSH_TO_STREAM>::value> wb(
buffer, BUFF_SIZE, &file_write_hook, reinterpret_cast<void *>(stream));
Writer writer(wb);
internal::flockfile(stream);
- int retval = printf_main(&writer, format, args);
+ auto retval = printf_main(&writer, format, args);
+ if (retval.has_error()) {
+ internal::funlockfile(stream);
+ return retval;
+ }
int flushval = wb.overflow_write("");
if (flushval != WRITE_OK)
- retval = flushval;
+ retval.error = -flushval;
internal::funlockfile(stream);
return retval;
}
diff --git a/libc/src/stdio/printf_core/write_int_converter.h b/libc/src/stdio/printf_core/write_int_converter.h
index efcff278bd284..b424278c66185 100644
--- a/libc/src/stdio/printf_core/write_int_converter.h
+++ b/libc/src/stdio/printf_core/write_int_converter.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
+#include "hdr/errno_macros.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"
@@ -26,14 +27,14 @@ LIBC_INLINE int convert_write_int(Writer<write_mode> *writer,
#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
// This is an additional check added by LLVM-libc.
if (to_conv.conv_val_ptr == nullptr)
- return NULLPTR_WRITE_ERROR;
+ return -EINVAL;
#endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
- int written = writer->get_chars_written();
+ size_t written = writer->get_chars_written();
switch (to_conv.length_modifier) {
case LengthModifier::none:
- *reinterpret_cast<int *>(to_conv.conv_val_ptr) = written;
+ *reinterpret_cast<int *>(to_conv.conv_val_ptr) = static_cast<int>(written);
break;
case LengthModifier::l:
*reinterpret_cast<long *>(to_conv.conv_val_ptr) = written;
diff --git a/libc/src/stdio/printf_core/writer.h b/libc/src/stdio/printf_core/writer.h
index 1d4734a51b9b8..9de108ece510f 100644
--- a/libc/src/stdio/printf_core/writer.h
+++ b/libc/src/stdio/printf_core/writer.h
@@ -127,7 +127,7 @@ template <WriteMode write_mode> struct WriteBuffer {
template <WriteMode write_mode> class Writer final {
WriteBuffer<write_mode> &wb;
- int chars_written = 0;
+ size_t chars_written = 0;
LIBC_INLINE int pad(char new_char, size_t length) {
// First, fill as much of the buffer as possible with the padding char.
@@ -161,7 +161,7 @@ template <WriteMode write_mode> class Writer final {
// Takes a string, copies it into the buffer if there is space, else passes it
// to the overflow mechanism to be handled separately.
LIBC_INLINE int write(cpp::string_view new_string) {
- chars_written += static_cast<int>(new_string.size());
+ chars_written += new_string.size();
if (LIBC_LIKELY(wb.buff_cur + new_string.size() <= wb.buff_len)) {
inline_memcpy(wb.buff + wb.buff_cur, new_string.data(),
new_string.size());
@@ -175,7 +175,7 @@ template <WriteMode write_mode> class Writer final {
// if there is space, else calls pad which will loop and call the overflow
// mechanism on a secondary buffer.
LIBC_INLINE int write(char new_char, size_t length) {
- chars_written += static_cast<int>(length);
+ chars_written += length;
if (LIBC_LIKELY(wb.buff_cur + length <= wb.buff_len)) {
inline_memset(wb.buff + wb.buff_cur, static_cast<unsigned char>(new_char),
@@ -199,7 +199,7 @@ template <WriteMode write_mode> class Writer final {
return wb.overflow_write(char_string_view);
}
- LIBC_INLINE int get_chars_written() { return chars_written; }
+ LIBC_INLINE size_t get_chars_written() { return chars_written; }
};
// Class-template auto deduction helpers.
diff --git a/libc/src/stdio/snprintf.cpp b/libc/src/stdio/snprintf.cpp
index c8940862f711f..6691da1debb5a 100644
--- a/libc/src/stdio/snprintf.cpp
+++ b/libc/src/stdio/snprintf.cpp
@@ -32,10 +32,20 @@ LLVM_LIBC_FUNCTION(int, snprintf,
wb(buffer, (buffsz > 0 ? buffsz - 1 : 0));
printf_core::Writer writer(wb);
- int ret_val = printf_core::printf_main(&writer, format, args);
+ auto ret_val = printf_core::printf_main(&writer, format, args);
+ if (ret_val.has_error()) {
+ libc_errno = ret_val.error;
+ return -1;
+ }
if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer.
wb.buff[wb.buff_cur] = '\0';
- return ret_val;
+
+ if (ret_val.value > cpp::numeric_limits<int>::max()) {
+ libc_errno = EOVERFLOW;
+ return -1;
+ }
+
+ return static_cast<int>(ret_val.value);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/sprintf.cpp b/libc/src/stdio/sprintf.cpp
index 7be97d3591aaf..10db9feb80121 100644
--- a/libc/src/stdio/sprintf.cpp
+++ b/libc/src/stdio/sprintf.cpp
@@ -33,9 +33,19 @@ LLVM_LIBC_FUNCTION(int, sprintf,
wb(buffer, cpp::numeric_limits<size_t>::max());
printf_core::Writer writer(wb);
- int ret_val = printf_core::printf_main(&writer, format, args);
+ auto ret_val = printf_core::printf_main(&writer, format, args);
+ if (ret_val.has_error()) {
+ libc_errno = ret_val.error;
+ return -1;
+ }
wb.buff[wb.buff_cur] = '\0';
- return ret_val;
+
+ if (ret_val.value > cpp::numeric_limits<int>::max()) {
+ libc_errno = EOVERFLOW;
+ return -1;
+ }
+
+ return static_cast<int>(ret_val.value);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/vasprintf.cpp b/libc/src/stdio/vasprintf.cpp
index 4a44d4a0f8842..11174727e5d64 100644
--- a/libc/src/stdio/vasprintf.cpp
+++ b/libc/src/stdio/vasprintf.cpp
@@ -18,7 +18,16 @@ LLVM_LIBC_FUNCTION(int, vasprintf,
internal::ArgList args(vlist); // This holder class allo...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/162876
More information about the libc-commits
mailing list