[libc-commits] [libc] [libc] Return an error from swprintf/vswprintf when output is truncated (PR #245171)
via libc-commits
libc-commits at lists.llvm.org
Mon Nov 23 01:41:06 PST 2026
https://github.com/SohamDutta02 created https://github.com/llvm/llvm-project/pull/245171
`swprintf` and `vswprintf` currently follow `snprintf` semantics when the output doesn't fit: they write a truncated string but return the full, untruncated length. For example, `swprintf(buf, 5, L"%ls", L"1234567890")` returns 10.
The wide functions are specified differently:
- C11 7.29.2.3p3 (`swprintf`) and 7.29.2.7p3 (`vswprintf`) require a negative return value if `n` or more wide characters were requested to be written.
- POSIX also requires `errno` to be set in that case.
Callers check for a negative return to detect truncation, so the current behavior makes truncation look like success and can return a count larger than the buffer.
### Changes
- Add a `TRUNCATION_ERROR` code to `printf_core/core_structs.h` and map it in both error mappers: `E2BIG` on Linux, and `ERANGE` on generic targets (the same as `OVERFLOW_ERROR`, since the generic mapper only uses C-standard errno values).
- In `swprintf` and `vswprintf`, return -1 and set `errno` through `internal_error_to_errno` when the output length is `>= bufsz`. When `bufsz > 0`, the truncated, null-terminated output is still written to the buffer.
- The existing `EOVERFLOW` check for counts above `INT_MAX` runs first, so that case is unchanged.
### Tests
- Updated `CutOff` in `swprintf_test.cpp` and `vswprintf_test.cpp`, which encoded the `snprintf` behavior, to expect -1 and `E2BIG` while still checking the truncated buffer contents.
- Added boundary cases: output of exactly `bufsz` characters fails, output of `bufsz - 1` characters succeeds, `bufsz == 1` is tested with empty and non-empty output, and `(nullptr, 0, L"")` fails.
- `CharsWrittenOverflow` now checks for `EOVERFLOW` specifically, so the order of the two checks is fixed by the tests.
- Without the source change, the updated `CutOff` tests fail in both suites. With it, the wchar suites and the stdio printf and stdlib `strfrom` suites all pass, built in overlay mode.
### Behavior change
`swprintf(nullptr, 0, ...)` now always fails, so it can no longer be used to query the required buffer size the way `snprintf(nullptr, 0, ...)` can. glibc behaves the same way.
### Open question: errno value
The standards don't say which errno value to use. glibc returns -1 in every overflow case I tried but sets `E2BIG` in only some of them. FreeBSD uses `EOVERFLOW`. I chose `E2BIG` to match glibc on Linux. If `EOVERFLOW` is preferred, the change is small: one line in the Linux error mapper plus the test expectations.
Fixes #245060.
>From 11fe46cf9c20cfa03af08990866cef18b43cd7bf Mon Sep 17 00:00:00 2001
From: Soham Dutta <soham32146 at gmail.com>
Date: Mon, 23 Nov 2026 08:07:21 +0000
Subject: [PATCH] [libc] Return an error from swprintf/vswprintf when output is
truncated
swprintf and vswprintf returned the untruncated output length when the
output did not fit in the buffer, as snprintf does. C11 7.29.2.3p3 and
7.29.2.7p3 instead require a negative return value if bufsz or more wide
characters were requested to be written, and POSIX requires errno to be
set. Callers rely on the negative return to detect truncation, so the
old behavior made truncation look like success and could return a count
larger than the buffer.
Add a TRUNCATION_ERROR printf_core error code, mapped to E2BIG on Linux
(matching glibc) and ERANGE on generic targets, and return -1 with that
errno when the output length is >= bufsz. The truncated, null-terminated
output is still written when bufsz > 0. The existing EOVERFLOW check for
counts above INT_MAX still takes priority.
Update the unit tests, which encoded snprintf semantics, and add
exact-fit boundary cases.
Fixes #245060.
---
libc/src/__support/printf_core/core_structs.h | 3 +
.../printf_core/generic/error_mapper.h | 1 +
.../printf_core/linux/error_mapper.h | 2 +
libc/src/wchar/swprintf.cpp | 9 +++
libc/src/wchar/vswprintf.cpp | 9 +++
libc/test/src/wchar/swprintf_test.cpp | 56 ++++++++++++++++---
libc/test/src/wchar/vswprintf_test.cpp | 51 ++++++++++++++---
7 files changed, 114 insertions(+), 17 deletions(-)
diff --git a/libc/src/__support/printf_core/core_structs.h b/libc/src/__support/printf_core/core_structs.h
index a25b9e29ab8421..f423468e73373f 100644
--- a/libc/src/__support/printf_core/core_structs.h
+++ b/libc/src/__support/printf_core/core_structs.h
@@ -163,6 +163,9 @@ constexpr int ALLOCATION_ERROR = -1006;
constexpr int OVERFLOW_ERROR = -1007;
constexpr int ILLEGAL_WIDE_CHAR = -1008;
constexpr int MB_CONVERSION_ERROR = -1009;
+// Used by functions such as swprintf that must fail, rather than silently
+// truncate, when the output does not fit in the buffer.
+constexpr int TRUNCATION_ERROR = -1010;
} // namespace printf_core
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/printf_core/generic/error_mapper.h b/libc/src/__support/printf_core/generic/error_mapper.h
index a3f355b531b33c..772053c9d21d48 100644
--- a/libc/src/__support/printf_core/generic/error_mapper.h
+++ b/libc/src/__support/printf_core/generic/error_mapper.h
@@ -34,6 +34,7 @@ LIBC_INLINE static int internal_error_to_errno(int internal_error) {
case INT_CONVERSION_ERROR:
case FIXED_POINT_CONVERSION_ERROR:
case OVERFLOW_ERROR:
+ case TRUNCATION_ERROR:
return ERANGE;
default:
LIBC_ASSERT(
diff --git a/libc/src/__support/printf_core/linux/error_mapper.h b/libc/src/__support/printf_core/linux/error_mapper.h
index db8cddcd11f661..d381b997743fe3 100644
--- a/libc/src/__support/printf_core/linux/error_mapper.h
+++ b/libc/src/__support/printf_core/linux/error_mapper.h
@@ -43,6 +43,8 @@ LIBC_INLINE static int internal_error_to_errno(int internal_error) {
case ILLEGAL_WIDE_CHAR:
case MB_CONVERSION_ERROR:
return EILSEQ;
+ case TRUNCATION_ERROR:
+ return E2BIG;
default:
LIBC_ASSERT(
false &&
diff --git a/libc/src/wchar/swprintf.cpp b/libc/src/wchar/swprintf.cpp
index 35b27ce840cbac..6a174cdfc4aafd 100644
--- a/libc/src/wchar/swprintf.cpp
+++ b/libc/src/wchar/swprintf.cpp
@@ -64,6 +64,15 @@ LLVM_LIBC_FUNCTION(int, swprintf,
return -1;
}
+ // Unlike snprintf, swprintf must return a negative value if bufsz or more
+ // wide characters were requested to be written (C11 7.29.2.3p3), and POSIX
+ // requires errno to be set. The truncated output written above is kept.
+ if (ret_val.value() >= bufsz) {
+ libc_errno =
+ printf_core::internal_error_to_errno(-printf_core::TRUNCATION_ERROR);
+ return -1;
+ }
+
return static_cast<int>(ret_val.value());
}
diff --git a/libc/src/wchar/vswprintf.cpp b/libc/src/wchar/vswprintf.cpp
index 37200db501c1c4..f5eacc902e4380 100644
--- a/libc/src/wchar/vswprintf.cpp
+++ b/libc/src/wchar/vswprintf.cpp
@@ -61,6 +61,15 @@ LLVM_LIBC_FUNCTION(int, vswprintf,
return -1;
}
+ // Unlike vsnprintf, vswprintf must return a negative value if bufsz or more
+ // wide characters were requested to be written (C11 7.29.2.7p3), and POSIX
+ // requires errno to be set. The truncated output written above is kept.
+ if (ret_val.value() >= bufsz) {
+ libc_errno =
+ printf_core::internal_error_to_errno(-printf_core::TRUNCATION_ERROR);
+ return -1;
+ }
+
return static_cast<int>(ret_val.value());
}
diff --git a/libc/test/src/wchar/swprintf_test.cpp b/libc/test/src/wchar/swprintf_test.cpp
index e0cc85485785f3..f4ad5f5e8ba9b8 100644
--- a/libc/test/src/wchar/swprintf_test.cpp
+++ b/libc/test/src/wchar/swprintf_test.cpp
@@ -3091,6 +3091,9 @@ TEST_F(LlvmLibcSwprintfTest, WideCharStringConversion) {
}
#endif // !LIBC_COPT_PRINTF_DISABLE_WIDE && !LIBC_TARGET_ARCH_IS_GPU
+// Unlike snprintf, swprintf returns a negative value and sets errno if bufsz or
+// more wide characters would be written. The truncated, null-terminated output
+// is still written to the buffer when bufsz is greater than zero.
TEST_F(LlvmLibcSwprintfTest, CutOff) {
constexpr size_t N = 100;
wchar_t buff[N];
@@ -3098,19 +3101,34 @@ TEST_F(LlvmLibcSwprintfTest, CutOff) {
written = LIBC_NAMESPACE::swprintf(buff, 16,
L"A simple string with no conversions.");
- EXPECT_EQ(written, 36);
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"A simple string");
written = LIBC_NAMESPACE::swprintf(buff, 5, L"%s", "1234567890");
- EXPECT_EQ(written, 10);
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"1234");
written = LIBC_NAMESPACE::swprintf(buff, 5, L"%ls", L"1234567890");
- EXPECT_EQ(written, 10);
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"1234");
+ // The output fills the buffer exactly, leaving no room for the terminator.
+ written = LIBC_NAMESPACE::swprintf(buff, 5, L"%ls", L"12345");
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_STREQ(buff, L"1234");
+
+ written = LIBC_NAMESPACE::swprintf(buff, 1, L"a");
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_STREQ(buff, L"");
+
written = LIBC_NAMESPACE::swprintf(buff, 67, L"%-101c", 'a');
- EXPECT_EQ(written, 101);
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"a "
L" " // Each of these is 8 spaces, and there are 8.
L" " // In total there are 65 spaces
@@ -3121,12 +3139,22 @@ TEST_F(LlvmLibcSwprintfTest, CutOff) {
L" "
L" ");
- // passing null as the output pointer is allowed as long as bufsz is 0.
+ // Passing null as the output pointer is allowed as long as bufsz is 0, but
+ // since there is no room for even the null terminator, the call fails.
+ // Unlike snprintf, swprintf cannot be used to query the required size.
written = LIBC_NAMESPACE::swprintf(nullptr, 0, L"%s and more", "1234567890");
- EXPECT_EQ(written, 19);
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
+ written = LIBC_NAMESPACE::swprintf(nullptr, 0, L"");
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
+
+ // The INT_MIN width is treated as a left-justified width of INT_MAX, which
+ // still fits in an int, so this fails with E2BIG rather than EOVERFLOW.
written = LIBC_NAMESPACE::swprintf(nullptr, 0, L"%*s", INT_MIN, "nothing");
- EXPECT_EQ(written, INT_MAX);
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
}
TEST_F(LlvmLibcSwprintfTest, NoCutOff) {
@@ -3146,15 +3174,25 @@ TEST_F(LlvmLibcSwprintfTest, NoCutOff) {
written = LIBC_NAMESPACE::swprintf(buff, 20, L"%ls", L"1234567890");
EXPECT_EQ(written, 10);
ASSERT_STREQ(buff, L"1234567890");
+
+ // The output and the null terminator fill the buffer exactly.
+ written = LIBC_NAMESPACE::swprintf(buff, 5, L"%ls", L"1234");
+ EXPECT_EQ(written, 4);
+ ASSERT_STREQ(buff, L"1234");
+
+ written = LIBC_NAMESPACE::swprintf(buff, 1, L"%ls", L"");
+ EXPECT_EQ(written, 0);
+ ASSERT_STREQ(buff, L"");
}
TEST_F(LlvmLibcSwprintfTest, CharsWrittenOverflow) {
wchar_t buff[0];
// Trigger an overflow in the return value of swprintf by writing more than
- // INT_MAX wide characters.
+ // INT_MAX wide characters. This is reported as EOVERFLOW even though the
+ // output also does not fit in the buffer.
int int_max = LIBC_NAMESPACE::cpp::numeric_limits<int>::max();
int written = LIBC_NAMESPACE::swprintf(buff, 0, L"%*stest", int_max, "");
EXPECT_LT(written, 0);
- ASSERT_ERRNO_FAILURE();
+ ASSERT_ERRNO_EQ(EOVERFLOW);
}
diff --git a/libc/test/src/wchar/vswprintf_test.cpp b/libc/test/src/wchar/vswprintf_test.cpp
index 9f25e270cf45c5..f63d78c8abc140 100644
--- a/libc/test/src/wchar/vswprintf_test.cpp
+++ b/libc/test/src/wchar/vswprintf_test.cpp
@@ -91,25 +91,43 @@ TEST_F(LlvmLibcVSWPrintfTest, CharConv) {
ASSERT_ERRNO_EQ(EILSEQ);
}
+// Unlike vsnprintf, vswprintf returns a negative value and sets errno if bufsz
+// or more wide characters would be written. The truncated, null-terminated
+// output is still written to the buffer when bufsz is greater than zero.
TEST_F(LlvmLibcVSWPrintfTest, CutOff) {
constexpr size_t N = 100;
wchar_t buff[N];
int written;
written = call_vswprintf(buff, 16, L"A simple string with no conversions.");
- EXPECT_EQ(written, 36);
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"A simple string");
written = call_vswprintf(buff, 5, L"%s", "1234567890");
- EXPECT_EQ(written, 10);
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"1234");
written = call_vswprintf(buff, 5, L"%ls", L"1234567890");
- EXPECT_EQ(written, 10);
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_STREQ(buff, L"1234");
+
+ // The output fills the buffer exactly, leaving no room for the terminator.
+ written = call_vswprintf(buff, 5, L"%ls", L"12345");
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"1234");
+ written = call_vswprintf(buff, 1, L"a");
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_STREQ(buff, L"");
+
written = call_vswprintf(buff, 67, L"%-101c", 'a');
- EXPECT_EQ(written, 101);
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"a "
L" " // Each of these is 8 spaces, and there are 8.
L" " // In total there are 65 spaces
@@ -120,9 +138,16 @@ TEST_F(LlvmLibcVSWPrintfTest, CutOff) {
L" "
L" ");
- // passing null as the output pointer is allowed as long as bufsz is 0.
+ // Passing null as the output pointer is allowed as long as bufsz is 0, but
+ // since there is no room for even the null terminator, the call fails.
+ // Unlike vsnprintf, vswprintf cannot be used to query the required size.
written = call_vswprintf(nullptr, 0, L"%s and more", "1234567890");
- EXPECT_EQ(written, 19);
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
+
+ written = call_vswprintf(nullptr, 0, L"");
+ EXPECT_EQ(written, -1);
+ ASSERT_ERRNO_EQ(E2BIG);
}
TEST_F(LlvmLibcVSWPrintfTest, NoCutOff) {
@@ -141,15 +166,25 @@ TEST_F(LlvmLibcVSWPrintfTest, NoCutOff) {
written = call_vswprintf(buff, 20, L"%ls", L"1234567890");
EXPECT_EQ(written, 10);
ASSERT_STREQ(buff, L"1234567890");
+
+ // The output and the null terminator fill the buffer exactly.
+ written = call_vswprintf(buff, 5, L"%ls", L"1234");
+ EXPECT_EQ(written, 4);
+ ASSERT_STREQ(buff, L"1234");
+
+ written = call_vswprintf(buff, 1, L"%ls", L"");
+ EXPECT_EQ(written, 0);
+ ASSERT_STREQ(buff, L"");
}
TEST_F(LlvmLibcVSWPrintfTest, CharsWrittenOverflow) {
wchar_t buff[1];
// Trigger an overflow in the return value of vswprintf by writing more than
- // INT_MAX wide characters.
+ // INT_MAX wide characters. This is reported as EOVERFLOW even though the
+ // output also does not fit in the buffer.
int int_max = LIBC_NAMESPACE::cpp::numeric_limits<int>::max();
int written = call_vswprintf(buff, 0, L"%*stest", int_max, "");
EXPECT_LT(written, 0);
- ASSERT_ERRNO_FAILURE();
+ ASSERT_ERRNO_EQ(EOVERFLOW);
}
More information about the libc-commits
mailing list