1 //===-- sanitizer_printf_test.cpp -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Tests for sanitizer_printf.cpp
10 //
11 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common/sanitizer_common.h"
13 #include "sanitizer_common/sanitizer_libc.h"
14 #include "gtest/gtest.h"
15 
16 #include <string.h>
17 #include <limits.h>
18 
19 namespace __sanitizer {
20 
21 TEST(Printf, Basic) {
22   char buf[1024];
23   uptr len = internal_snprintf(buf, sizeof(buf),
24       "a%db%zdc%ue%zuf%xh%zxq%pe%sr",
25       (int)-1, (uptr)-2, // NOLINT
26       (unsigned)-4, (uptr)5, // NOLINT
27       (unsigned)10, (uptr)11, // NOLINT
28       (void*)0x123, "_string_");
29   EXPECT_EQ(len, strlen(buf));
30 
31   std::string expectedString = "a-1b-2c4294967292e5fahbq0x";
32   expectedString += std::string(SANITIZER_POINTER_FORMAT_LENGTH - 3, '0');
33   expectedString += "123e_string_r";
34   EXPECT_STREQ(expectedString.c_str(), buf);
35 }
36 
37 TEST(Printf, OverflowStr) {
38   char buf[] = "123456789";
39   uptr len = internal_snprintf(buf, 4, "%s", "abcdef");  // NOLINT
40   EXPECT_EQ(len, (uptr)6);
41   EXPECT_STREQ("abc", buf);
42   EXPECT_EQ(buf[3], 0);
43   EXPECT_EQ(buf[4], '5');
44   EXPECT_EQ(buf[5], '6');
45   EXPECT_EQ(buf[6], '7');
46   EXPECT_EQ(buf[7], '8');
47   EXPECT_EQ(buf[8], '9');
48   EXPECT_EQ(buf[9], 0);
49 }
50 
51 TEST(Printf, OverflowInt) {
52   char buf[] = "123456789";
53   internal_snprintf(buf, 4, "%d", -123456789);  // NOLINT
54   EXPECT_STREQ("-12", buf);
55   EXPECT_EQ(buf[3], 0);
56   EXPECT_EQ(buf[4], '5');
57   EXPECT_EQ(buf[5], '6');
58   EXPECT_EQ(buf[6], '7');
59   EXPECT_EQ(buf[7], '8');
60   EXPECT_EQ(buf[8], '9');
61   EXPECT_EQ(buf[9], 0);
62 }
63 
64 TEST(Printf, OverflowUint) {
65   char buf[] = "123456789";
66   uptr val;
67   if (sizeof(val) == 4) {
68     val = (uptr)0x12345678;
69   } else {
70     val = (uptr)0x123456789ULL;
71   }
72   internal_snprintf(buf, 4, "a%zx", val);  // NOLINT
73   EXPECT_STREQ("a12", buf);
74   EXPECT_EQ(buf[3], 0);
75   EXPECT_EQ(buf[4], '5');
76   EXPECT_EQ(buf[5], '6');
77   EXPECT_EQ(buf[6], '7');
78   EXPECT_EQ(buf[7], '8');
79   EXPECT_EQ(buf[8], '9');
80   EXPECT_EQ(buf[9], 0);
81 }
82 
83 TEST(Printf, OverflowPtr) {
84   char buf[] = "123456789";
85   void *p;
86   if (sizeof(p) == 4) {
87     p = (void*)0x1234567;
88   } else {
89     p = (void*)0x123456789ULL;
90   }
91   internal_snprintf(buf, 4, "%p", p);  // NOLINT
92   EXPECT_STREQ("0x0", buf);
93   EXPECT_EQ(buf[3], 0);
94   EXPECT_EQ(buf[4], '5');
95   EXPECT_EQ(buf[5], '6');
96   EXPECT_EQ(buf[6], '7');
97   EXPECT_EQ(buf[7], '8');
98   EXPECT_EQ(buf[8], '9');
99   EXPECT_EQ(buf[9], 0);
100 }
101 
102 #if defined(_WIN32)
103 // Oh well, MSVS headers don't define snprintf.
104 # define snprintf _snprintf
105 #endif
106 
107 template<typename T>
108 static void TestAgainstLibc(const char *fmt, T arg1, T arg2) {
109   char buf[1024];
110   uptr len = internal_snprintf(buf, sizeof(buf), fmt, arg1, arg2);
111   char buf2[1024];
112   snprintf(buf2, sizeof(buf2), fmt, arg1, arg2);
113   EXPECT_EQ(len, strlen(buf));
114   EXPECT_STREQ(buf2, buf);
115 }
116 
117 TEST(Printf, MinMax) {
118   TestAgainstLibc<int>("%d-%d", INT_MIN, INT_MAX);  // NOLINT
119   TestAgainstLibc<unsigned>("%u-%u", 0, UINT_MAX);  // NOLINT
120   TestAgainstLibc<unsigned>("%x-%x", 0, UINT_MAX);  // NOLINT
121 #if !defined(_WIN32)
122   // %z* format doesn't seem to be supported by MSVS.
123   TestAgainstLibc<long>("%zd-%zd", LONG_MIN, LONG_MAX);  // NOLINT
124   TestAgainstLibc<unsigned long>("%zu-%zu", 0, ULONG_MAX);  // NOLINT
125   TestAgainstLibc<unsigned long>("%zx-%zx", 0, ULONG_MAX);  // NOLINT
126 #endif
127 }
128 
129 TEST(Printf, Padding) {
130   TestAgainstLibc<int>("%3d - %3d", 1, 0);
131   TestAgainstLibc<int>("%3d - %3d", -1, 123);
132   TestAgainstLibc<int>("%3d - %3d", -1, -123);
133   TestAgainstLibc<int>("%3d - %3d", 12, 1234);
134   TestAgainstLibc<int>("%3d - %3d", -12, -1234);
135   TestAgainstLibc<int>("%03d - %03d", 1, 0);
136   TestAgainstLibc<int>("%03d - %03d", -1, 123);
137   TestAgainstLibc<int>("%03d - %03d", -1, -123);
138   TestAgainstLibc<int>("%03d - %03d", 12, 1234);
139   TestAgainstLibc<int>("%03d - %03d", -12, -1234);
140 }
141 
142 TEST(Printf, Precision) {
143   char buf[1024];
144   uptr len = internal_snprintf(buf, sizeof(buf), "%.*s", 3, "12345");
145   EXPECT_EQ(3U, len);
146   EXPECT_STREQ("123", buf);
147   len = internal_snprintf(buf, sizeof(buf), "%.*s", 6, "12345");
148   EXPECT_EQ(5U, len);
149   EXPECT_STREQ("12345", buf);
150   len = internal_snprintf(buf, sizeof(buf), "%-6s", "12345");
151   EXPECT_EQ(6U, len);
152   EXPECT_STREQ("12345 ", buf);
153   // Check that width does not overflow the smaller buffer, although
154   // 10 chars is requested, it stops at the buffer size, 8.
155   len = internal_snprintf(buf, 8, "%-10s", "12345");
156   EXPECT_EQ(10U, len);  // The required size reported.
157   EXPECT_STREQ("12345  ", buf);
158 }
159 
160 }  // namespace __sanitizer
161