aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Wstringop-overflow-44.c
blob: 9e292a9b7f93d33b2c2c6804c286d1617eb86d78 (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
120
121
122
123
124
125
126
127
128
129
/* PR middle-end/97175 - ICE on an excessive strncpy bound
   { dg-do compile }
   { dg-options "-O -Wall" } */

int n;

char *d;

void sink (void*);

/* Exercise calls with a destination of unknown size.  */

void f0 (const void *s)
{
  if (n > 0) return;
  __builtin_memcpy (d, s, n);       // eliminated
}

void f1 (const void *s)
{
  if (n > 0) return;
  __builtin_memmove (d, s, n);      // eliminated
}

void f2 (void)
{
  if (n > 0) return;
  __builtin_memset (d, 0, n);       // eliminated
}

void f3 (const char *s)
{
  if (n > 0) return;
  __builtin_strncpy (d, s, n);      // can be eliminated but isn't
}

void f4 (const char *s)
{
  if (n > 0) return;
  *d = 0;
  __builtin_strncat (d, s, n);      // can be eliminated but isn't
}


/* Exercise the same calls but with a declared destination object.  */

void g0 (const void *s)
{
  if (n > 0) return;
  char a[1];
  __builtin_memcpy (a, s, n);       // eliminated
  sink (a);
}

void g1 (const void *s)
{
  if (n > 0) return;
  char a[1];
  __builtin_memmove (a, s, n);      // eliminated
  sink (a);
}

void g2 (void)
{
  if (n > 0) return;
  char a[1];
  __builtin_memset (a, 0, n);       // eliminated
  sink (a);
}

void g3 (const char *s)
{
  if (n > 0) return;
  char a[1];
  __builtin_strncpy (a, s, n);      // can be eliminated but isn't
  sink (a);
}

void g4 (const char *s)
{
  if (n > 0) return;
  char a[1];
  *a = 0;
  __builtin_strncat (a, s, n);      // can be eliminated but isn't
  sink (a);
}


void h0 (const void *s)
{
  if (n > 0) return;
  d = __builtin_malloc (1);
  __builtin_memcpy (d, s, n);       // eliminated
}

void h1 (const void *s)
{
  if (n > 0) return;
  d = __builtin_malloc (1);
  __builtin_memmove (d, s, n);      // eliminated
}

void h2 (void)
{
  if (n > 0) return;
  d = __builtin_malloc (1);
  __builtin_memset (d, 0, n);       // eliminated
}

void h3 (const char *s)
{
  if (n > 0) return;
  d = __builtin_malloc (1);
  __builtin_strncpy (d, s, n);      // can be eliminated but isn't
}

void h4 (const char *s)
{
  if (n > 0) return;
  d = __builtin_malloc (1);
  *d = 0;
  __builtin_strncat (d, s, n);      // can be eliminated but isn't
}

/* The calls above that aren't eliminated trigger
     warning: specified size between INT_MAX and SIZE_MAX exceed maximum
              object size PTRDIFF_MAX
  { dg-prune-output "-Wstringop-overflow" }
  { dg-prune-output "-Wstringop-overread" } */