1 //===-- sanitizer_printf_test.cc ------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Tests for sanitizer_printf.cc
11 //
12 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_common.h"
14 #include "sanitizer_common/sanitizer_libc.h"
15 #include "gtest/gtest.h"
16
17 #include <string.h>
18 #include <limits.h>
19
20 namespace __sanitizer {
21
TEST(Printf,Basic)22 TEST(Printf, Basic) {
23 char buf[1024];
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));
31
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);
36 }
37
TEST(Printf,OverflowStr)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);
43 EXPECT_EQ(buf[3], 0);
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');
49 EXPECT_EQ(buf[9], 0);
50 }
51
TEST(Printf,OverflowInt)52 TEST(Printf, OverflowInt) {
53 char buf[] = "123456789";
54 internal_snprintf(buf, 4, "%d", -123456789); // NOLINT
55 EXPECT_STREQ("-12", buf);
56 EXPECT_EQ(buf[3], 0);
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');
62 EXPECT_EQ(buf[9], 0);
63 }
64
TEST(Printf,OverflowUint)65 TEST(Printf, OverflowUint) {
66 char buf[] = "123456789";
67 uptr val;
68 if (sizeof(val) == 4) {
69 val = (uptr)0x12345678;
70 } else {
71 val = (uptr)0x123456789ULL;
72 }
73 internal_snprintf(buf, 4, "a%zx", val); // NOLINT
74 EXPECT_STREQ("a12", buf);
75 EXPECT_EQ(buf[3], 0);
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');
81 EXPECT_EQ(buf[9], 0);
82 }
83
TEST(Printf,OverflowPtr)84 TEST(Printf, OverflowPtr) {
85 char buf[] = "123456789";
86 void *p;
87 if (sizeof(p) == 4) {
88 p = (void*)0x1234567;
89 } else {
90 p = (void*)0x123456789ULL;
91 }
92 internal_snprintf(buf, 4, "%p", p); // NOLINT
93 EXPECT_STREQ("0x0", buf);
94 EXPECT_EQ(buf[3], 0);
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);
101 }
102
103 #if defined(_WIN32)
104 // Oh well, MSVS headers don't define snprintf.
105 # define snprintf _snprintf
106 #endif
107
108 template<typename T>
TestAgainstLibc(const char * fmt,T arg1,T arg2)109 static void TestAgainstLibc(const char *fmt, T arg1, T arg2) {
110 char buf[1024];
111 uptr len = internal_snprintf(buf, sizeof(buf), fmt, arg1, arg2);
112 char buf2[1024];
113 snprintf(buf2, sizeof(buf2), fmt, arg1, arg2);
114 EXPECT_EQ(len, strlen(buf));
115 EXPECT_STREQ(buf2, buf);
116 }
117
TEST(Printf,MinMax)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
122 #if !defined(_WIN32)
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
127 #endif
128 }
129
TEST(Printf,Padding)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);
141 }
142
TEST(Printf,Precision)143 TEST(Printf, Precision) {
144 char buf[1024];
145 uptr len = internal_snprintf(buf, sizeof(buf), "%.*s", 3, "12345");
146 EXPECT_EQ(3U, len);
147 EXPECT_STREQ("123", buf);
148 len = internal_snprintf(buf, sizeof(buf), "%.*s", 6, "12345");
149 EXPECT_EQ(5U, len);
150 EXPECT_STREQ("12345", buf);
151 }
152
153 } // namespace __sanitizer
154