[libc-commits] [libc] [libc] return -1 when v/swprintf truncate output (PR #245838)
Aaron Danen via libc-commits
libc-commits at lists.llvm.org
Wed Nov 25 07:06:16 PST 2026
https://github.com/aadanen updated https://github.com/llvm/llvm-project/pull/245838
>From 7cc54e165c5cac7bcc26d9962359828939c661af Mon Sep 17 00:00:00 2001
From: Aaron Danen <aaron.danen at gmail.com>
Date: Tue, 24 Nov 2026 16:20:41 -0800
Subject: [PATCH 1/2] [libc] return -1 when v/swprintf truncate output
---
libc/src/__support/printf_core/core_structs.h | 1 +
.../printf_core/generic/error_mapper.h | 2 ++
.../__support/printf_core/linux/error_mapper.h | 2 ++
libc/src/wchar/swprintf.cpp | 6 ++++++
libc/src/wchar/vswprintf.cpp | 6 ++++++
libc/test/src/wchar/swprintf_test.cpp | 18 ++++++++++++------
libc/test/src/wchar/vswprintf_test.cpp | 15 ++++++++++-----
7 files changed, 39 insertions(+), 11 deletions(-)
diff --git a/libc/src/__support/printf_core/core_structs.h b/libc/src/__support/printf_core/core_structs.h
index a25b9e29ab8421..1dc6d99ef9fbda 100644
--- a/libc/src/__support/printf_core/core_structs.h
+++ b/libc/src/__support/printf_core/core_structs.h
@@ -163,6 +163,7 @@ constexpr int ALLOCATION_ERROR = -1006;
constexpr int OVERFLOW_ERROR = -1007;
constexpr int ILLEGAL_WIDE_CHAR = -1008;
constexpr int MB_CONVERSION_ERROR = -1009;
+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..24554b8cba45b5 100644
--- a/libc/src/__support/printf_core/generic/error_mapper.h
+++ b/libc/src/__support/printf_core/generic/error_mapper.h
@@ -35,6 +35,8 @@ LIBC_INLINE static int internal_error_to_errno(int internal_error) {
case FIXED_POINT_CONVERSION_ERROR:
case OVERFLOW_ERROR:
return ERANGE;
+ case TRUNCATION_ERROR:
+ return E2BIG;
default:
LIBC_ASSERT(
false &&
diff --git a/libc/src/__support/printf_core/linux/error_mapper.h b/libc/src/__support/printf_core/linux/error_mapper.h
index db8cddcd11f661..29bcaf17f9c475 100644
--- a/libc/src/__support/printf_core/linux/error_mapper.h
+++ b/libc/src/__support/printf_core/linux/error_mapper.h
@@ -40,6 +40,8 @@ LIBC_INLINE static int internal_error_to_errno(int internal_error) {
return ENOMEM;
case OVERFLOW_ERROR:
return EOVERFLOW;
+ case TRUNCATION_ERROR:
+ return E2BIG;
case ILLEGAL_WIDE_CHAR:
case MB_CONVERSION_ERROR:
return EILSEQ;
diff --git a/libc/src/wchar/swprintf.cpp b/libc/src/wchar/swprintf.cpp
index 35b27ce840cbac..cfffd105526ec2 100644
--- a/libc/src/wchar/swprintf.cpp
+++ b/libc/src/wchar/swprintf.cpp
@@ -64,6 +64,12 @@ LLVM_LIBC_FUNCTION(int, swprintf,
return -1;
}
+ 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..4a377ee539b78e 100644
--- a/libc/src/wchar/vswprintf.cpp
+++ b/libc/src/wchar/vswprintf.cpp
@@ -61,6 +61,12 @@ LLVM_LIBC_FUNCTION(int, vswprintf,
return -1;
}
+ 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..a05e2a962e44bb 100644
--- a/libc/test/src/wchar/swprintf_test.cpp
+++ b/libc/test/src/wchar/swprintf_test.cpp
@@ -3098,19 +3098,23 @@ TEST_F(LlvmLibcSwprintfTest, CutOff) {
written = LIBC_NAMESPACE::swprintf(buff, 16,
L"A simple string with no conversions.");
- EXPECT_EQ(written, 36);
+ EXPECT_LT(written, 0);
+ 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_LT(written, 0);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"1234");
written = LIBC_NAMESPACE::swprintf(buff, 5, L"%ls", L"1234567890");
- EXPECT_EQ(written, 10);
+ EXPECT_LT(written, 0);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"1234");
written = LIBC_NAMESPACE::swprintf(buff, 67, L"%-101c", 'a');
- EXPECT_EQ(written, 101);
+ EXPECT_LT(written, 0);
+ 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
@@ -3123,10 +3127,12 @@ TEST_F(LlvmLibcSwprintfTest, CutOff) {
// passing null as the output pointer is allowed as long as bufsz is 0.
written = LIBC_NAMESPACE::swprintf(nullptr, 0, L"%s and more", "1234567890");
- EXPECT_EQ(written, 19);
+ EXPECT_LT(written, 0);
+ ASSERT_ERRNO_EQ(E2BIG);
written = LIBC_NAMESPACE::swprintf(nullptr, 0, L"%*s", INT_MIN, "nothing");
- EXPECT_EQ(written, INT_MAX);
+ EXPECT_LT(written, 0);
+ ASSERT_ERRNO_EQ(E2BIG);
}
TEST_F(LlvmLibcSwprintfTest, NoCutOff) {
diff --git a/libc/test/src/wchar/vswprintf_test.cpp b/libc/test/src/wchar/vswprintf_test.cpp
index 9f25e270cf45c5..fbb9912b19cd4d 100644
--- a/libc/test/src/wchar/vswprintf_test.cpp
+++ b/libc/test/src/wchar/vswprintf_test.cpp
@@ -97,19 +97,23 @@ TEST_F(LlvmLibcVSWPrintfTest, CutOff) {
int written;
written = call_vswprintf(buff, 16, L"A simple string with no conversions.");
- EXPECT_EQ(written, 36);
+ EXPECT_LT(written, 0);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"A simple string");
written = call_vswprintf(buff, 5, L"%s", "1234567890");
- EXPECT_EQ(written, 10);
+ EXPECT_LT(written, 0);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"1234");
written = call_vswprintf(buff, 5, L"%ls", L"1234567890");
- EXPECT_EQ(written, 10);
+ EXPECT_LT(written, 0);
+ ASSERT_ERRNO_EQ(E2BIG);
ASSERT_STREQ(buff, L"1234");
written = call_vswprintf(buff, 67, L"%-101c", 'a');
- EXPECT_EQ(written, 101);
+ EXPECT_LT(written, 0);
+ 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
@@ -122,7 +126,8 @@ TEST_F(LlvmLibcVSWPrintfTest, CutOff) {
// passing null as the output pointer is allowed as long as bufsz is 0.
written = call_vswprintf(nullptr, 0, L"%s and more", "1234567890");
- EXPECT_EQ(written, 19);
+ EXPECT_LT(written, 0);
+ ASSERT_ERRNO_EQ(E2BIG);
}
TEST_F(LlvmLibcVSWPrintfTest, NoCutOff) {
>From 364f85360712c45a15c28bdfcdc7ef76767940e7 Mon Sep 17 00:00:00 2001
From: Aaron Danen <aaron.danen at gmail.com>
Date: Wed, 25 Nov 2026 07:05:16 -0800
Subject: [PATCH 2/2] use OVERFLOW_ERROR errno mapping
---
libc/src/__support/printf_core/core_structs.h | 1 -
.../src/__support/printf_core/generic/error_mapper.h | 2 --
libc/src/__support/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 | 12 ++++++------
libc/test/src/wchar/vswprintf_test.cpp | 10 +++++-----
7 files changed, 15 insertions(+), 30 deletions(-)
diff --git a/libc/src/__support/printf_core/core_structs.h b/libc/src/__support/printf_core/core_structs.h
index 1dc6d99ef9fbda..a25b9e29ab8421 100644
--- a/libc/src/__support/printf_core/core_structs.h
+++ b/libc/src/__support/printf_core/core_structs.h
@@ -163,7 +163,6 @@ constexpr int ALLOCATION_ERROR = -1006;
constexpr int OVERFLOW_ERROR = -1007;
constexpr int ILLEGAL_WIDE_CHAR = -1008;
constexpr int MB_CONVERSION_ERROR = -1009;
-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 24554b8cba45b5..a3f355b531b33c 100644
--- a/libc/src/__support/printf_core/generic/error_mapper.h
+++ b/libc/src/__support/printf_core/generic/error_mapper.h
@@ -35,8 +35,6 @@ LIBC_INLINE static int internal_error_to_errno(int internal_error) {
case FIXED_POINT_CONVERSION_ERROR:
case OVERFLOW_ERROR:
return ERANGE;
- case TRUNCATION_ERROR:
- return E2BIG;
default:
LIBC_ASSERT(
false &&
diff --git a/libc/src/__support/printf_core/linux/error_mapper.h b/libc/src/__support/printf_core/linux/error_mapper.h
index 29bcaf17f9c475..db8cddcd11f661 100644
--- a/libc/src/__support/printf_core/linux/error_mapper.h
+++ b/libc/src/__support/printf_core/linux/error_mapper.h
@@ -40,8 +40,6 @@ LIBC_INLINE static int internal_error_to_errno(int internal_error) {
return ENOMEM;
case OVERFLOW_ERROR:
return EOVERFLOW;
- case TRUNCATION_ERROR:
- return E2BIG;
case ILLEGAL_WIDE_CHAR:
case MB_CONVERSION_ERROR:
return EILSEQ;
diff --git a/libc/src/wchar/swprintf.cpp b/libc/src/wchar/swprintf.cpp
index cfffd105526ec2..79c871a9f53ab5 100644
--- a/libc/src/wchar/swprintf.cpp
+++ b/libc/src/wchar/swprintf.cpp
@@ -58,18 +58,13 @@ LLVM_LIBC_FUNCTION(int, swprintf,
wb.buff[wb.buff_cur] = L'\0';
}
- if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {
+ if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max()) ||
+ ret_val.value() >= bufsz) {
libc_errno =
printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);
return -1;
}
- 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 4a377ee539b78e..7f6747609572a9 100644
--- a/libc/src/wchar/vswprintf.cpp
+++ b/libc/src/wchar/vswprintf.cpp
@@ -55,18 +55,13 @@ LLVM_LIBC_FUNCTION(int, vswprintf,
wb.buff[wb.buff_cur] = L'\0';
}
- if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {
+ if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max()) ||
+ ret_val.value() >= bufsz) {
libc_errno =
printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);
return -1;
}
- 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 a05e2a962e44bb..687ed89e84c4e3 100644
--- a/libc/test/src/wchar/swprintf_test.cpp
+++ b/libc/test/src/wchar/swprintf_test.cpp
@@ -3099,22 +3099,22 @@ TEST_F(LlvmLibcSwprintfTest, CutOff) {
written = LIBC_NAMESPACE::swprintf(buff, 16,
L"A simple string with no conversions.");
EXPECT_LT(written, 0);
- ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_ERRNO_FAILURE();
ASSERT_STREQ(buff, L"A simple string");
written = LIBC_NAMESPACE::swprintf(buff, 5, L"%s", "1234567890");
EXPECT_LT(written, 0);
- ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_ERRNO_FAILURE();
ASSERT_STREQ(buff, L"1234");
written = LIBC_NAMESPACE::swprintf(buff, 5, L"%ls", L"1234567890");
EXPECT_LT(written, 0);
- ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_ERRNO_FAILURE();
ASSERT_STREQ(buff, L"1234");
written = LIBC_NAMESPACE::swprintf(buff, 67, L"%-101c", 'a');
EXPECT_LT(written, 0);
- ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_ERRNO_FAILURE();
ASSERT_STREQ(buff, L"a "
L" " // Each of these is 8 spaces, and there are 8.
L" " // In total there are 65 spaces
@@ -3128,11 +3128,11 @@ TEST_F(LlvmLibcSwprintfTest, CutOff) {
// passing null as the output pointer is allowed as long as bufsz is 0.
written = LIBC_NAMESPACE::swprintf(nullptr, 0, L"%s and more", "1234567890");
EXPECT_LT(written, 0);
- ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_ERRNO_FAILURE();
written = LIBC_NAMESPACE::swprintf(nullptr, 0, L"%*s", INT_MIN, "nothing");
EXPECT_LT(written, 0);
- ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_ERRNO_FAILURE();
}
TEST_F(LlvmLibcSwprintfTest, NoCutOff) {
diff --git a/libc/test/src/wchar/vswprintf_test.cpp b/libc/test/src/wchar/vswprintf_test.cpp
index fbb9912b19cd4d..2b5e9e43164004 100644
--- a/libc/test/src/wchar/vswprintf_test.cpp
+++ b/libc/test/src/wchar/vswprintf_test.cpp
@@ -98,22 +98,22 @@ TEST_F(LlvmLibcVSWPrintfTest, CutOff) {
written = call_vswprintf(buff, 16, L"A simple string with no conversions.");
EXPECT_LT(written, 0);
- ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_ERRNO_FAILURE();
ASSERT_STREQ(buff, L"A simple string");
written = call_vswprintf(buff, 5, L"%s", "1234567890");
EXPECT_LT(written, 0);
- ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_ERRNO_FAILURE();
ASSERT_STREQ(buff, L"1234");
written = call_vswprintf(buff, 5, L"%ls", L"1234567890");
EXPECT_LT(written, 0);
- ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_ERRNO_FAILURE();
ASSERT_STREQ(buff, L"1234");
written = call_vswprintf(buff, 67, L"%-101c", 'a');
EXPECT_LT(written, 0);
- ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_ERRNO_FAILURE();
ASSERT_STREQ(buff, L"a "
L" " // Each of these is 8 spaces, and there are 8.
L" " // In total there are 65 spaces
@@ -127,7 +127,7 @@ TEST_F(LlvmLibcVSWPrintfTest, CutOff) {
// passing null as the output pointer is allowed as long as bufsz is 0.
written = call_vswprintf(nullptr, 0, L"%s and more", "1234567890");
EXPECT_LT(written, 0);
- ASSERT_ERRNO_EQ(E2BIG);
+ ASSERT_ERRNO_FAILURE();
}
TEST_F(LlvmLibcVSWPrintfTest, NoCutOff) {
More information about the libc-commits
mailing list