| 1 |
/* PR tree-optimization/89350 - Wrong -Wstringop-overflow warning |
| 2 |
on a variable offset from the end of an array |
| 3 |
Test exercising -Wstringop-truncation alone, with -Warray-bounds |
| 4 |
explicitly disabled. |
| 5 |
{ dg-do compile } |
| 6 |
{ dg-options "-O2 -Wno-array-bounds -Wstringop-overflow -ftrack-macro-expansion=0" } */ |
| 7 |
|
| 8 |
#include "range.h" |
| 9 |
|
| 10 |
extern void* memcpy (void*, const void*, size_t); |
| 11 |
extern void* memset (void*, int, size_t); |
| 12 |
|
| 13 |
void sink (void*); |
| 14 |
|
| 15 |
extern char ga7[7]; |
| 16 |
|
| 17 |
|
| 18 |
#define T(d, n) (memcpy ((d), s, (n)), sink (d)) |
| 19 |
|
| 20 |
void test_memcpy_array_cst_range_off (const void *s) |
| 21 |
{ |
| 22 |
char *d = ga7 + 1; |
| 23 |
|
| 24 |
T (d + UR (1, 2), 1); |
| 25 |
T (d + UR (1, 2), 5); |
| 26 |
|
| 27 |
T (d + UR (0, 1), 6); |
| 28 |
T (d + UR (0, 1), 7); /* { dg-warning ".memcpy. writing 7 bytes into a region of size 6 overflows the destination" "pr89428" } */ |
| 29 |
T (d + UR (1, 2), 6); /* { dg-warning ".memcpy. writing 6 bytes into a region of size 5 overflows the destination" } */ |
| 30 |
T (d + UR (1, 2), 7); /* { dg-warning "writing 7 bytes into a region of size 5 " } */ |
| 31 |
|
| 32 |
T (d + SR (-3, -2), 1); /* { dg-warning "writing 1 byte into a region of size 0 " } */ |
| 33 |
T (d + SR (-2, -1), 1); |
| 34 |
T (d + SR (-2, -1), 2); /* { dg-warning "writing 2 bytes into a region of size 7 " "pr89428" { xfail *-*-* } } */ |
| 35 |
T (d + SR (-2, -1), 9); /* { dg-warning "writing 9 bytes into a region of size 7 " } */ |
| 36 |
|
| 37 |
d = ga7 + 7; |
| 38 |
T (d + SR (-7, -6), 1); |
| 39 |
T (d + SR (-7, -1), 1); |
| 40 |
T (d + SR (-2, -1), 3); /* { dg-warning "writing 3 bytes into a region of size 2 " } */ |
| 41 |
|
| 42 |
T (d + UR (1, 2), 1); /* { dg-warning "writing 1 byte into a region of size 0 " } */ |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
void test_memcpy_array_range_range_off (const void *s) |
| 47 |
{ |
| 48 |
char *d = ga7 + UR (0, 1); |
| 49 |
T (d + SR (-1, 0), 1); |
| 50 |
T (d + SR (-1, 0), 7); |
| 51 |
T (d + SR (-1, 0), 8); /* { dg-warning "writing 8 bytes into a region of size 7 " } */ |
| 52 |
T (d + SR (-1, 0), 9); /* { dg-warning "writing 9 bytes into a region of size 7 " "pr89350" } */ |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
#undef T |
| 57 |
#define T(d, n) (memset ((d), 0, (n)), sink (d)) |
| 58 |
|
| 59 |
void test_memset_array_unsigned_off (void) |
| 60 |
{ |
| 61 |
char *d = ga7 + 1; |
| 62 |
|
| 63 |
T (d + UR (1, 2), 1); |
| 64 |
T (d + UR (1, 2), 5); |
| 65 |
|
| 66 |
T (d + UR (0, 1), 6); |
| 67 |
T (d + UR (0, 1), 7); /* { dg-warning ".memset. writing 7 bytes into a region of size 6 overflows the destination" "pr89428" } */ |
| 68 |
T (d + UR (1, 2), 6); /* { dg-warning ".memset. writing 6 bytes into a region of size 5 overflows the destination" } */ |
| 69 |
T (d + UR (1, 2), 7); /* { dg-warning "writing 7 bytes into a region of size 5 " } */ |
| 70 |
|
| 71 |
T (d + SR (-3, -2), 1); /* { dg-warning "writing 1 byte into a region of size 0 " } */ |
| 72 |
T (d + SR (-2, -1), 1); |
| 73 |
T (d + SR (-2, -1), 2); /* { dg-warning "writing 2 bytes into a region of size 7 " "pr89428" { xfail *-*-* } } */ |
| 74 |
T (d + SR (-2, -1), 9); /* { dg-warning "writing 9 bytes into a region of size 7 " } */ |
| 75 |
|
| 76 |
d = ga7 + 7; |
| 77 |
T (d + SR (-7, -6), 1); |
| 78 |
T (d + SR (-7, -1), 1); |
| 79 |
T (d + SR (-2, -1), 3); /* { dg-warning "writing 3 bytes into a region of size 2 " } */ |
| 80 |
|
| 81 |
T (d + UR (1, 2), 1); /* { dg-warning "writing 1 byte into a region of size 0 " } */ |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
struct MemArray { char a7[7], a3[3], c; }; |
| 87 |
|
| 88 |
extern struct MemArray gma; |
| 89 |
|
| 90 |
void test_memset_memarray (void) |
| 91 |
{ |
| 92 |
char *d = gma.a7 + 1; |
| 93 |
|
| 94 |
T (d + UR (1, 2), 1); |
| 95 |
T (d + UR (1, 2), 5); |
| 96 |
|
| 97 |
T (d + UR (0, 1), 6); |
| 98 |
T (d + UR (0, 1), 7); /* { dg-warning ".memset. writing 6 bytes into a region of size 5 overflows the destination" "pr89428" { xfail *-*-* } } */ |
| 99 |
T (d + UR (1, 2), 6); /* { dg-warning ".memset. writing 6 bytes into a region of size 5 overflows the destination" "pr89350" { xfail *-*-* } } */ |
| 100 |
T (d + UR (1, 2), 7); /* { dg-warning "writing 7 bytes into a region of size 5 " "pr85350" { xfail *-*-* } } */ |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
#undef T |
| 106 |
#define T(d, n) (memcpy ((d), s, (n)), sink (d)) |
| 107 |
|
| 108 |
void test_memcpy_array_signed_off (const void *s) |
| 109 |
{ |
| 110 |
char *d = ga7 + 1; |
| 111 |
|
| 112 |
T (d + SR (-7, 7), 7); |
| 113 |
T (d + SR (-1, 1), 7); |
| 114 |
T (d + SR (-1, 1), 9); /* { dg-warning "writing 9 bytes into a region of size " } */ |
| 115 |
T (d + SR (-1, 2), 9); /* { dg-warning "writing 9 bytes into a region of size " } */ |
| 116 |
T (d + SR (1, 2), 1); |
| 117 |
T (d + SR (1, 2), 5); |
| 118 |
|
| 119 |
T (d + SR (0, 1), 6); |
| 120 |
T (d + UR (1, 2), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */ |
| 121 |
} |