1 /* Verify -Wstringop-overflow a with destination pointer pointing either
2 before the beginning or past the end of an object.
3 { dg-do compile }
4 { dg-options "-O -Wall -Wno-array-bounds -Wno-restrict" } */
5
6 typedef __SIZE_TYPE__ size_t;
7
8 char* strcpy (char *, const char *);
9
10
11 extern char a[1];
12
13 volatile char *d;
14
15 void cpy_si_1_max (int i, const char *s)
16 {
17 if (i < 1) i = 1;
18 d = strcpy (a + i, s); // { dg-warning "writing 1 or more bytes into a region of size 0" }
19 d = strcpy (a + i + 1, s); // { dg-warning "writing 1 or more bytes into a region of size 0" }
20 }
21
22 void cpy_ui_1_max (unsigned i, const char *s)
23 {
24 if (i < 1) i = 1;
25 d = strcpy (a + i, s); // { dg-warning "writing 1 or more bytes into a region of size 0" }
26 d = strcpy (a + i + 1, s); // { dg-warning "writing 1 or more bytes into a region of size 0" "" { xfail { ! lp64 } } }
27 }
28
29 void cpy_sl_1_max (long i, const char *s)
30 {
31 if (i < 1) i = 1;
32 d = strcpy (a + i, s); // { dg-warning "writing 1 or more bytes into a region of size 0" "" { target { ! ptr_eq_short } } }
33 d = strcpy (a + i + 1, s); // { dg-warning "writing 1 or more bytes into a region of size 0" "" { target { ! ptr_eq_short } } }
34 }
35
36 void cpy_ul_1_max (unsigned long i, const char *s)
37 {
38 if (i < 1) i = 1;
39
40 d = strcpy (a + i, s); // { dg-warning "writing 1 or more bytes into a region of size 0" "" { target { ! ptr_eq_short } } }
41
42 /* Because of integer wraparound the offset's range is [1, 0] so
43 the overflow isn't diagnosed (yet). */
44 d = strcpy (a + i + 1, s); // { dg-warning "writing 1 or more bytes into a region of size 0" "" { xfail *-*-* } }
45 }
46
47
48 void cpy_si_min_m1 (int i, const char *s)
49 {
50 if (i > -1) i = -1;
51 d = strcpy (a + i - 1, s); // { dg-warning "writing 1 or more bytes into a region of size 0" }
52 d = strcpy (a + i, s); // { dg-warning "writing 1 or more bytes into a region of size 0" }
53 d = strcpy (a + i + 2, s);
54 }
55
56 void cpy_sl_min_m1 (long i, const char *s)
57 {
58 if (i > -1) i = -1;
59 d = strcpy (a + i - 1, s); // { dg-warning "writing 1 or more bytes into a region of size 0" "" { target { ! ptr_eq_short } } }
60 d = strcpy (a + i, s); // { dg-warning "writing 1 or more bytes into a region of size 0" "" { target { ! ptr_eq_short } } }
61 d = strcpy (a + i + 2, s);
62 }