1 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify=kprintf,nonkprintf,expected
2 // RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify=kprintf,nonkprintf,expected
3 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 -Wno-format-truncation -Wno-format-overflow %s -verify
4 // RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 -Wno-format-truncation -Wno-format-overflow %s -verify
5 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 -Wno-format-truncation-non-kprintf -Wno-format-overflow-non-kprintf %s -verify=kprintf,expected
6 // RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 -Wno-format-truncation-non-kprintf -Wno-format-overflow-non-kprintf %s -verify=kprintf,expected
7 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 -Wno-format-overflow -Wno-format-truncation -Wformat-truncation-non-kprintf -Wformat-overflow-non-kprintf %s -verify=nonkprintf,expected
8 // RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 -Wno-format-overflow -Wno-format-truncation -Wformat-truncation-non-kprintf -Wformat-overflow-non-kprintf %s -verify=nonkprintf,expected
10 typedef unsigned long size_t;
16 extern int sprintf(char *str
, const char *format
, ...);
22 void call_snprintf(double d
, int n
, int *ptr
) {
24 __builtin_snprintf(buf
, 10, "merp");
25 __builtin_snprintf(buf
, 11, "merp"); // expected-warning {{'snprintf' size argument is too large; destination buffer has size 10, but size argument is 11}}
26 __builtin_snprintf(buf
, 12, "%#12x", n
); // kprintf-warning {{'snprintf' will always be truncated; specified size is 12, but format string expands to at least 13}} \
27 // expected-warning {{'snprintf' size argument is too large; destination buffer has size 10, but size argument is 12}}
28 __builtin_snprintf(buf
, 0, "merp");
29 __builtin_snprintf(buf
, 3, "merp"); // kprintf-warning {{'snprintf' will always be truncated; specified size is 3, but format string expands to at least 5}}
30 __builtin_snprintf(buf
, 4, "merp"); // kprintf-warning {{'snprintf' will always be truncated; specified size is 4, but format string expands to at least 5}}
31 __builtin_snprintf(buf
, 5, "merp");
32 __builtin_snprintf(buf
, 1, "%.1000g", d
); // kprintf-warning {{'snprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
33 __builtin_snprintf(buf
, 5, "%.1000g", d
);
34 __builtin_snprintf(buf
, 5, "%.1000G", d
);
35 __builtin_snprintf(buf
, 10, " %#08x", n
);
36 __builtin_snprintf(buf
, 2, "%#x", n
);
37 __builtin_snprintf(buf
, 2, "%#X", n
);
38 __builtin_snprintf(buf
, 2, "%#o", n
);
39 __builtin_snprintf(buf
, 1, "%#x", n
); // kprintf-warning {{'snprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
40 __builtin_snprintf(buf
, 1, "%#X", n
); // kprintf-warning {{'snprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
41 __builtin_snprintf(buf
, 1, "%#o", n
); // kprintf-warning {{'snprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
43 __builtin_snprintf(node_name
, sizeof(node_name
), "%pOFn", ptr
); // nonkprintf-warning {{'snprintf' will always be truncated; specified size is 6, but format string expands to at least 7}}
44 __builtin_snprintf(node_name
, sizeof(node_name
), "12345%pOFn", ptr
); // nonkprintf-warning {{'snprintf' will always be truncated; specified size is 6, but format string expands to at least 12}}
45 __builtin_snprintf(node_name
, sizeof(node_name
), "123456%pOFn", ptr
); // nonkprintf-warning {{'snprintf' will always be truncated; specified size is 6, but format string expands to at least 13}}
48 void call_vsnprintf(void) {
50 __builtin_va_list list
;
51 __builtin_vsnprintf(buf
, 10, "merp", list
);
52 __builtin_vsnprintf(buf
, 11, "merp", list
); // expected-warning {{'vsnprintf' size argument is too large; destination buffer has size 10, but size argument is 11}}
53 __builtin_vsnprintf(buf
, 0, "merp", list
);
54 __builtin_vsnprintf(buf
, 3, "merp", list
); // kprintf-warning {{'vsnprintf' will always be truncated; specified size is 3, but format string expands to at least 5}}
55 __builtin_vsnprintf(buf
, 4, "merp", list
); // kprintf-warning {{'vsnprintf' will always be truncated; specified size is 4, but format string expands to at least 5}}
56 __builtin_vsnprintf(buf
, 5, "merp", list
);
57 __builtin_vsnprintf(buf
, 1, "%.1000g", list
); // kprintf-warning {{'vsnprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
58 __builtin_vsnprintf(buf
, 5, "%.1000g", list
);
59 __builtin_vsnprintf(buf
, 5, "%.1000G", list
);
60 __builtin_vsnprintf(buf
, 10, " %#08x", list
);
61 __builtin_vsnprintf(buf
, 2, "%#x", list
);
62 __builtin_vsnprintf(buf
, 2, "%#X", list
);
63 __builtin_vsnprintf(buf
, 2, "%#o", list
);
64 __builtin_vsnprintf(buf
, 1, "%#x", list
); // kprintf-warning {{'vsnprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
65 __builtin_vsnprintf(buf
, 1, "%#X", list
); // kprintf-warning {{'vsnprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
66 __builtin_vsnprintf(buf
, 1, "%#o", list
); // kprintf-warning {{'vsnprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
68 __builtin_snprintf(node_name
, sizeof(node_name
), "%pOFn", list
); // nonkprintf-warning {{'snprintf' will always be truncated; specified size is 6, but format string expands to at least 7}}
69 __builtin_snprintf(node_name
, sizeof(node_name
), "12345%pOFn", list
); // nonkprintf-warning {{'snprintf' will always be truncated; specified size is 6, but format string expands to at least 12}}
70 __builtin_snprintf(node_name
, sizeof(node_name
), "123456%pOFn", list
); // nonkprintf-warning {{'snprintf' will always be truncated; specified size is 6, but format string expands to at least 13}}
73 void call_sprintf_chk(char *buf
) {
74 __builtin___sprintf_chk(buf
, 1, 6, "hell\n");
75 __builtin___sprintf_chk(buf
, 1, 5, "hell\n"); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 5, but format string expands to at least 6}}
76 __builtin___sprintf_chk(buf
, 1, 6, "hell\0 boy"); // expected-warning {{format string contains '\0' within the string body}}
77 __builtin___sprintf_chk(buf
, 1, 2, "hell\0 boy"); // expected-warning {{format string contains '\0' within the string body}} \
78 // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 2, but format string expands to at least 5}}
79 __builtin___sprintf_chk(buf
, 1, 6, "hello");
80 __builtin___sprintf_chk(buf
, 1, 5, "hello"); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 5, but format string expands to at least 6}}
81 __builtin___sprintf_chk(buf
, 1, 2, "%c", '9');
82 __builtin___sprintf_chk(buf
, 1, 1, "%c", '9'); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
83 __builtin___sprintf_chk(buf
, 1, 2, "%d", 9);
84 __builtin___sprintf_chk(buf
, 1, 1, "%d", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
85 __builtin___sprintf_chk(buf
, 1, 2, "%i", 9);
86 __builtin___sprintf_chk(buf
, 1, 1, "%i", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
87 __builtin___sprintf_chk(buf
, 1, 2, "%o", 9);
88 __builtin___sprintf_chk(buf
, 1, 1, "%o", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
89 __builtin___sprintf_chk(buf
, 1, 2, "%u", 9);
90 __builtin___sprintf_chk(buf
, 1, 1, "%u", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
91 __builtin___sprintf_chk(buf
, 1, 2, "%x", 9);
92 __builtin___sprintf_chk(buf
, 1, 1, "%x", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
93 __builtin___sprintf_chk(buf
, 1, 2, "%X", 9);
94 __builtin___sprintf_chk(buf
, 1, 1, "%X", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
95 __builtin___sprintf_chk(buf
, 1, 2, "%hhd", (char)9);
96 __builtin___sprintf_chk(buf
, 1, 1, "%hhd", (char)9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
97 __builtin___sprintf_chk(buf
, 1, 2, "%hd", (short)9);
98 __builtin___sprintf_chk(buf
, 1, 1, "%hd", (short)9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
99 __builtin___sprintf_chk(buf
, 1, 2, "%ld", 9l);
100 __builtin___sprintf_chk(buf
, 1, 1, "%ld", 9l); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
101 __builtin___sprintf_chk(buf
, 1, 2, "%lld", 9ll);
102 __builtin___sprintf_chk(buf
, 1, 1, "%lld", 9ll); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
103 __builtin___sprintf_chk(buf
, 1, 2, "%%");
104 __builtin___sprintf_chk(buf
, 1, 1, "%%"); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
105 __builtin___sprintf_chk(buf
, 1, 4, "%#x", 9);
106 __builtin___sprintf_chk(buf
, 1, 3, "%#x", 9);
107 __builtin___sprintf_chk(buf
, 1, 4, "%p", (void *)9);
108 __builtin___sprintf_chk(buf
, 1, 3, "%p", (void *)9); // nonkprintf-warning {{'sprintf' will always overflow; destination buffer has size 3, but format string expands to at least 4}}
109 __builtin___sprintf_chk(buf
, 1, 3, "%+d", 9);
110 __builtin___sprintf_chk(buf
, 1, 2, "%+d", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 2, but format string expands to at least 3}}
111 __builtin___sprintf_chk(buf
, 1, 3, "% i", 9);
112 __builtin___sprintf_chk(buf
, 1, 2, "% i", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 2, but format string expands to at least 3}}
113 __builtin___sprintf_chk(buf
, 1, 6, "%5d", 9);
114 __builtin___sprintf_chk(buf
, 1, 5, "%5d", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 5, but format string expands to at least 6}}
115 __builtin___sprintf_chk(buf
, 1, 9, "%f", 9.f
);
116 __builtin___sprintf_chk(buf
, 1, 8, "%f", 9.f
); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 8, but format string expands to at least 9}}
117 __builtin___sprintf_chk(buf
, 1, 9, "%Lf", (long double)9.);
118 __builtin___sprintf_chk(buf
, 1, 8, "%Lf", (long double)9.); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 8, but format string expands to at least 9}}
119 __builtin___sprintf_chk(buf
, 1, 10, "%+f", 9.f
);
120 __builtin___sprintf_chk(buf
, 1, 9, "%+f", 9.f
); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 9, but format string expands to at least 10}}
121 __builtin___sprintf_chk(buf
, 1, 12, "%e", 9.f
);
122 __builtin___sprintf_chk(buf
, 1, 11, "%e", 9.f
); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 11, but format string expands to at least 12}}
125 void call_sprintf(void) {
127 sprintf(buf
, "hell\0 boy"); // expected-warning {{format string contains '\0' within the string body}}
128 sprintf(buf
, "hello b\0y"); // expected-warning {{format string contains '\0' within the string body}} \
129 // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 8}}
130 sprintf(buf
, "hello");
131 sprintf(buf
, "hello!"); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
132 sprintf(buf
, "1234%%");
133 sprintf(buf
, "12345%%"); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
134 sprintf(buf
, "1234%c", '9');
135 sprintf(buf
, "12345%c", '9'); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
136 sprintf(buf
, "1234%d", 9);
137 sprintf(buf
, "12345%d", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
138 sprintf(buf
, "1234%lld", 9ll);
139 sprintf(buf
, "12345%lld", 9ll); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
140 sprintf(buf
, "12%#x", 9);
141 sprintf(buf
, "123%#x", 9);
142 sprintf(buf
, "12%p", (void *)9);
143 sprintf(buf
, "123%p", (void *)9); // nonkprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
144 sprintf(buf
, "123%+d", 9);
145 sprintf(buf
, "1234%+d", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
146 sprintf(buf
, "123% i", 9);
147 sprintf(buf
, "1234% i", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
148 sprintf(buf
, "%5d", 9);
149 sprintf(buf
, "1%5d", 9); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
150 sprintf(buf
, "%.3f", 9.f
);
151 sprintf(buf
, "5%.3f", 9.f
); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
152 sprintf(buf
, "%+.2f", 9.f
);
153 sprintf(buf
, "%+.3f", 9.f
); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
154 sprintf(buf
, "%.0e", 9.f
);
155 sprintf(buf
, "5%.1e", 9.f
); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 8}}