aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Wstringop-overflow-39.c
blob: 295a38de926bf74ede438250d928b7a81b079b0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* PR middle-end/95667 - unintended warning for memset writing across multiple
   members
   { dg-do compile }
   { dg-options "-O2 -Wall" }
   { dg-require-effective-target alloca } */

extern void sink (void*);

struct S1 { char a[3], b[5]; };

void warn_strcpy_s1 (void)
{
  struct S1 *p = __builtin_malloc (sizeof *p);
  char s[] = "1234567";
  __builtin_strcpy (p->a, s);         // { dg-warning "\\\[-Wstringop-overflow" }
  sink (p);
}

void nowarn_memset_s1 (void)
{
  struct S1 *p = __builtin_malloc (sizeof *p);
  __builtin_memset (p->a, 0, 8);      // { dg-bogus "\\\[-Wstringop-overflow" }
  sink (p);
}

struct S2 { char a[2], b[2][2], c[3]; };

void nowarn_memset_s2 (void)
{
  struct S2 *p = __builtin_malloc (sizeof *p);

  __builtin_memset (p->a, 0, sizeof *p);
  sink (p);

  __builtin_memset (p->b, 0, 7);
  sink (p);

  __builtin_memset (&p->b[0], 0, 7);
  sink (p);

  __builtin_memset (&p->b[1], 0, 5);
  sink (p);

  __builtin_memset (&p->b[0][0], 0, 7);
  sink (p);

  __builtin_memset (&p->b[0][1], 0, 6);
  sink (p);

  __builtin_memset (&p->b[1][0], 0, 5);
  sink (p);

  __builtin_memset (&p->b[1][1], 0, 4);
  sink (p);
}

void warn_memset_s2 (void)
{
  const unsigned n = sizeof (struct S2);
  struct S2 *p = __builtin_malloc (n);

  /* These should trigger -Wstringop-overflow rather than -Warray-bounds
     but the main purpose of the test is to verify the absence of warnings
     above so the exact warning for these overflwing calls isn't important
     here.  */

  __builtin_memset (p->a, 0, n + 1);  // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
  sink (p);

  __builtin_memset (p->b, 0, 8);      // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
  sink (p);

  __builtin_memset (&p->b[0], 0, 8);  // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
  sink (p);

  __builtin_memset (&p->b[0][0], 0, 8); // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
  sink (p);

  __builtin_memset (&p->b[1], 0, 6);  // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
  sink (p);

  __builtin_memset (&p->b[0][1], 0, 7); // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
  sink (p);
}

void nowarn_vl_struct (unsigned n)
{
  if (n < 3 || 5 < n)
    n = 3;

  struct V { char a[3], b[n], c[7]; } v;

  __builtin_memset (v.a, 0, 15);
  sink (&v);

  __builtin_memset (v.b, 0, 12);
  sink (&v);

  __builtin_memset (v.c, 0, 7);
  sink (&v);

  __builtin_memset (v.a, 0, 16);      // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
  sink (&v);

  __builtin_memset (v.b, 0, 13);      // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
  sink (&v);

  /* The &V.C argument is represented as a variable offset from
     the beginning of the allocated object and there's no good
     way to figure out from its variable offset that it's base
     is the C member:
       s.1_12 = __builtin_alloca_with_align (prephitmp_24, 8);
       _9 = s.1_12 + prephitmp_27;
       __builtin_memset (_9, 0, 2);
  */

  __builtin_memset (v.c, 0, 8);       // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" "pr?????" { xfail *-*-* } }
  sink (&v);
}