File: Warray-bounds-81.c

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (302 lines) | stat: -rw-r--r-- 8,850 bytes parent folder | download | duplicates (2)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* PR tree-optimization/101397 - spurious warning writing to the result
   of stpcpy minus 1
   Verify warnings for indexing into a pointer returned from stpncpy.
   The call stpncpy(S1, S2, N) returns the address of the copy of
   the first NUL is it exists or &S1[N] otherwise.
   { dg-do compile }
   { dg-options "-O2 -Wall -Wno-stringop-truncation" } */

typedef __SIZE_TYPE__ size_t;

void* calloc (size_t, size_t);
char* stpncpy (char*, const char*, size_t);

void sink (int, ...);

extern char ax[], a3[3], a5[5], a7[7], a9[9], *s;

volatile int x;

/* Verify warnings for indexing into the result of stpncpy with a source
   pointing to an array of unknown bound.  */

void test_stpncpy_from_ptr (int i, int n)
{
  {
    // P is in [ax, ax + 5].
    char *p = stpncpy (ax, s, 5);
    x = p[-6];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-5], p[-1], p[0], p[9]);
  }

  {
    // P is in [a5, a5 + 3].
    char *p = stpncpy (a5, s, 3);
    x = p[-4];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-3], p[-2], p[-1], p[0]);
    sink (p[ 1], p[ 2], p[ 3], p[4]);
    x = p[ 5];                          // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    // P is in [ax, ax + 4].
    char *p = stpncpy (a5, s, 4);
    x = p[-5];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-4], p[-3], p[-2], p[-1], p[0]);
    sink (p[ 1], p[ 2], p[ 3], p[4]);
    x = p[ 5];                          // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    // P is in [ax, ax + 5].
    char *p = stpncpy (a5, s, n);
    x = p[-6];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-5], p[-4], p[-3], p[-2], p[-1], p[0]);
    sink (p[ 1], p[ 2], p[ 3], p[4]);
    x = p[ 5];                          // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    // P is in [ax, ax + 4].
    char *p = stpncpy (a5, s, 4);

    if (i > -1) i = -1;
    x = p[i];

    if (i > -2) i = -2;
    x = p[i];

    if (i > -3) i = -3;
    x = p[i];

    if (i > -4) i = -4;
    x = p[i];

    if (i > -5) i = -5;
    x = p[i];                           // { dg-warning "\\\[-Warray-bounds" }
  }
}

/* Verify warnings for indexing into the result of stpncpy with a source
   an array of size 5.  */

void test_stpncpy_from_a5 (int i, int n, int n3_9)
{
  {
    // The returned pointer is in [ax, ax + 3].
    char *p = stpncpy (ax, a5, 3);
    x = p[-4];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-3], p[-2], p[-1], p[0], p[1], p[99]);
  }

  {
    // The returned pointer is in [ax, ax + 5].
    char *p = stpncpy (ax, a5, 5);
    x = p[-6];                          // { dg-warning "\\\[-Warray-bounds" }
    x = p[-5];
    x = p[-1];
    x = p[ 0];
    x = p[ 9];
  }

  {
    //The returned pointer is in [ax, ax + 5] even though n is not known.
    char *p = stpncpy (ax, a5, n);
    x = p[-6];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-5], p[-4], p[-2], p[-1], p[0]);
    sink (p[1], p[2], p[3], p[4], p[9], p[99]);
  }


  {
    // The returned pointer is in [a3, a3 + 3].
    char *p = stpncpy (a3, a5, 3);
    x = p[-4];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-3], p[-2], p[-1], p[0]);
    sink (p[ 1], p[ 2]);
    x = p[ 3];                          // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    // The returned pointer is in [a3, a3 + 3].
    char *p = stpncpy (a3, a5, n);
    x = p[-4];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-3], p[-2], p[-1], p[0]);
    sink (p[ 1], p[ 2]);
    x = p[ 3];                          // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    if (n3_9 < 3 || 9 < n3_9)
      n3_9 = 3;

    // The returned pointer is in [a3, a3 + 3].
    char *p = stpncpy (a3, a5, n3_9);
    x = p[-4];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-3], p[-2], p[-1], p[0]);
    sink (p[ 1], p[ 2]);
    x = p[ 3];                          // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    char *p = stpncpy (a3, a5, 3);

    if (i > -1) i = -1;
    x = p[i];

    if (i > -2) i = -2;
    x = p[i];

    if (i > -3) i = -3;
    x = p[i];

    if (i > -4) i = -4;
    x = p[i];                           // { dg-warning "\\\[-Warray-bounds" }
  }
}


/* Verify warnings for indexing into the result of stpncpy with a source
   an array of size 7.  */

void test_stpncpy_from_a7 (int i, int n, int n3_9)
{
  {
    // The returned pointer is ax + 5.
    char *p = stpncpy (ax, a7, 5);
    x = p[-6];                          // { dg-warning "\\\[-Warray-bounds" }
    x = p[-5];
    x = p[-1];
    x = p[ 0];
    x = p[ 9];
  }

  {
    //The returned pointer is in [ax, ax + 7] even though n is not known.
    char *p = stpncpy (ax, a7, n);
    x = p[-8];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-7], p[-6], p[-5], p[-4], p[-3], p[-2], p[-1], p[0]);
    sink (p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9]);
  }


  {
    // The returned pointer is in [a5, a5 + 3].
    char *p = stpncpy (a5, a7, 3);
    x = p[-4];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-3], p[-2], p[-1], p[0]);
    sink (p[1], p[2], p[3], p[4]);
    x = p[ 5];                          // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    // The returned pointer is a5 + 4.
    char *p = stpncpy (a5, a7, 4);
    x = p[-5];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-4], p[-3], p[-2], p[-1], p[0]);
    sink (p[1], p[2], p[3], p[4]);
    x = p[ 5];                          // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    // The returned pointer is in [a5, a5 + 5].
    char *p = stpncpy (a5, a7, n);
    x = p[-6];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-5], p[-4], p[-3], p[-2], p[-1], p[0]);
    sink (p[1], p[2], p[3], p[4]);
    x = p[ 5];                          // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    if (n3_9 < 3 || 9 < n3_9)
      n3_9 = 3;

    // The returned pointer is in [a5, a5 + 5].
    char *p = stpncpy (a5, a7, n3_9);
    x = p[-6];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-5], p[-4], p[-3], p[-2], p[-1], p[0]);
    sink (p[1], p[2], p[3], p[4]);
    x = p[ 5];                          // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    char *p = stpncpy (a5, a7, 4);

    if (i > -1) i = -1;
    x = p[i];

    if (i > -2) i = -2;
    x = p[i];

    if (i > -3) i = -3;
    x = p[i];

    if (i > -4) i = -4;
    x = p[i];

    if (i > -5) i = -5;
    x = p[i];                           // { dg-warning "\\\[-Warray-bounds" }
  }
}


void test_stpncpy_from_a5_to_allocated (int i, int n, int n5_7, int n3_9)
{
  if (n5_7 < 5 || 7 < n5_7)
    n5_7 = 5;

  {
    char *d = calloc (n5_7, 1);
    char *p = stpncpy (d, s, n);
    x = p[-8];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-7], p[-6], p[-5], p[-4], p[-3], p[-2], p[-1], p[0]);
    sink (p[1], p[2], p[3], p[4], p[5], p[6]);
    x = p[7];                           // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    char *d = calloc (n5_7, 1);
    char *p = stpncpy (d, a3, n);
    x = p[-4];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-3], p[-2], p[-1], p[0]);
    sink (p[1], p[2], p[3], p[4], p[5], p[6]);
    x = p[7];                           // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    char *d = calloc (n5_7, 1);
    char *p = stpncpy (d, a5, n);
    x = p[-6];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-5], p[-4], p[-3], p[-2], p[-1], p[0]);
    sink (p[1], p[2], p[3], p[4], p[5], p[6]);
    x = p[7];                           // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    char *d = calloc (n5_7, 1);
    char *p = stpncpy (d, a9, n);
    x = p[-8];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-7], p[-6], p[-5], p[-3], p[-4], p[-2], p[-1], p[0]);
    sink (p[1], p[2], p[3], p[4], p[5], p[6]);
    x = p[7];                           // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    char *d = calloc (n5_7, 1);
    char *p = stpncpy (d, a3, n3_9);
    x = p[-4];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-3], p[-2], p[-1], p[0]);
    sink (p[1], p[2], p[3], p[4], p[5], p[6]);
    x = p[7];                           // { dg-warning "\\\[-Warray-bounds" }
  }

  {
    char *d = calloc (n5_7, 1);
    char *p = stpncpy (d, a9, n3_9);
    x = p[-8];                          // { dg-warning "\\\[-Warray-bounds" }
    sink (p[-7], p[-6], p[-5], p[-4], p[-4], p[-2], p[-1], p[0]);
    sink (p[1], p[2], p[3], p[4], p[5], p[6]);
    x = p[7];                           // { dg-warning "\\\[-Warray-bounds" }
  }

}