1 //===-- sanitizer_printf_test.cc ------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Tests for sanitizer_printf.cc
12 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_common.h"
14 #include "sanitizer_common/sanitizer_libc.h"
15 #include "gtest/gtest.h"
20 namespace __sanitizer
{
24 uptr len
= internal_snprintf(buf
, sizeof(buf
),
25 "a%db%zdc%ue%zuf%xh%zxq%pe%sr",
26 (int)-1, (uptr
)-2, // NOLINT
27 (unsigned)-4, (uptr
)5, // NOLINT
28 (unsigned)10, (uptr
)11, // NOLINT
29 (void*)0x123, "_string_");
30 EXPECT_EQ(len
, strlen(buf
));
32 std::string expectedString
= "a-1b-2c4294967292e5fahbq0x";
33 expectedString
+= std::string(SANITIZER_POINTER_FORMAT_LENGTH
- 3, '0');
34 expectedString
+= "123e_string_r";
35 EXPECT_STREQ(expectedString
.c_str(), buf
);
38 TEST(Printf
, OverflowStr
) {
39 char buf
[] = "123456789";
40 uptr len
= internal_snprintf(buf
, 4, "%s", "abcdef"); // NOLINT
41 EXPECT_EQ(len
, (uptr
)6);
42 EXPECT_STREQ("abc", buf
);
44 EXPECT_EQ(buf
[4], '5');
45 EXPECT_EQ(buf
[5], '6');
46 EXPECT_EQ(buf
[6], '7');
47 EXPECT_EQ(buf
[7], '8');
48 EXPECT_EQ(buf
[8], '9');
52 TEST(Printf
, OverflowInt
) {
53 char buf
[] = "123456789";
54 internal_snprintf(buf
, 4, "%d", -123456789); // NOLINT
55 EXPECT_STREQ("-12", buf
);
57 EXPECT_EQ(buf
[4], '5');
58 EXPECT_EQ(buf
[5], '6');
59 EXPECT_EQ(buf
[6], '7');
60 EXPECT_EQ(buf
[7], '8');
61 EXPECT_EQ(buf
[8], '9');
65 TEST(Printf
, OverflowUint
) {
66 char buf
[] = "123456789";
68 if (sizeof(val
) == 4) {
69 val
= (uptr
)0x12345678;
71 val
= (uptr
)0x123456789ULL
;
73 internal_snprintf(buf
, 4, "a%zx", val
); // NOLINT
74 EXPECT_STREQ("a12", buf
);
76 EXPECT_EQ(buf
[4], '5');
77 EXPECT_EQ(buf
[5], '6');
78 EXPECT_EQ(buf
[6], '7');
79 EXPECT_EQ(buf
[7], '8');
80 EXPECT_EQ(buf
[8], '9');
84 TEST(Printf
, OverflowPtr
) {
85 char buf
[] = "123456789";
90 p
= (void*)0x123456789ULL
;
92 internal_snprintf(buf
, 4, "%p", p
); // NOLINT
93 EXPECT_STREQ("0x0", buf
);
95 EXPECT_EQ(buf
[4], '5');
96 EXPECT_EQ(buf
[5], '6');
97 EXPECT_EQ(buf
[6], '7');
98 EXPECT_EQ(buf
[7], '8');
99 EXPECT_EQ(buf
[8], '9');
100 EXPECT_EQ(buf
[9], 0);
104 // Oh well, MSVS headers don't define snprintf.
105 # define snprintf _snprintf
109 static void TestAgainstLibc(const char *fmt
, T arg1
, T arg2
) {
111 uptr len
= internal_snprintf(buf
, sizeof(buf
), fmt
, arg1
, arg2
);
113 snprintf(buf2
, sizeof(buf2
), fmt
, arg1
, arg2
);
114 EXPECT_EQ(len
, strlen(buf
));
115 EXPECT_STREQ(buf2
, buf
);
118 TEST(Printf
, MinMax
) {
119 TestAgainstLibc
<int>("%d-%d", INT_MIN
, INT_MAX
); // NOLINT
120 TestAgainstLibc
<unsigned>("%u-%u", 0, UINT_MAX
); // NOLINT
121 TestAgainstLibc
<unsigned>("%x-%x", 0, UINT_MAX
); // NOLINT
123 // %z* format doesn't seem to be supported by MSVS.
124 TestAgainstLibc
<long>("%zd-%zd", LONG_MIN
, LONG_MAX
); // NOLINT
125 TestAgainstLibc
<unsigned long>("%zu-%zu", 0, ULONG_MAX
); // NOLINT
126 TestAgainstLibc
<unsigned long>("%zx-%zx", 0, ULONG_MAX
); // NOLINT
130 TEST(Printf
, Padding
) {
131 TestAgainstLibc
<int>("%3d - %3d", 1, 0);
132 TestAgainstLibc
<int>("%3d - %3d", -1, 123);
133 TestAgainstLibc
<int>("%3d - %3d", -1, -123);
134 TestAgainstLibc
<int>("%3d - %3d", 12, 1234);
135 TestAgainstLibc
<int>("%3d - %3d", -12, -1234);
136 TestAgainstLibc
<int>("%03d - %03d", 1, 0);
137 TestAgainstLibc
<int>("%03d - %03d", -1, 123);
138 TestAgainstLibc
<int>("%03d - %03d", -1, -123);
139 TestAgainstLibc
<int>("%03d - %03d", 12, 1234);
140 TestAgainstLibc
<int>("%03d - %03d", -12, -1234);
143 TEST(Printf
, Precision
) {
145 uptr len
= internal_snprintf(buf
, sizeof(buf
), "%.*s", 3, "12345");
147 EXPECT_STREQ("123", buf
);
148 len
= internal_snprintf(buf
, sizeof(buf
), "%.*s", 6, "12345");
150 EXPECT_STREQ("12345", buf
);
153 } // namespace __sanitizer