5 /* Wanalyzer-out-of-bounds tests for buffer under-reads and underwrites. */
7 /* Avoid folding of memcpy. */
8 typedef void * (*memcpy_t
) (void *dst
, const void *src
, size_t n
);
9 static memcpy_t
__attribute__((noinline
))
20 *e
= 42; /* { dg-line test1 } */
22 /* { dg-warning "stack-based buffer underwrite" "warning" { target *-*-* } test1 } */
23 /* { dg-message "out-of-bounds write from byte -4 till byte -1 but 'buf' starts at byte 0" "final event" { target *-*-* } test1 } */
24 /* { dg-message "valid subscripts for 'buf' are '\\\[0\\\]' to '\\\[3\\\]'" "valid subscript note" { target *-*-* } test1 } */
40 *(e
- 2) = 321; /* { dg-line test3 } */
42 /* { dg-warning "stack-based buffer underwrite" "warning" { target *-*-* } test3 } */
43 /* { dg-message "out-of-bounds write from byte -4 till byte -1 but 'buf' starts at byte 0" "final event" { target *-*-* } test3 } */
44 /* { dg-message "valid subscripts for 'buf' are '\\\[0\\\]' to '\\\[3\\\]'" "valid subscript note" { target *-*-* } test3 } */
49 memcpy_t fn
= get_memcpy ();
51 memset (buf
, 1, 4 * sizeof (int));
53 fn (&(buf
[n
]), buf
, sizeof (int)); /* { dg-line test4 } */
55 /* { dg-warning "stack-based buffer underwrite" "warning" { target *-*-* } test4 } */
56 /* { dg-message "out-of-bounds write from byte -16 till byte -13 but 'buf' starts at byte 0" "final event" { target *-*-* } test4 } */
57 /* { dg-message "valid subscripts for 'buf' are '\\\[0\\\]' to '\\\[3\\\]'" "valid subscript note" { target *-*-* } test4 } */
63 memset (buf
, 1, 4 * sizeof (int));
66 for (int i
= 4; i
>= 0; i
++)
67 sum
+= *(buf
- i
); /* { dg-line test5 } */
69 /* { dg-warning "stack-based buffer under-read" "warning" { target *-*-* } test5 } */
70 /* { dg-message "out-of-bounds read from byte -16 till byte -13 but 'buf' starts at byte 0" "final event" { target *-*-* } test5 } */
71 /* { dg-message "valid subscripts for 'buf' are '\\\[0\\\]' to '\\\[3\\\]'" "valid subscript note" { target *-*-* } test5 } */
77 memset (buf
, 1, 4 * sizeof (int));
81 for (int i
= 0; i
< 4; i
++)
87 memcpy_t fn
= get_memcpy ();
89 memset (buf
, 1, 4 * sizeof (int));
91 fn (buf
, &(buf
[n
]), sizeof (int)); /* { dg-line test8 } */
93 /* { dg-warning "stack-based buffer under-read" "warning" { target *-*-* } test8 } */
94 /* { dg-message "out-of-bounds read from byte -16 till byte -13 but 'buf' starts at byte 0" "note" { target *-*-* } test8 } */
95 /* { dg-message "valid subscripts for 'buf' are '\\\[0\\\]' to '\\\[3\\\]'" "valid subscript note" { target *-*-* } test8 } */