1 /* Verify that we don't print "<unknown>" in various diagnostics
2 (PR analyzer/99771). */
3
4 #include <stdlib.h>
5
6 void test_1 (void)
7 {
8 *(char*)malloc (1024) = 42; /* { dg-warning "dereference of possibly-NULL 'malloc\\(1024\\)'" } */
9 } /* { dg-warning "leak of 'malloc\\(1024\\)'" "warning" } */
10 /* { dg-message "'malloc\\(1024\\)' leaks here" "final event" { target *-*-* } .-1 } */
11
12 void test_2 (size_t n)
13 {
14 *(char*)malloc (4 * n) = 42; /* { dg-warning "dereference of possibly-NULL 'malloc\\(n \\* 4\\)'" "warning" } */
15 /* { dg-message "'malloc\\(n \\* 4\\)' could be NULL" "final event" { target *-*-* } .-1 } */
16 } /* { dg-warning "leak of 'malloc\\(n \\* 4\\)'" "warning" } */
17 /* { dg-message "'malloc\\(n \\* 4\\)' leaks here" "final event" { target *-*-* } .-1 } */
18
19 /* A compound example. */
20
21 void test_3 (size_t a, size_t b, size_t c)
22 {
23 *(char*)malloc (a + (b * c)) = 42; /* { dg-warning "dereference of possibly-NULL 'malloc\\(a \\+ b \\* c\\)'" "warning" } */
24 /* { dg-message "'malloc\\(a \\+ b \\* c\\)' could be NULL" "final event" { target *-*-* } .-1 } */
25 } /* { dg-warning "leak of 'malloc\\(a \\+ b \\* c\\)'" "warning" } */
26 /* { dg-message "'malloc\\(a \\+ b \\* c\\)' leaks here" "final event" { target *-*-* } .-1 } */
27
28 void test_4 (size_t a, size_t b, size_t c)
29 {
30 *(char *)malloc (a ? b : c) = 42; /* { dg-warning "dereference of possibly-NULL 'malloc\\(<unknown>\\)'" "warning" } */
31 /* { dg-message "'malloc\\(<unknown>\\)' could be NULL" "final event" { target *-*-* } .-1 } */
32 } /* { dg-warning "leak of 'malloc\\(<unknown>\\)'" "warning" } */
33 /* { dg-message "'malloc\\(<unknown>\\)' leaks here" "final event" { target *-*-* } .-1 } */
34
35 /* Unary operators. */
36
37 void test_5 (size_t a)
38 {
39 *(char*)malloc (-a) = 42; /* { dg-warning "dereference of possibly-NULL 'malloc\\(-a\\)'" } */
40 } /* { dg-warning "leak of 'malloc\\(-a\\)'" "warning" } */
41 /* { dg-message "'malloc\\(-a\\)' leaks here" "final event" { target *-*-* } .-1 } */
42
43 void test_6 (size_t a)
44 {
45 *(char*)malloc (~a) = 42; /* { dg-warning "dereference of possibly-NULL 'malloc\\(~a\\)'" } */
46 } /* { dg-warning "leak of 'malloc\\(~a\\)'" "warning" } */
47 /* { dg-message "'malloc\\(~a\\)' leaks here" "final event" { target *-*-* } .-1 } */
48
49 /* Field access. */
50
51 struct s7 { size_t sz; };
52
53 void test_7a(struct s7 s)
54 {
55 *(char*)malloc (s.sz) = 42; /* { dg-warning "dereference of possibly-NULL 'malloc\\(s\\.sz\\)'" } */
56 } /* { dg-warning "leak of 'malloc\\(s\\.sz\\)'" "warning" } */
57 /* { dg-message "'malloc\\(s\\.sz\\)' leaks here" "final event" { target *-*-* } .-1 } */
58
59 void test_7b (struct s7 *s)
60 {
61 *(char*)malloc (s->sz) = 42; /* { dg-warning "dereference of possibly-NULL 'malloc\\(\\*s\\.sz\\)'" } */
62 } /* { dg-warning "leak of 'malloc\\(\\*s\\.sz\\)'" "warning" } */
63 /* { dg-message "'malloc\\(\\*s\\.sz\\)' leaks here" "final event" { target *-*-* } .-1 } */