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, (long)-2, // NOLINT
27 (unsigned)-4, (unsigned long)5, // NOLINT
28 (unsigned)10, (unsigned long)11, // NOLINT
29 (void*)0x123, "_string_");
30 EXPECT_EQ(len
, strlen(buf
));
32 if (sizeof(ptr
) == 4) {
33 EXPECT_STREQ("a-1b-2c4294967292e5fahbq"
34 "0x00000123e_string_r", buf
);
36 EXPECT_STREQ("a-1b-2c4294967292e5fahbq"
37 "0x000000000123e_string_r", buf
);
41 TEST(Printf
, OverflowStr
) {
42 char buf
[] = "123456789";
43 uptr len
= internal_snprintf(buf
, 4, "%s", "abcdef"); // NOLINT
44 EXPECT_EQ(len
, (uptr
)6);
45 EXPECT_STREQ("abc", buf
);
47 EXPECT_EQ(buf
[4], '5');
48 EXPECT_EQ(buf
[5], '6');
49 EXPECT_EQ(buf
[6], '7');
50 EXPECT_EQ(buf
[7], '8');
51 EXPECT_EQ(buf
[8], '9');
55 TEST(Printf
, OverflowInt
) {
56 char buf
[] = "123456789";
57 internal_snprintf(buf
, 4, "%d", -123456789); // NOLINT
58 EXPECT_STREQ("-12", buf
);
60 EXPECT_EQ(buf
[4], '5');
61 EXPECT_EQ(buf
[5], '6');
62 EXPECT_EQ(buf
[6], '7');
63 EXPECT_EQ(buf
[7], '8');
64 EXPECT_EQ(buf
[8], '9');
68 TEST(Printf
, OverflowUint
) {
69 char buf
[] = "123456789";
71 if (sizeof(val
) == 4) {
72 val
= (uptr
)0x12345678;
74 val
= (uptr
)0x123456789ULL
;
76 internal_snprintf(buf
, 4, "a%zx", val
); // NOLINT
77 EXPECT_STREQ("a12", buf
);
79 EXPECT_EQ(buf
[4], '5');
80 EXPECT_EQ(buf
[5], '6');
81 EXPECT_EQ(buf
[6], '7');
82 EXPECT_EQ(buf
[7], '8');
83 EXPECT_EQ(buf
[8], '9');
87 TEST(Printf
, OverflowPtr
) {
88 char buf
[] = "123456789";
93 p
= (void*)0x123456789ULL
;
95 internal_snprintf(buf
, 4, "%p", p
); // NOLINT
96 EXPECT_STREQ("0x0", buf
);
98 EXPECT_EQ(buf
[4], '5');
99 EXPECT_EQ(buf
[5], '6');
100 EXPECT_EQ(buf
[6], '7');
101 EXPECT_EQ(buf
[7], '8');
102 EXPECT_EQ(buf
[8], '9');
103 EXPECT_EQ(buf
[9], 0);
107 static void TestAgainstLibc(const char *fmt
, T arg1
, T arg2
) {
109 uptr len
= internal_snprintf(buf
, sizeof(buf
), fmt
, arg1
, arg2
);
111 snprintf(buf2
, sizeof(buf2
), fmt
, arg1
, arg2
);
112 EXPECT_EQ(len
, strlen(buf
));
113 EXPECT_STREQ(buf2
, buf
);
116 TEST(Printf
, MinMax
) {
117 TestAgainstLibc
<int>("%d-%d", INT_MIN
, INT_MAX
); // NOLINT
118 TestAgainstLibc
<long>("%zd-%zd", LONG_MIN
, LONG_MAX
); // NOLINT
119 TestAgainstLibc
<unsigned>("%u-%u", 0, UINT_MAX
); // NOLINT
120 TestAgainstLibc
<unsigned long>("%zu-%zu", 0, ULONG_MAX
); // NOLINT
121 TestAgainstLibc
<unsigned>("%x-%x", 0, UINT_MAX
); // NOLINT
122 TestAgainstLibc
<unsigned long>("%zx-%zx", 0, ULONG_MAX
); // NOLINT
125 TEST(Printf
, Padding
) {
126 TestAgainstLibc
<int>("%3d - %3d", 1, 0);
127 TestAgainstLibc
<int>("%3d - %3d", -1, 123);
128 TestAgainstLibc
<int>("%3d - %3d", -1, -123);
129 TestAgainstLibc
<int>("%3d - %3d", 12, 1234);
130 TestAgainstLibc
<int>("%3d - %3d", -12, -1234);
131 TestAgainstLibc
<int>("%03d - %03d", 1, 0);
132 TestAgainstLibc
<int>("%03d - %03d", -1, 123);
133 TestAgainstLibc
<int>("%03d - %03d", -1, -123);
134 TestAgainstLibc
<int>("%03d - %03d", 12, 1234);
135 TestAgainstLibc
<int>("%03d - %03d", -12, -1234);
138 TEST(Printf
, Precision
) {
140 uptr len
= internal_snprintf(buf
, sizeof(buf
), "%.*s", 3, "12345");
142 EXPECT_STREQ("123", buf
);
143 len
= internal_snprintf(buf
, sizeof(buf
), "%.*s", 6, "12345");
145 EXPECT_STREQ("12345", buf
);
148 } // namespace __sanitizer