PR middle-end/87041 - -Wformat reading through null pointer on unreachable code
[official-gcc.git] / gcc / testsuite / gcc.dg / tree-ssa / builtin-printf-warn-1.c
blob31a5bd3e9b075b3e37ae8409228dce4b6011998e
1 /* PR middle-end/87041 - -Wformat "reading through null pointer" on
2 unreachable code
3 Test to verify that the applicable subset of -Wformat-overflow warnings
4 are issued for the printf function.
5 { dg-do compile }
6 { dg-options "-O -Wformat -Wformat-overflow=1 -ftrack-macro-expansion=0" }
7 { dg-require-effective-target int32plus } */
9 /* When debugging, define LINE to the line number of the test case to exercise
10 and avoid exercising any of the others. The buffer and objsize macros
11 below make use of LINE to avoid warnings for other lines. */
12 #ifndef LINE
13 # define LINE 0
14 #endif
16 #define INT_MAX __INT_MAX__
18 typedef __SIZE_TYPE__ size_t;
20 #if !__cplusplus
21 typedef __WCHAR_TYPE__ wchar_t;
22 #endif
24 typedef __WINT_TYPE__ wint_t;
26 typedef unsigned char UChar;
28 void sink (void*, ...);
30 int dummy_printf (const char*, ...);
32 const char chr_no_nul = 'a';
33 const char arr_no_nul[] = { 'a', 'b' };
36 /* Helper to expand function to either __builtin_f or dummy_f to
37 make debugging GCC easy. */
38 #define T(...) \
39 (((!LINE || LINE == __LINE__) \
40 ? __builtin_printf : dummy_printf) (__VA_ARGS__))
42 /* Exercise the "%c" directive with constant arguments. */
44 void test_printf_c_const (int width)
46 /* Verify that a warning is issued for exceeding INT_MAX bytes and
47 not otherwise. */
48 T ("%*c", INT_MAX - 1, '1');
49 T ("%*c", INT_MAX, '1');
50 T ("X%*c", INT_MAX - 1, '1');
51 T ("X%*c", INT_MAX, '1'); /* { dg-warning "directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
53 T ("%*c%*c", INT_MAX - 1, '1', INT_MAX - 1, '2'); /* { dg-warning "directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
55 T ("%*cX", INT_MAX - 2, '1');
56 T ("%*cX", INT_MAX - 1, '1');
57 T ("%*cX", INT_MAX, '1'); /* { dg-warning "output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
59 if (width < INT_MAX - 1)
60 width = INT_MAX - 1;
61 T ("%*cX", width, '1');
62 T ("%*cXY", width, '1'); /* { dg-warning ".XY. directive output of 2 bytes causes result to exceed .INT_MAX." } */
64 /* Also exercise a non-constant format string. The warning points
65 to the line where the format is declared (see bug 87773) so avoid
66 triggering that bug here. */
67 const char *fmt = "%*cXYZ"; T (fmt, width, '1'); /* { dg-warning ".XYZ. directive output of 3 bytes causes result to exceed .INT_MAX." } */
71 /* Exercise the "%s" directive with constant arguments. */
73 void test_printf_s_const (int width)
75 const char *nulptr = 0;
77 T ("%s", nulptr); /* { dg-warning "\\\[-Wformat|-Wnonnull]" } */
78 T ("%.0s", nulptr); /* { dg-warning ".%.0s. directive argument is null" } */
80 /* Verify no warning is issued for unreachable code. */
81 if (nulptr)
82 T ("%s", nulptr);
84 T ("%s", &chr_no_nul); /* { dg-warning ".%s. directive argument is not a nul-terminated string" "pr87756" { xfail *-*-* } } */
85 T ("%s", arr_no_nul); /* { dg-warning ".%s. directive argument is not a nul-terminated string" } */
87 /* Verify that output in excess of INT_MAX bytes is diagnosed even
88 when the size of the destination object is unknown. */
89 T ("%*s", INT_MAX - 1, "");
90 T ("%*s", INT_MAX, "");
91 T ("X%*s", INT_MAX, ""); /* { dg-warning "directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
93 if (width < INT_MAX - 1)
94 width = INT_MAX - 1;
95 T ("%*sX", width, "1");
96 T ("%*sXY", width, "1"); /* { dg-warning ".XY. directive output of 2 bytes causes result to exceed .INT_MAX." } */
100 const wchar_t wchr_no_nul = L'a';
101 const wchar_t warr_no_nul[] = { L'a', L'b' };
103 /* Exercise the "%s" directive with constant arguments. */
105 void test_printf_ls_const (int width)
107 const wchar_t *nulptr = 0;
109 T ("%ls", nulptr); /* { dg-warning ".%ls. directive argument is null" } */
110 T ("%.0ls", nulptr); /* { dg-warning ".%.0ls. directive argument is null" } */
112 /* Verify no warning is issued for unreachable code. */
113 if (nulptr)
114 T ("%ls", nulptr);
116 T ("%ls", &wchr_no_nul); /* { dg-warning ".%ls. directive argument is not a nul-terminated string" "pr87756" { xfail *-*-* } } */
117 T ("%ls", warr_no_nul); /* { dg-warning ".%ls. directive argument is not a nul-terminated string" "pr87756" { xfail *-*-* } } */
119 /* Verify that output in excess of INT_MAX bytes is diagnosed even
120 when the size of the destination object is unknown. */
121 T ("%*ls", INT_MAX - 1, L"");
122 T ("%*ls", INT_MAX, L"");
123 T ("X%*ls", INT_MAX, L""); /* { dg-warning "directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
125 if (width < INT_MAX - 1)
126 width = INT_MAX - 1;
127 T ("%*lsX", width, L"1");
128 T ("%*lsXY", width, L"1"); /* { dg-warning ".XY. directive output of 2 bytes causes result to exceed .INT_MAX." } */