format-strings.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -isystem %S/Inputs %s
  2. // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -isystem %S/Inputs -fno-signed-char %s
  3. #include <stdarg.h>
  4. #include <stddef.h>
  5. #define __need_wint_t
  6. #include <stddef.h> // For wint_t and wchar_t
  7. typedef struct _FILE FILE;
  8. int fprintf(FILE *, const char *restrict, ...);
  9. int printf(const char *restrict, ...); // expected-note{{passing argument to parameter here}}
  10. int snprintf(char *restrict, size_t, const char *restrict, ...);
  11. int sprintf(char *restrict, const char *restrict, ...);
  12. int vasprintf(char **, const char *, va_list);
  13. int asprintf(char **, const char *, ...);
  14. int vfprintf(FILE *, const char *restrict, va_list);
  15. int vprintf(const char *restrict, va_list);
  16. int vsnprintf(char *, size_t, const char *, va_list);
  17. int vsprintf(char *restrict, const char *restrict, va_list); // expected-note{{passing argument to parameter here}}
  18. int vscanf(const char *restrict format, va_list arg);
  19. char * global_fmt;
  20. void check_string_literal( FILE* fp, const char* s, char *buf, ... ) {
  21. char * b;
  22. va_list ap;
  23. va_start(ap,buf);
  24. printf(s); // expected-warning {{format string is not a string literal}}
  25. vprintf(s,ap); // expected-warning {{format string is not a string literal}}
  26. fprintf(fp,s); // expected-warning {{format string is not a string literal}}
  27. vfprintf(fp,s,ap); // expected-warning {{format string is not a string literal}}
  28. asprintf(&b,s); // expected-warning {{format string is not a string lit}}
  29. vasprintf(&b,s,ap); // expected-warning {{format string is not a string literal}}
  30. sprintf(buf,s); // expected-warning {{format string is not a string literal}}
  31. snprintf(buf,2,s); // expected-warning {{format string is not a string lit}}
  32. __builtin___sprintf_chk(buf,0,-1,s); // expected-warning {{format string is not a string literal}}
  33. __builtin___snprintf_chk(buf,2,0,-1,s); // expected-warning {{format string is not a string lit}}
  34. vsprintf(buf,s,ap); // expected-warning {{format string is not a string lit}}
  35. vsnprintf(buf,2,s,ap); // expected-warning {{format string is not a string lit}}
  36. vsnprintf(buf,2,global_fmt,ap); // expected-warning {{format string is not a string literal}}
  37. __builtin___vsnprintf_chk(buf,2,0,-1,s,ap); // expected-warning {{format string is not a string lit}}
  38. __builtin___vsnprintf_chk(buf,2,0,-1,global_fmt,ap); // expected-warning {{format string is not a string literal}}
  39. vscanf(s, ap); // expected-warning {{format string is not a string literal}}
  40. // rdar://6079877
  41. printf("abc"
  42. "%*d", 1, 1); // no-warning
  43. printf("abc\
  44. def"
  45. "%*d", 1, 1); // no-warning
  46. // <rdar://problem/6079850>, allow 'unsigned' (instead of 'int') to be used for both
  47. // the field width and precision. This deviates from C99, but is reasonably safe
  48. // and is also accepted by GCC.
  49. printf("%*d", (unsigned) 1, 1); // no-warning
  50. }
  51. // When calling a non-variadic format function (vprintf, vscanf, NSLogv, ...),
  52. // warn only if the format string argument is a parameter that is not itself
  53. // declared as a format string with compatible format.
  54. __attribute__((__format__ (__printf__, 2, 4)))
  55. void check_string_literal2( FILE* fp, const char* s, char *buf, ... ) {
  56. char * b;
  57. va_list ap;
  58. va_start(ap,buf);
  59. printf(s); // expected-warning {{format string is not a string literal}}
  60. vprintf(s,ap); // no-warning
  61. fprintf(fp,s); // expected-warning {{format string is not a string literal}}
  62. vfprintf(fp,s,ap); // no-warning
  63. asprintf(&b,s); // expected-warning {{format string is not a string lit}}
  64. vasprintf(&b,s,ap); // no-warning
  65. sprintf(buf,s); // expected-warning {{format string is not a string literal}}
  66. snprintf(buf,2,s); // expected-warning {{format string is not a string lit}}
  67. __builtin___vsnprintf_chk(buf,2,0,-1,s,ap); // no-warning
  68. vscanf(s, ap); // expected-warning {{format string is not a string literal}}
  69. }
  70. void check_conditional_literal(const char* s, int i) {
  71. printf(i == 1 ? "yes" : "no"); // no-warning
  72. printf(i == 0 ? (i == 1 ? "yes" : "no") : "dont know"); // no-warning
  73. printf(i == 0 ? (i == 1 ? s : "no") : "dont know"); // expected-warning{{format string is not a string literal}}
  74. printf("yes" ?: "no %d", 1); // expected-warning{{data argument not used by format string}}
  75. }
  76. void check_writeback_specifier()
  77. {
  78. int x;
  79. char *b;
  80. printf("%n", b); // expected-warning{{format specifies type 'int *' but the argument has type 'char *'}}
  81. printf("%n", &x); // no-warning
  82. printf("%hhn", (signed char*)0); // no-warning
  83. printf("%hhn", (char*)0); // no-warning
  84. printf("%hhn", (unsigned char*)0); // no-warning
  85. printf("%hhn", (int*)0); // expected-warning{{format specifies type 'signed char *' but the argument has type 'int *'}}
  86. printf("%hn", (short*)0); // no-warning
  87. printf("%hn", (unsigned short*)0); // no-warning
  88. printf("%hn", (int*)0); // expected-warning{{format specifies type 'short *' but the argument has type 'int *'}}
  89. printf("%n", (int*)0); // no-warning
  90. printf("%n", (unsigned int*)0); // no-warning
  91. printf("%n", (char*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'char *'}}
  92. printf("%ln", (long*)0); // no-warning
  93. printf("%ln", (unsigned long*)0); // no-warning
  94. printf("%ln", (int*)0); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
  95. printf("%lln", (long long*)0); // no-warning
  96. printf("%lln", (unsigned long long*)0); // no-warning
  97. printf("%lln", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
  98. printf("%qn", (long long*)0); // no-warning
  99. printf("%qn", (unsigned long long*)0); // no-warning
  100. printf("%qn", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
  101. printf("%Ln", 0); // expected-warning{{length modifier 'L' results in undefined behavior or no effect with 'n' conversion specifier}}
  102. // expected-note@-1{{did you mean to use 'll'?}}
  103. }
  104. void check_invalid_specifier(FILE* fp, char *buf)
  105. {
  106. printf("%s%lb%d","unix",10,20); // expected-warning {{invalid conversion specifier 'b'}}
  107. fprintf(fp,"%%%l"); // expected-warning {{incomplete format specifier}}
  108. sprintf(buf,"%%%%%ld%d%d", 1, 2, 3); // expected-warning{{format specifies type 'long' but the argument has type 'int'}}
  109. snprintf(buf, 2, "%%%%%ld%;%d", 1, 2, 3); // expected-warning{{format specifies type 'long' but the argument has type 'int'}} expected-warning {{invalid conversion specifier ';'}}
  110. }
  111. void check_null_char_string(char* b)
  112. {
  113. printf("\0this is bogus%d",1); // expected-warning {{string contains '\0'}}
  114. snprintf(b,10,"%%%%%d\0%d",1,2); // expected-warning {{string contains '\0'}}
  115. printf("%\0d",1); // expected-warning {{string contains '\0'}}
  116. }
  117. void check_empty_format_string(char* buf, ...)
  118. {
  119. va_list ap;
  120. va_start(ap,buf);
  121. vprintf("",ap); // expected-warning {{format string is empty}}
  122. sprintf(buf, "", 1); // expected-warning {{format string is empty}}
  123. // Don't warn about empty format strings when there are no data arguments.
  124. // This can arise from macro expansions and non-standard format string
  125. // functions.
  126. sprintf(buf, ""); // no-warning
  127. }
  128. void check_wide_string(char* b, ...)
  129. {
  130. va_list ap;
  131. va_start(ap,b);
  132. printf(L"foo %d",2); // expected-warning {{incompatible pointer types}}, expected-warning {{should not be a wide string}}
  133. vsprintf(b,L"bar %d",ap); // expected-warning {{incompatible pointer types}}, expected-warning {{should not be a wide string}}
  134. }
  135. void check_asterisk_precision_width(int x) {
  136. printf("%*d"); // expected-warning {{'*' specified field width is missing a matching 'int' argument}}
  137. printf("%.*d"); // expected-warning {{'.*' specified field precision is missing a matching 'int' argument}}
  138. printf("%*d",12,x); // no-warning
  139. printf("%*d","foo",x); // expected-warning {{field width should have type 'int', but argument has type 'char *'}}
  140. printf("%.*d","foo",x); // expected-warning {{field precision should have type 'int', but argument has type 'char *'}}
  141. }
  142. void __attribute__((format(printf,1,3))) myprintf(const char*, int blah, ...);
  143. void test_myprintf() {
  144. myprintf("%d", 17, 18); // okay
  145. }
  146. void test_constant_bindings(void) {
  147. const char * const s1 = "hello";
  148. const char s2[] = "hello";
  149. const char *s3 = "hello";
  150. char * const s4 = "hello";
  151. extern const char s5[];
  152. printf(s1); // no-warning
  153. printf(s2); // no-warning
  154. printf(s3); // expected-warning{{not a string literal}}
  155. printf(s4); // expected-warning{{not a string literal}}
  156. printf(s5); // expected-warning{{not a string literal}}
  157. }
  158. // Test what happens when -Wformat-security only.
  159. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  160. #pragma GCC diagnostic warning "-Wformat-security"
  161. void test9(char *P) {
  162. int x;
  163. printf(P); // expected-warning {{format string is not a string literal (potentially insecure)}}
  164. printf(P, 42);
  165. }
  166. void torture(va_list v8) {
  167. vprintf ("%*.*d", v8); // no-warning
  168. }
  169. void test10(int x, float f, int i, long long lli) {
  170. printf("%s"); // expected-warning{{more '%' conversions than data arguments}}
  171. printf("%@", 12); // expected-warning{{invalid conversion specifier '@'}}
  172. printf("\0"); // expected-warning{{format string contains '\0' within the string body}}
  173. printf("xs\0"); // expected-warning{{format string contains '\0' within the string body}}
  174. printf("%*d\n"); // expected-warning{{'*' specified field width is missing a matching 'int' argument}}
  175. printf("%*.*d\n", x); // expected-warning{{'.*' specified field precision is missing a matching 'int' argument}}
  176. printf("%*d\n", f, x); // expected-warning{{field width should have type 'int', but argument has type 'double'}}
  177. printf("%*.*d\n", x, f, x); // expected-warning{{field precision should have type 'int', but argument has type 'double'}}
  178. printf("%**\n"); // expected-warning{{invalid conversion specifier '*'}}
  179. printf("%d%d\n", x); // expected-warning{{more '%' conversions than data arguments}}
  180. printf("%d\n", x, x); // expected-warning{{data argument not used by format string}}
  181. printf("%W%d\n", x, x); // expected-warning{{invalid conversion specifier 'W'}}
  182. printf("%"); // expected-warning{{incomplete format specifier}}
  183. printf("%.d", x); // no-warning
  184. printf("%.", x); // expected-warning{{incomplete format specifier}}
  185. printf("%f", 4); // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
  186. printf("%qd", lli); // no-warning
  187. printf("%qd", x); // expected-warning{{format specifies type 'long long' but the argument has type 'int'}}
  188. printf("%qp", (void *)0); // expected-warning{{length modifier 'q' results in undefined behavior or no effect with 'p' conversion specifier}}
  189. printf("hhX %hhX", (unsigned char)10); // no-warning
  190. printf("llX %llX", (long long) 10); // no-warning
  191. // This is fine, because there is an implicit conversion to an int.
  192. printf("%d", (unsigned char) 10); // no-warning
  193. printf("%d", (long long) 10); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
  194. printf("%Lf\n", (long double) 1.0); // no-warning
  195. printf("%f\n", (long double) 1.0); // expected-warning{{format specifies type 'double' but the argument has type 'long double'}}
  196. // The man page says that a zero precision is okay.
  197. printf("%.0Lf", (long double) 1.0); // no-warning
  198. printf("%c\n", "x"); // expected-warning{{format specifies type 'int' but the argument has type 'char *'}}
  199. printf("%c\n", 1.23); // expected-warning{{format specifies type 'int' but the argument has type 'double'}}
  200. printf("Format %d, is %! %f", 1, 2, 4.4); // expected-warning{{invalid conversion specifier '!'}}
  201. }
  202. typedef unsigned char uint8_t;
  203. void should_understand_small_integers() {
  204. printf("%hhu", (short) 10); // expected-warning{{format specifies type 'unsigned char' but the argument has type 'short'}}
  205. printf("%hu\n", (unsigned char) 1); // expected-warning{{format specifies type 'unsigned short' but the argument has type 'unsigned char'}}
  206. printf("%hu\n", (uint8_t)1); // expected-warning{{format specifies type 'unsigned short' but the argument has type 'uint8_t'}}
  207. }
  208. void test11(void *p, char *s) {
  209. printf("%p", p); // no-warning
  210. printf("%p", 123); // expected-warning{{format specifies type 'void *' but the argument has type 'int'}}
  211. printf("%.4p", p); // expected-warning{{precision used with 'p' conversion specifier, resulting in undefined behavior}}
  212. printf("%+p", p); // expected-warning{{flag '+' results in undefined behavior with 'p' conversion specifier}}
  213. printf("% p", p); // expected-warning{{flag ' ' results in undefined behavior with 'p' conversion specifier}}
  214. printf("%0p", p); // expected-warning{{flag '0' results in undefined behavior with 'p' conversion specifier}}
  215. printf("%s", s); // no-warning
  216. printf("%+s", p); // expected-warning{{flag '+' results in undefined behavior with 's' conversion specifier}}
  217. printf("% s", p); // expected-warning{{flag ' ' results in undefined behavior with 's' conversion specifier}}
  218. printf("%0s", p); // expected-warning{{flag '0' results in undefined behavior with 's' conversion specifier}}
  219. }
  220. void test12(char *b) {
  221. unsigned char buf[4];
  222. printf ("%.4s\n", buf); // no-warning
  223. printf ("%.4s\n", &buf); // expected-warning{{format specifies type 'char *' but the argument has type 'unsigned char (*)[4]'}}
  224. // Verify that we are checking asprintf
  225. asprintf(&b, "%d", "asprintf"); // expected-warning{{format specifies type 'int' but the argument has type 'char *'}}
  226. }
  227. void test13(short x) {
  228. char bel = 007;
  229. printf("bel: '0%hhd'\n", bel); // no-warning
  230. printf("x: '0%hhd'\n", x); // expected-warning {{format specifies type 'char' but the argument has type 'short'}}
  231. }
  232. typedef struct __aslclient *aslclient;
  233. typedef struct __aslmsg *aslmsg;
  234. int asl_log(aslclient asl, aslmsg msg, int level, const char *format, ...) __attribute__((__format__ (__printf__, 4, 5)));
  235. void test_asl(aslclient asl) {
  236. // Test case from <rdar://problem/7341605>.
  237. asl_log(asl, 0, 3, "Error: %m"); // no-warning
  238. asl_log(asl, 0, 3, "Error: %W"); // expected-warning{{invalid conversion specifier 'W'}}
  239. }
  240. // <rdar://problem/7595366>
  241. typedef enum { A } int_t;
  242. void f0(int_t x) { printf("%d\n", x); }
  243. // Unicode test cases. These are possibly specific to Mac OS X. If so, they should
  244. // eventually be moved into a separate test.
  245. void test_unicode_conversions(wchar_t *s) {
  246. printf("%S", s); // no-warning
  247. printf("%s", s); // expected-warning{{format specifies type 'char *' but the argument has type 'wchar_t *'}}
  248. printf("%C", s[0]); // no-warning
  249. printf("%c", s[0]);
  250. // FIXME: This test reports inconsistent results. On Windows, '%C' expects
  251. // 'unsigned short'.
  252. // printf("%C", 10);
  253. printf("%S", "hello"); // expected-warning{{but the argument has type 'char *'}}
  254. }
  255. // Mac OS X supports positional arguments in format strings.
  256. // This is an IEEE extension (IEEE Std 1003.1).
  257. // FIXME: This is probably not portable everywhere.
  258. void test_positional_arguments() {
  259. printf("%0$", (int)2); // expected-warning{{position arguments in format strings start counting at 1 (not 0)}}
  260. printf("%1$*0$d", (int) 2); // expected-warning{{position arguments in format strings start counting at 1 (not 0)}}
  261. printf("%1$d", (int) 2); // no-warning
  262. printf("%1$d", (int) 2, 2); // expected-warning{{data argument not used by format string}}
  263. printf("%1$d%1$f", (int) 2); // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
  264. printf("%1$2.2d", (int) 2); // no-warning
  265. printf("%2$*1$.2d", (int) 2, (int) 3); // no-warning
  266. printf("%2$*8$d", (int) 2, (int) 3); // expected-warning{{specified field width is missing a matching 'int' argument}}
  267. printf("%%%1$d", (int) 2); // no-warning
  268. printf("%1$d%%", (int) 2); // no-warning
  269. }
  270. // PR 6697 - Handle format strings where the data argument is not adjacent to the format string
  271. void myprintf_PR_6697(const char *format, int x, ...) __attribute__((__format__(printf,1, 3)));
  272. void test_pr_6697() {
  273. myprintf_PR_6697("%s\n", 1, "foo"); // no-warning
  274. myprintf_PR_6697("%s\n", 1, (int)0); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
  275. // FIXME: Not everything should clearly support positional arguments,
  276. // but we need a way to identify those cases.
  277. myprintf_PR_6697("%1$s\n", 1, "foo"); // no-warning
  278. myprintf_PR_6697("%2$s\n", 1, "foo"); // expected-warning{{data argument position '2' exceeds the number of data arguments (1)}}
  279. myprintf_PR_6697("%18$s\n", 1, "foo"); // expected-warning{{data argument position '18' exceeds the number of data arguments (1)}}
  280. myprintf_PR_6697("%1$s\n", 1, (int) 0); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
  281. }
  282. void rdar8026030(FILE *fp) {
  283. fprintf(fp, "\%"); // expected-warning{{incomplete format specifier}}
  284. }
  285. void bug7377_bad_length_mod_usage() {
  286. // Bad length modifiers
  287. printf("%hhs", "foo"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
  288. printf("%1$zp", (void *)0); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}}
  289. printf("%ls", L"foo"); // no-warning
  290. printf("%#.2Lf", (long double)1.234); // no-warning
  291. // Bad flag usage
  292. printf("%#p", (void *) 0); // expected-warning{{flag '#' results in undefined behavior with 'p' conversion specifier}}
  293. printf("%0d", -1); // no-warning
  294. printf("%#n", (int *) 0); // expected-warning{{flag '#' results in undefined behavior with 'n' conversion specifier}}
  295. printf("%-n", (int *) 0); // expected-warning{{flag '-' results in undefined behavior with 'n' conversion specifier}}
  296. printf("%-p", (void *) 0); // no-warning
  297. // Bad optional amount use
  298. printf("%.2c", 'a'); // expected-warning{{precision used with 'c' conversion specifier, resulting in undefined behavior}}
  299. printf("%1n", (int *) 0); // expected-warning{{field width used with 'n' conversion specifier, resulting in undefined behavior}}
  300. printf("%.9n", (int *) 0); // expected-warning{{precision used with 'n' conversion specifier, resulting in undefined behavior}}
  301. // Ignored flags
  302. printf("% +f", 1.23); // expected-warning{{flag ' ' is ignored when flag '+' is present}}
  303. printf("%+ f", 1.23); // expected-warning{{flag ' ' is ignored when flag '+' is present}}
  304. printf("%0-f", 1.23); // expected-warning{{flag '0' is ignored when flag '-' is present}}
  305. printf("%-0f", 1.23); // expected-warning{{flag '0' is ignored when flag '-' is present}}
  306. printf("%-+f", 1.23); // no-warning
  307. }
  308. // PR 7981 - handle '%lc' (wint_t)
  309. void pr7981(wint_t c, wchar_t c2) {
  310. printf("%lc", c); // no-warning
  311. printf("%lc", 1.0); // expected-warning{{the argument has type 'double'}}
  312. printf("%lc", (char) 1); // no-warning
  313. printf("%lc", &c); // expected-warning{{the argument has type 'wint_t *'}}
  314. // If wint_t and wchar_t are the same width and wint_t is signed where
  315. // wchar_t is unsigned, an implicit conversion isn't possible.
  316. #if defined(__WINT_UNSIGNED__) || !defined(__WCHAR_UNSIGNED__) || \
  317. __WINT_WIDTH__ > __WCHAR_WIDTH__
  318. printf("%lc", c2); // no-warning
  319. #endif
  320. }
  321. // <rdar://problem/8269537> -Wformat-security says NULL is not a string literal
  322. void rdar8269537() {
  323. // This is likely to crash in most cases, but -Wformat-nonliteral technically
  324. // doesn't warn in this case.
  325. printf(0); // no-warning
  326. }
  327. // Handle functions with multiple format attributes.
  328. extern void rdar8332221_vprintf_scanf(const char *, va_list, const char *, ...)
  329. __attribute__((__format__(__printf__, 1, 0)))
  330. __attribute__((__format__(__scanf__, 3, 4)));
  331. void rdar8332221(va_list ap, int *x, long *y) {
  332. rdar8332221_vprintf_scanf("%", ap, "%d", x); // expected-warning{{incomplete format specifier}}
  333. }
  334. // PR8641
  335. void pr8641() {
  336. printf("%#x\n", 10);
  337. printf("%#X\n", 10);
  338. }
  339. void posix_extensions() {
  340. // Test %'d, "thousands grouping".
  341. // <rdar://problem/8816343>
  342. printf("%'d\n", 123456789); // no-warning
  343. printf("%'i\n", 123456789); // no-warning
  344. printf("%'f\n", (float) 1.0); // no-warning
  345. printf("%'p\n", (void*) 0); // expected-warning{{results in undefined behavior with 'p' conversion specifier}}
  346. }
  347. // PR8486
  348. //
  349. // Test what happens when -Wformat is on, but -Wformat-security is off.
  350. #pragma GCC diagnostic warning "-Wformat"
  351. #pragma GCC diagnostic ignored "-Wformat-security"
  352. void pr8486() {
  353. printf("%s", 1); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
  354. }
  355. // PR9314
  356. // Don't warn about string literals that are PreDefinedExprs, e.g. __func__.
  357. void pr9314() {
  358. printf(__PRETTY_FUNCTION__); // no-warning
  359. printf(__func__); // no-warning
  360. }
  361. int printf(const char * restrict, ...) __attribute__((__format__ (__printf__, 1, 2)));
  362. void rdar9612060(void) {
  363. printf("%s", 2); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
  364. }
  365. void check_char(unsigned char x, signed char y) {
  366. printf("%c", y); // no-warning
  367. printf("%hhu", x); // no-warning
  368. printf("%hhi", y); // no-warning
  369. printf("%hhi", x); // no-warning
  370. printf("%c", x); // no-warning
  371. printf("%hhu", y); // no-warning
  372. }
  373. // Test suppression of individual warnings.
  374. void test_suppress_invalid_specifier() {
  375. #pragma clang diagnostic push
  376. #pragma clang diagnostic ignored "-Wformat-invalid-specifier"
  377. printf("%@", 12); // no-warning
  378. #pragma clang diagnostic pop
  379. }
  380. // Make sure warnings are on for next test.
  381. #pragma GCC diagnostic warning "-Wformat"
  382. #pragma GCC diagnostic warning "-Wformat-security"
  383. // Test that the printf call site is where the warning is attached. If the
  384. // format string is somewhere else, point to it in a note.
  385. void pr9751() {
  386. const char kFormat1[] = "%d %d \n"; // expected-note{{format string is defined here}}}
  387. printf(kFormat1, 0); // expected-warning{{more '%' conversions than data arguments}}
  388. printf("%d %s\n", 0); // expected-warning{{more '%' conversions than data arguments}}
  389. const char kFormat2[] = "%18$s\n"; // expected-note{{format string is defined here}}
  390. printf(kFormat2, 1, "foo"); // expected-warning{{data argument position '18' exceeds the number of data arguments (2)}}
  391. printf("%18$s\n", 1, "foo"); // expected-warning{{data argument position '18' exceeds the number of data arguments (2)}}
  392. const char kFormat4[] = "%y"; // expected-note{{format string is defined here}}
  393. printf(kFormat4, 5); // expected-warning{{invalid conversion specifier 'y'}}
  394. printf("%y", 5); // expected-warning{{invalid conversion specifier 'y'}}
  395. const char kFormat5[] = "%."; // expected-note{{format string is defined here}}
  396. printf(kFormat5, 5); // expected-warning{{incomplete format specifier}}
  397. printf("%.", 5); // expected-warning{{incomplete format specifier}}
  398. const char kFormat6[] = "%s"; // expected-note{{format string is defined here}}
  399. printf(kFormat6, 5); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
  400. printf("%s", 5); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
  401. const char kFormat7[] = "%0$"; // expected-note{{format string is defined here}}
  402. printf(kFormat7, 5); // expected-warning{{position arguments in format strings start counting at 1 (not 0)}}
  403. printf("%0$", 5); // expected-warning{{position arguments in format strings start counting at 1 (not 0)}}
  404. const char kFormat8[] = "%1$d %d"; // expected-note{{format string is defined here}}
  405. printf(kFormat8, 4, 4); // expected-warning{{cannot mix positional and non-positional arguments in format string}}
  406. printf("%1$d %d", 4, 4); // expected-warning{{cannot mix positional and non-positional arguments in format string}}
  407. const char kFormat9[] = ""; // expected-note{{format string is defined here}}
  408. printf(kFormat9, 4, 4); // expected-warning{{format string is empty}}
  409. printf("", 4, 4); // expected-warning{{format string is empty}}
  410. const char kFormat10[] = "\0%d"; // expected-note{{format string is defined here}}
  411. printf(kFormat10, 4); // expected-warning{{format string contains '\0' within the string body}}
  412. printf("\0%d", 4); // expected-warning{{format string contains '\0' within the string body}}
  413. const char kFormat11[] = "%*d"; // expected-note{{format string is defined here}}
  414. printf(kFormat11); // expected-warning{{'*' specified field width is missing a matching 'int' argument}}
  415. printf("%*d"); // expected-warning{{'*' specified field width is missing a matching 'int' argument}}
  416. const char kFormat12[] = "%*d"; // expected-note{{format string is defined here}}
  417. printf(kFormat12, 4.4); // expected-warning{{field width should have type 'int', but argument has type 'double'}}
  418. printf("%*d", 4.4); // expected-warning{{field width should have type 'int', but argument has type 'double'}}
  419. const char kFormat13[] = "%.3p"; // expected-note{{format string is defined here}}
  420. void *p;
  421. printf(kFormat13, p); // expected-warning{{precision used with 'p' conversion specifier, resulting in undefined behavior}}
  422. printf("%.3p", p); // expected-warning{{precision used with 'p' conversion specifier, resulting in undefined behavior}}
  423. const char kFormat14[] = "%0s"; // expected-note{{format string is defined here}}
  424. printf(kFormat14, "a"); // expected-warning{{flag '0' results in undefined behavior with 's' conversion specifier}}
  425. printf("%0s", "a"); // expected-warning{{flag '0' results in undefined behavior with 's' conversion specifier}}
  426. const char kFormat15[] = "%hhs"; // expected-note{{format string is defined here}}
  427. printf(kFormat15, "a"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
  428. printf("%hhs", "a"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
  429. const char kFormat16[] = "%-0d"; // expected-note{{format string is defined here}}
  430. printf(kFormat16, 5); // expected-warning{{flag '0' is ignored when flag '-' is present}}
  431. printf("%-0d", 5); // expected-warning{{flag '0' is ignored when flag '-' is present}}
  432. // Make sure that the "format string is defined here" note is not emitted
  433. // when the original string is within the argument expression.
  434. printf(1 ? "yes %d" : "no %d"); // expected-warning 2{{more '%' conversions than data arguments}}
  435. const char kFormat17[] = "%hu"; // expected-note{{format string is defined here}}}
  436. printf(kFormat17, (int[]){0}); // expected-warning{{format specifies type 'unsigned short' but the argument}}
  437. printf("%a", (long double)0); // expected-warning{{format specifies type 'double' but the argument has type 'long double'}}
  438. // Test braced char[] initializers.
  439. const char kFormat18[] = { "%lld" }; // expected-note{{format string is defined here}}
  440. printf(kFormat18, 0); // expected-warning{{format specifies type}}
  441. // Make sure we point at the offending argument rather than the format string.
  442. const char kFormat19[] = "%d"; // expected-note{{format string is defined here}}
  443. printf(kFormat19,
  444. 0.0); // expected-warning{{format specifies}}
  445. }
  446. void pr18905() {
  447. const char s1[] = "s\0%s"; // expected-note{{format string is defined here}}
  448. const char s2[1] = "s"; // expected-note{{format string is defined here}}
  449. const char s3[2] = "s\0%s"; // expected-warning{{initializer-string for char array is too long}}
  450. const char s4[10] = "s";
  451. const char s5[0] = "%s"; // expected-warning{{initializer-string for char array is too long}}
  452. // expected-note@-1{{format string is defined here}}
  453. printf(s1); // expected-warning{{format string contains '\0' within the string body}}
  454. printf(s2); // expected-warning{{format string is not null-terminated}}
  455. printf(s3); // no-warning
  456. printf(s4); // no-warning
  457. printf(s5); // expected-warning{{format string is not null-terminated}}
  458. }
  459. void __attribute__((format(strfmon,1,2))) monformat(const char *fmt, ...);
  460. void __attribute__((format(strftime,1,0))) dateformat(const char *fmt);
  461. // Other formats
  462. void test_other_formats() {
  463. char *str = "";
  464. monformat("", 1); // expected-warning{{format string is empty}}
  465. monformat(str); // expected-warning{{format string is not a string literal (potentially insecure)}}
  466. dateformat(""); // expected-warning{{format string is empty}}
  467. dateformat(str); // no-warning (using strftime non-literal is not unsafe)
  468. }
  469. // Do not warn about unused arguments coming from system headers.
  470. // <rdar://problem/11317765>
  471. #include <format-unused-system-args.h>
  472. void test_unused_system_args(int x) {
  473. PRINT1("%d\n", x); // no-warning{{extra argument is system header is OK}}
  474. }
  475. void pr12761(char c) {
  476. // This should not warn even with -fno-signed-char.
  477. printf("%hhx", c);
  478. }
  479. // Test that we correctly merge the format in both orders.
  480. extern void test14_foo(const char *, const char *, ...)
  481. __attribute__((__format__(__printf__, 1, 3)));
  482. extern void test14_foo(const char *, const char *, ...)
  483. __attribute__((__format__(__scanf__, 2, 3)));
  484. extern void test14_bar(const char *, const char *, ...)
  485. __attribute__((__format__(__scanf__, 2, 3)));
  486. extern void test14_bar(const char *, const char *, ...)
  487. __attribute__((__format__(__printf__, 1, 3)));
  488. void test14_zed(int *p) {
  489. test14_foo("%", "%d", p); // expected-warning{{incomplete format specifier}}
  490. test14_bar("%", "%d", p); // expected-warning{{incomplete format specifier}}
  491. }
  492. void test_qualifiers(volatile int *vip, const int *cip,
  493. const volatile int *cvip) {
  494. printf("%n", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
  495. printf("%n", cvip); // expected-warning{{format specifies type 'int *' but the argument has type 'const volatile int *'}}
  496. printf("%n", vip); // No warning.
  497. printf("%p", cip); // No warning.
  498. printf("%p", cvip); // No warning.
  499. typedef int* ip_t;
  500. typedef const int* cip_t;
  501. printf("%n", (ip_t)0); // No warning.
  502. printf("%n", (cip_t)0); // expected-warning{{format specifies type 'int *' but the argument has type 'cip_t' (aka 'const int *')}}
  503. }
  504. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  505. #pragma GCC diagnostic warning "-Wformat-security"
  506. // <rdar://problem/14178260>
  507. extern void test_format_security_extra_args(const char*, int, ...)
  508. __attribute__((__format__(__printf__, 1, 3)));
  509. void test_format_security_pos(char* string) {
  510. test_format_security_extra_args(string, 5); // expected-warning {{format string is not a string literal (potentially insecure)}}
  511. }
  512. #pragma GCC diagnostic warning "-Wformat-nonliteral"