libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / testsuite / gcc.dg / tree-ssa / builtin-sprintf-warn-21.c
blob41f932a41a878f405aeddc2e6a001c78da276e1f
1 /* PR tree-optimization/83431 -Wformat-truncation may incorrectly report
2 truncation
3 { dg-do compile }
4 { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
6 typedef __SIZE_TYPE__ size_t;
8 extern int snprintf (char*, size_t, const char*, ...);
9 extern char* strcpy (char*, const char*);
11 struct S
13 char a9[9];
14 char a5[5];
15 int x;
19 void test_assign_nowarn (struct S* s)
21 int i = 0;
24 char a9[9] = "1234";
25 snprintf (s[i].a5, sizeof (s[i].a5), "%s", a9); /* { dg-bogus "\\\[-Wformat-truncation]" } */
29 ++i;
30 char a8[8] = "123";
31 snprintf (s[i].a5, sizeof (s[i].a5), "%s\n", a8); /* { dg-bogus "\\\[-Wformat-truncation]" } */
35 ++i;
36 char a7[7] = "12";
37 snprintf (s[i].a5, sizeof (s[i].a5), "[%s]", a7); /* { dg-bogus "\\\[-Wformat-truncation]" } */
41 ++i;
42 char a6[6] = "1";
43 snprintf (s[i].a5, sizeof (s[i].a5), "[%s]\n", a6); /* { dg-bogus "\\\[-Wformat-truncation]" } */
48 void test_strcpy_nowarn (struct S* s)
50 int i = 0;
52 strcpy (s[i].a9, "1234");
53 snprintf (s[i].a5, sizeof (s[i].a5), "%s", s[i].a9);
55 ++i;
56 strcpy (s[i].a9, "123");
57 snprintf (s[i].a5, sizeof (s[i].a5), "%s\n", s[i].a9); /* { dg-bogus "\\\[-Wformat-truncation]" } */
59 ++i;
60 strcpy (s[i].a9, "12");
61 snprintf (s[i].a5, sizeof (s[i].a5), "[%s]", s[i].a9); /* { dg-bogus "\\\[-Wformat-truncation]" } */
63 ++i;
64 strcpy (s[i].a9, "1");
65 snprintf (s[i].a5, sizeof (s[i].a5), "[%s]\n", s[i].a9); /* { dg-bogus "\\\[-Wformat-truncation]" } */
69 void test_warn (struct S* s)
71 int i = 0;
72 strcpy (s[i].a9, "12345678");
73 snprintf (s[i].a5, sizeof (s[i].a5), "%s", s[i].a9); /* { dg-warning "'%s' directive output truncated writing 8 bytes into a region of size 5" } */
75 ++i;
76 strcpy (s[i].a9, "1234567");
77 snprintf (s[i].a5, sizeof (s[i].a5), "%s", s[i].a9); /* { dg-warning "'%s' directive output truncated writing 7 bytes into a region of size 5" } */
79 ++i;
80 strcpy (s[i].a9, "123456");
81 snprintf (s[i].a5, sizeof (s[i].a5), "%s", s[i].a9); /* { dg-warning "'%s' directive output truncated writing 6 bytes into a region of size 5" } */
83 ++i;
84 strcpy (s[i].a9, "12345");
85 snprintf (s[i].a5, sizeof (s[i].a5), "%s", s[i].a9); /* { dg-warning "'snprintf' output truncated before the last format character" } */
87 ++i;
88 strcpy (s[i].a9, "1234");
89 snprintf (s[i].a5, sizeof (s[i].a5), "%s\n", s[i].a9); /* { dg-warning "output truncated before the last format character" } */
91 ++i;
92 strcpy (s[i].a9, "123");
93 snprintf (s[i].a5, sizeof (s[i].a5), ">%s<", s[i].a9); /* { dg-warning "output truncated before the last format character" } */