Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/taint-size-access-attr-1.c

raw
1 /* Passing tainted sizes to external functions with attribute ((access)) with
2 a size-index. */
3
4 /* { dg-additional-options "-fanalyzer-show-duplicate-count" } */
5
6 #include "analyzer-decls.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 struct foo
12 {
13 size_t sz;
14 };
15
16 char buf[100];
17
18 extern void extern_fn_read_only (void *p, size_t sz) /* { dg-message "parameter 2 of 'extern_fn_read_only' marked as a size via attribute 'access \\(read_only, 1, 2\\)'" } */
19 __attribute__ ((access (read_only, 1, 2)));
20
21 void test_fn_read_only (FILE *f, void *p)
22 {
23 struct foo tmp;
24 if (1 == fread(&tmp, sizeof(tmp), 1, f)) { /* { dg-message "\\(\[0-9\]+\\) 'tmp' gets an unchecked value here" "event: tmp gets unchecked value" { xfail *-*-* } } */
25 /* { dg-message "\\(\[0-9\]+\\) following 'true' branch\\.\\.\\." "event: following true branch" { target *-*-* } .-1 } */
26 __analyzer_dump_state ("taint", tmp.sz); /* { dg-warning "state: 'tainted'" } */
27 /* { dg-message "\\(\[0-9\]+\\) \\.\\.\\.to here" "event: to here" { target *-*-* } .-1 } */
28
29 extern_fn_read_only (p, tmp.sz); /* { dg-warning "use of attacker-controlled value 'tmp.sz' as size without upper-bounds checking" "warning" } */
30 /* { dg-bogus "duplicate" "duplicate" { target *-*-* } .-1 } */
31 }
32 }
33
34 /* We shouldn't complain if the value has been sanitized. */
35
36 void test_fn_sanitized (FILE *f, void *p)
37 {
38 struct foo tmp;
39 if (1 == fread(&tmp, sizeof(tmp), 1, f)) {
40 __analyzer_dump_state ("taint", tmp.sz); /* { dg-warning "state: 'tainted'" } */
41
42 if (tmp.sz > 100)
43 return;
44
45 __analyzer_dump_state ("taint", tmp.sz); /* { dg-warning "state: 'has_ub'" } */
46
47 extern_fn_read_only (p, tmp.sz); /* { dg-bogus "use of attacker-controlled value" } */
48 }
49 }
50
51 /* We shouldn't complain if there was no size annotation. */
52
53 extern void extern_fn_no_size (void *p)
54 __attribute__ ((access (read_only, 1)));
55
56 void test_fn_no_size (FILE *f, void *p)
57 {
58 struct foo tmp;
59 if (1 == fread(&tmp, sizeof(tmp), 1, f)) {
60 __analyzer_dump_state ("taint", tmp.sz); /* { dg-warning "state: 'tainted'" } */
61 extern_fn_no_size (p);
62 }
63 }