summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Warray-bounds-29.c
blob: 72c5d1cecf8b94ea0e31b61102a5d77158b8f2c3 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* PR tree-optimization/83776: missing -Warray-bounds indexing past the end
   of a string literal
   Test to exercise warnings for computations of otherwise in-bounds indices
   into strings that temporarily exceed the bounds of the string.
   { dg-do compile }
   { dg-options "-O2 -Warray-bounds=2 -ftrack-macro-expansion=0" } */

#include "range.h"

#define MAX DIFF_MAX
#define MIN DIFF_MIN

typedef __WCHAR_TYPE__ wchar_t;

void sink (int, ...);

#define T(expr)   sink (0, expr)

void test_narrow (void)
{
  int i = SR (1, 2);

  const char *p0 = "12";
  const char *p1 = p0 + i;    /* points at '2' or beyond */
  const char *p2 = p1 + i;    /* points at '\0' or beyond */
  const char *p3 = p2 + i;    /* points just past the end */
  const char *p4 = p3 + i;    /* invalid */

  T (p0[-1]);                 /* { dg-warning "array subscript \(-1|\[0-9\]+) is outside array bounds of .char\\\[3]." } */
  T (p0[0]);
  T (p0[1]);
  T (p0[2]);
  T (p0[3]);                  /* { dg-warning "array subscript 3 is outside array bounds of .char\\\[3]." } */

  T (&p0[-1]);                /* { dg-warning "array subscript \(-1|\[0-9\]+) is \(above|below|outside\) array bounds of .char\\\[3]." } */
  T (&p0[0]);
  T (&p0[1]);
  T (&p0[2]);
  T (&p0[3]);
  T (&p0[4]);                 /* { dg-warning "array subscript 4 is \(above|outside\) array bounds of .char\\\[3]." } */

  T (p1[-3]);                 /* { dg-warning "array subscript \\\[-2, -1] is outside array bounds of .char\\\[3]." } */
  T (p1[-2]);
  T (p1[-1]);
  T (p1[ 0]);
  T (p1[ 1]);
  T (p1[ 2]);                 /* { dg-warning "array subscript \\\[3, 4] is outside array bounds of .char\\\[3]." } */
  T (p1[ 3]);                 /* { dg-warning "array subscript \\\[4, 5] is outside array bounds of .char\\\[3]." } */

  T (&p1[-3]);                /* { dg-warning "array subscript \\\[-2, -1] is outside array bounds of .char\\\[3]." "bug" { xfail *-*-* } } */
  T (&p1[-2]);
  T (&p1[-1]);
  T (&p1[ 0]);
  T (&p1[ 1]);
  T (&p1[ 2]);
  T (&p1[ 3]);                /* { dg-warning "array subscript \\\[4, 6] is outside array bounds of .char\\\[3]." "bug" { xfail *-*-* } } */

  T (p2[-4]);                 /* { dg-warning "intermediate array offset 4 is outside array bounds of .char\\\[3]." } */
  T (p2[-3]);
  T (p2[-2]);
  T (p2[-1]);
  T (p2[ 0]);

  /* Even though the lower bound of p3's offsets is in bounds, in order
     to subtract 4 from p3 and get a dereferenceable pointer its value
     would have to be out-of-bounds.  */
  T (p3[-4]);                 /* { dg-warning "intermediate array offset 4 is outside array bounds of .char\\\[3]." } */
  T (p3[-3]);
  T (p3[-2]);
  T (p3[-1]);
  T (p3[ 0]);                 /* { dg-warning "array subscript \\\[3, 6] is outside array bounds of .char\\\[3]." } */

  T (p4[-4]);                 /* { dg-warning "intermediate array offset 4 is outside array bounds of .char\\\[3]." } */
  T (p4[-3]);                 /* { dg-warning "intermediate array offset 4 is outside array bounds of .char\\\[3]." } */
  T (p4[-2]);                 /* { dg-warning "intermediate array offset 4 is outside array bounds of .char\\\[3]." } */

  /* The final subscripts below are invalid.  */
  T (p4[-1]);                 /* { dg-warning "array subscript \\\[3, 7] is outside array bounds of .char\\\[3]." } */
  T (p4[ 0]);                 /* { dg-warning "array subscript \\\[4, 8] is outside array bounds of .char\\\[3]." } */
}


void test_narrow_vflow (void)
{
  int i = SR (DIFF_MAX - 2, DIFF_MAX);
  int j = SR (1, DIFF_MAX);

  const char *p0 = "123";
  const char *p1 = p0 + i;    /* points at '2' or beyond */
  const char *p2 = p1 + i;    /* points at '\0' or beyond */
  const char *p3 = p2 + i;    /* points just past the end */
  const char *p4 = p3 + i;    /* invalid */
}


void test_wide (void)
{
  int i = SR (1, 2);

  const wchar_t *p0 = L"123";
  const wchar_t *p1 = p0 + i;    /* points at L'2' or beyond */
  const wchar_t *p2 = p1 + i;    /* points at L'3' or beyond */
  const wchar_t *p3 = p2 + i;    /* points at L'\0' or beyond */
  const wchar_t *p4 = p3 + i;    /* points just past the end */
  const wchar_t *p5 = p4 + i;    /* invalid */

  T (p0[0]);
  T (p0[1]);
  T (p0[2]);
  T (p0[3]);
  T (p0[4]);                  /* { dg-warning "array subscript 4 is outside array bounds of .\[a-z \]+\\\[4]." } */

  T (p1[-1]);
  T (p1[ 0]);
  T (p1[ 1]);
  T (p1[ 2]);
  T (p1[ 3]);                  /* { dg-warning "array subscript \\\[4, 5] is outside array bounds of .\[a-z \]+\\\[4]." } */

  T (&p1[-1]);
  T (&p1[ 0]);
  T (&p1[ 1]);
  T (&p1[ 2]);
  T (&p1[ 3]);
  T (&p1[ 4]);                 /* { dg-warning "array subscript \\\[5, 6] is outside array bounds of .\[a-z \]+\\\[4]." "bug" { xfail *-*-* } } */

  T (p2[-5]);                 /* { dg-warning "array subscript \\\[-3, -1] is outside array bounds of .\[a-z \]+\\\[4]." } */
  T (p2[-4]);
  T (p2[-3]);
  T (p2[-2]);
  T (p2[-1]);
  T (p2[ 0]);

  /* Even though the lower bound of p3's offsets is in bounds, in order
     to subtract 5 from p3 and get a dereferenceable pointer its value
     would have to be out-of-bounds.  */
  T (p3[-5]);                 /* { dg-warning "intermediate array offset 5 is outside array bounds of .\[a-z \]+\\\[4]." } */
  T (p3[-4]);
  T (p3[-3]);
  T (p3[-2]);
  T (p3[-1]);
  T (p3[ 0]);
  T (p3[ 1]);                 /* { dg-warning "array subscript \\\[4, 7] is outside array bounds of .\[a-z \]+\\\[4]." } */

  T (p4[-5]);                 /* { dg-warning "intermediate array offset 5 is outside array bounds of .\[a-z \]+\\\[4]." } */
  T (p4[-4]);
  T (p4[-3]);
  T (p4[-2]);
  T (p4[-1]);
  T (p4[ 0]);                 /* { dg-warning "array subscript \\\[4, 8] is outside array bounds of .\[a-z \]+\\\[4]." } */
}