Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/out-of-bounds-read-int-arr.c

raw
1 #include <stdint.h>
2
3 int32_t arr[10]; /* { dg-message "capacity: 40 bytes" } */
4
5 int32_t int_arr_read_element_before_start_far(void)
6 {
7 return arr[-100]; /* { dg-warning "buffer under-read" "warning" } */
8 /* { dg-message "out-of-bounds read from byte -400 till byte -397 but 'arr' starts at byte 0" "final event" { target *-*-* } .-1 } */
9 /* { dg-message "valid subscripts for 'arr' are '\\\[0\\\]' to '\\\[9\\\]'" "valid subscript note" { target *-*-* } .-2 } */
10 }
11
12 int32_t int_arr_read_element_before_start_near(void)
13 {
14 return arr[-2]; /* { dg-warning "buffer under-read" "warning" } */
15 /* { dg-message "out-of-bounds read from byte -8 till byte -5 but 'arr' starts at byte 0" "final event" { target *-*-* } .-1 } */
16 /* { dg-message "valid subscripts for 'arr' are '\\\[0\\\]' to '\\\[9\\\]'" "valid subscript note" { target *-*-* } .-2 } */
17 }
18
19 int32_t int_arr_read_element_before_start_off_by_one(void)
20 {
21 return arr[-1]; /* { dg-warning "buffer under-read" "warning" } */
22 /* { dg-message "out-of-bounds read from byte -4 till byte -1 but 'arr' starts at byte 0" "final event" { target *-*-* } .-1 } */
23 /* { dg-message "valid subscripts for 'arr' are '\\\[0\\\]' to '\\\[9\\\]'" "valid subscript note" { target *-*-* } .-2 } */
24 }
25
26 int32_t int_arr_read_element_at_start(void)
27 {
28 return arr[0];
29 }
30
31 int32_t int_arr_read_element_at_end(void)
32 {
33 return arr[9];
34 }
35
36 int32_t int_arr_read_element_after_end_off_by_one(void)
37 {
38 return arr[10]; /* { dg-warning "buffer over-read" "warning" } */
39 /* { dg-message "out-of-bounds read from byte 40 till byte 43 but 'arr' ends at byte 40" "final event" { target *-*-* } .-1 } */
40 /* { dg-message "read of 4 bytes from after the end of 'arr'" "num bad bytes note" { target *-*-* } .-2 } */
41 /* { dg-message "valid subscripts for 'arr' are '\\\[0\\\]' to '\\\[9\\\]'" "valid subscript note" { target *-*-* } .-3 } */
42 }
43
44 int32_t int_arr_read_element_after_end_near(void)
45 {
46 return arr[11]; /* { dg-warning "buffer over-read" "warning" } */
47 /* { dg-message "out-of-bounds read from byte 44 till byte 47 but 'arr' ends at byte 40" "final event" { target *-*-* } .-1 } */
48 /* { dg-message "read of 4 bytes from after the end of 'arr'" "num bad bytes note" { target *-*-* } .-2 } */
49 /* { dg-message "valid subscripts for 'arr' are '\\\[0\\\]' to '\\\[9\\\]'" "valid subscript note" { target *-*-* } .-3 } */
50 }
51
52 int32_t int_arr_read_element_after_end_far(void)
53 {
54 return arr[100]; /* { dg-warning "buffer over-read" "warning" } */
55 /* { dg-message "out-of-bounds read from byte 400 till byte 403 but 'arr' ends at byte 40" "final event" { target *-*-* } .-1 } */
56 /* { dg-message "read of 4 bytes from after the end of 'arr'" "num bad bytes note" { target *-*-* } .-2 } */
57 /* { dg-message "valid subscripts for 'arr' are '\\\[0\\\]' to '\\\[9\\\]'" "valid subscript note" { target *-*-* } .-3 } */
58 }