libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / testsuite / gcc.dg / Wstringop-overflow-61.c
blob93c54c646c069f0d466003241f201b49dad31b65
1 /* { dg-do compile }
2 { dg-options "-O2 -Wall" } */
4 typedef __SIZE_TYPE__ size_t;
6 void* malloc (size_t);
7 void* memcpy (void*, const void*, size_t);
8 size_t strlen (const char *);
10 // Test case reduced from gcc/attribs.c.
12 char* sorted_attr_string (char *argv[])
14 size_t n = 0;
15 unsigned int i;
17 for (i = 0; argv[i]; ++i)
18 n += strlen (argv[i]);
20 char *s = (char*)malloc (n);
21 n = 0;
22 for (i = 0; argv[i]; ++i)
24 const char *str = argv[i];
25 size_t len = strlen (str);
26 memcpy (s + n, str, len);
27 n += len + 1;
30 /* Replace "=,-" with "_". */
31 for (i = 0; i < strlen (s); i++)
32 if (s[i] == '=')
33 s[i] = '_'; // { dg-bogus "\\\[-Wstringop-overflow" }
35 return s;
39 void f (void*);
41 void nowarn_cond_escape (int c, int *x)
43 extern char a3[3], a5[5];
45 char *p;
46 if (c)
48 p = a3;
49 *x = 2;
51 else
53 p = a5;
54 *x = 4;
57 f (p); // may modify *x
59 if (*x == 2)
60 p[2] = 0;
61 else if (*x == 4)
62 p[4] = 0; // { dg-bogus "\\\[-Wstringop-overflow" }
65 void warn_cond_escape (int c, int *x)
67 extern char a3_2[3];
68 extern char a5_2[5]; // { dg-message "at offset 5 into object 'a5_2'" }
70 char *p;
71 if (c)
73 p = a3_2;
74 *x = 2;
76 else
78 p = a5_2;
79 *x = 5;
82 f (p); // may modify *x
84 if (*x == 2)
85 p[2] = 0;
86 else if (*x == 5)
87 p[5] = 0; // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }