]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/gcc.dg/Wstringop-overflow-61.c
Correct handling of variable offset minus constant in -Warray-bounds [PR100137]
[thirdparty/gcc.git] / gcc / testsuite / gcc.dg / Wstringop-overflow-61.c
CommitLineData
eafe8ee7
MS
1/* { dg-do compile }
2 { dg-options "-O2 -Wall" } */
3
4typedef __SIZE_TYPE__ size_t;
5
6void* malloc (size_t);
7void* memcpy (void*, const void*, size_t);
8size_t strlen (const char *);
9
10// Test case reduced from gcc/attribs.c.
11
12char* sorted_attr_string (char *argv[])
13{
14 size_t n = 0;
15 unsigned int i;
16
17 for (i = 0; argv[i]; ++i)
18 n += strlen (argv[i]);
19
20 char *s = (char*)malloc (n);
21 n = 0;
22 for (i = 0; argv[i]; ++i)
23 {
24 const char *str = argv[i];
25 size_t len = strlen (str);
26 memcpy (s + n, str, len);
27 n += len + 1;
28 }
29
30 /* Replace "=,-" with "_". */
31 for (i = 0; i < strlen (s); i++)
32 if (s[i] == '=')
33 s[i] = '_'; // { dg-bogus "\\\[-Wstringop-overflow" }
34
35 return s;
36}
37
38
39void f (void*);
40
41void nowarn_cond_escape (int c, int *x)
42{
43 extern char a3[3], a5[5];
44
45 char *p;
46 if (c)
47 {
48 p = a3;
49 *x = 2;
50 }
51 else
52 {
53 p = a5;
54 *x = 4;
55 }
56
57 f (p); // may modify *x
58
59 if (*x == 2)
60 p[2] = 0;
61 else if (*x == 4)
62 p[4] = 0; // { dg-bogus "\\\[-Wstringop-overflow" }
63}
64
65void warn_cond_escape (int c, int *x)
66{
67 extern char a3_2[3];
a1108556 68 extern char a5_2[5]; // { dg-message "at offset 5 into object 'a5_2'" }
eafe8ee7
MS
69
70 char *p;
71 if (c)
72 {
73 p = a3_2;
74 *x = 2;
75 }
76 else
77 {
78 p = a5_2;
79 *x = 5;
80 }
81
82 f (p); // may modify *x
83
84 if (*x == 2)
85 p[2] = 0;
86 else if (*x == 5)
a1108556 87 p[5] = 0; // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
eafe8ee7 88}