aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/stdio/snprintf_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libc/test/src/stdio/snprintf_test.cpp')
-rw-r--r--libc/test/src/stdio/snprintf_test.cpp15
1 files changed, 15 insertions, 0 deletions
diff --git a/libc/test/src/stdio/snprintf_test.cpp b/libc/test/src/stdio/snprintf_test.cpp
index baaa664..95507e0 100644
--- a/libc/test/src/stdio/snprintf_test.cpp
+++ b/libc/test/src/stdio/snprintf_test.cpp
@@ -8,8 +8,12 @@
#include "src/stdio/snprintf.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
+using LlvmLibcSNPrintfTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
// The sprintf test cases cover testing the shared printf functionality, so
// these tests will focus on snprintf exclusive features.
@@ -59,3 +63,14 @@ TEST(LlvmLibcSNPrintfTest, NoCutOff) {
EXPECT_EQ(written, 10);
ASSERT_STREQ(buff, "1234567890");
}
+
+TEST(LlvmLibcSNPrintfTest, CharsWrittenOverflow) {
+ char buff[0];
+
+ // Trigger an overflow in the return value of snprintf by writing more than
+ // INT_MAX bytes.
+ int int_max = LIBC_NAMESPACE::cpp::numeric_limits<int>::max();
+ int written = LIBC_NAMESPACE::snprintf(buff, 0, "%*stest", int_max, "");
+ EXPECT_LT(written, 0);
+ ASSERT_ERRNO_FAILURE();
+}