summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/warn/Warray-bounds.C
blob: 61c7c5d8e32ea91c4dc97555d53cbbbc31dadf07 (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
/* { dg-do compile } */
/* { dg-options "-O2 -Warray-bounds" } */

int a[10];

extern "C" __SIZE_TYPE__ strlen(const char *s);

static inline int n(void) {
    return strlen("12345");
}

void g(int *p);
void h(int p);

int* f(void) {
    int b[10];
    int i;
    struct {
       int c[10];
    } c;

    a[-1] = 0;             /* { dg-warning "array subscript" } */
    a[ 0] = 0;
    a[ 1] = 0;


    a[ 9] = 0;
    a[10] = 0;             /* { dg-warning "array subscript" } */
    a[11] = 0;             /* { dg-warning "array subscript" } */
    a[2 * n() - 11] = 1;    /* { dg-warning "array subscript" } */
    a[2 * n() - 10] = 1;
    a[2 * n() -  1] = 1;
    a[2 * n() -  0] = 1;    /* { dg-warning "array subscript" } */

    b[-1] = 0;             /* { dg-warning "array subscript" } */
    b[ 0] = 0;
    b[ 1] = 0;
    b[ 9] = 0;
    b[10] = 0;             /* { dg-warning "array subscript" } */
    b[11] = 0;             /* { dg-warning "array subscript" } */
    b[2 * n() - 11] = 1;    /* { dg-warning "array subscript" } */
    b[2 * n() - 10] = 1;
    b[2 * n() -  1] = 1;
    b[2 * n() -  0] = 1;    /* { dg-warning "array subscript" } */

    c.c[-1] = 0;           /* { dg-warning "array subscript" } */
    c.c[ 0] = 0;
    c.c[ 1] = 0;
    c.c[ 9] = 0;
    c.c[10] = 0;           /* { dg-warning "array subscript" } */
    c.c[11] = 0;           /* { dg-warning "array subscript" } */
    c.c[2 * n() - 11] = 1;  /* { dg-warning "array subscript" } */
    c.c[2 * n() - 10] = 1;
    c.c[2 * n() -  1] = 1;
    c.c[2 * n() -  0] = 1;  /* { dg-warning "array subscript" } */

    g(&a[8]);
    g(&a[9]);
    g(&a[10]);
    g(&a[11]);             /* { dg-warning "array subscript" } */
    g(&a[-30]+10);             /* { dg-warning "array subscript" } */
    g(&a[-30]+30);

    g(&b[10]);
    g(&c.c[10]);
    g(&a[11]);             /* { dg-warning "array subscript" } */
    g(&b[11]);             /* { dg-warning "array subscript" } */
    g(&c.c[11]);           /* { dg-warning "array subscript" } */

    g(&a[0]);
    g(&b[0]);
    g(&c.c[0]);

    g(&a[-1]);             /* { dg-warning "array subscript" } */
    g(&b[-1]);             /* { dg-warning "array subscript" } */ 
    h(sizeof a[-1]);
    h(sizeof a[10]);
    h(sizeof b[-1]);
    h(sizeof b[10]);
    h(sizeof c.c[-1]);
    h(sizeof c.c[10]);

    if (10 < 10)
       a[10] = 0;
    if (10 < 10)
       b[10] = 0;
    if (-1 >= 0)
       c.c[-1] = 0;

    return a;
}