1 /* Verify -Wstringop-overread with a source 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" } */
5
6 typedef __SIZE_TYPE__ size_t;
7
8 size_t strlen (const char *);
9
10 void sink (void*, ...);
11
12 void off_sz_or_1 (size_t i)
13 {
14 i |= 1;
15
16 /* Verify the offset in the notes only mentions the meaningful lower
17 bound and not a range with the excessive (and meaningless) upper
18 bound like [2, 9223372036854775807]. */
19 extern char a[1];
20 // { dg-message "at offset 1 into source object 'a'" "note" { target *-*-* } .-1 }
21 // { dg-message "at offset 2 " "note" { target *-*-* } .-2 }
22
23 char *p1 = a + i;
24 char *p2 = p1 + 1;
25 char *p3 = p1 - 1;
26
27 size_t n = 0;
28 n += strlen (p1); // { dg-warning "reading 1 or more bytes from a region of size 0" }
29 n += strlen (p2); // { dg-warning "reading 1 or more bytes from a region of size 0" }
30 n += strlen (p3);
31
32 sink (p1, p2, p3, n);
33 }
34
35
36 void off_sz_or_2 (size_t i)
37 {
38 i |= 2;
39
40 extern char b[2];
41 // { dg-message "at offset 2 " "note" { target *-*-* } .-1 }
42 // { dg-message "at offset 3 " "note" { target *-*-* } .-2 }
43
44 char *p1 = b + i;
45 char *p2 = p1 + 1;
46 char *p3 = p1 - 1;
47
48 size_t n = 0;
49 n += strlen (p1); // { dg-warning "reading 1 or more bytes from a region of size 0" }
50 n += strlen (p2); // { dg-warning "reading 1 or more bytes from a region of size 0" }
51 n += strlen (p3);
52
53 sink (p1, p2, p3, n);
54 }
55
56
57 void off_sz_or_4 (size_t i)
58 {
59 i |= 4;
60
61 extern char c[3];
62 // { dg-message "at offset 4 " "note" { target *-*-* } .-1 }
63 // { dg-message "at offset 5 " "note" { target *-*-* } .-2 }
64 // { dg-message "at offset 3 " "note" { target *-*-* } .-3 }
65
66 char *p1 = c + i;
67 char *p2 = p1 + 1;
68 char *p3 = p1 - 1;
69
70 size_t n = 0;
71 n += strlen (p1); // { dg-warning "reading 1 or more bytes from a region of size 0" }
72 n += strlen (p2); // { dg-warning "reading 1 or more bytes from a region of size 0" }
73 n += strlen (p3); // { dg-warning "reading 1 or more bytes from a region of size 0" }
74
75 sink (p1, p2, p3, n);
76 }