Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/malloc-vs-local-3.c

raw
1 /* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */
2
3 #include <stdlib.h>
4 #include "analyzer-decls.h"
5
6 extern int foo (int);
7
8 static int __attribute__((noinline))
9 do_stuff_2 (int *p, int n)
10 {
11 return 0;
12 }
13
14 /* As malloc-vs-local-2.c, but with a memory leak for the "on the heap case"
15 by not attempting to free at the end. */
16
17 int test_1 (int n)
18 {
19 int buf[10];
20 int *ptr;
21 int result;
22
23 if (n > 10)
24 ptr = (int *)malloc (sizeof (int) * n);
25 else
26 ptr = buf;
27
28 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
29
30 {
31 int *p = ptr;
32 int sum = 0;
33 int i;
34 for (i = 0; i < n; i++)
35 p[i] = i; /* { dg-warning "dereference of possibly-NULL" } */
36 for (i = 0; i < n; i++)
37 sum += foo (p[i]); /* { dg-bogus "uninitialized" } */
38 result = sum;
39 }
40
41 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
42
43 return result; /* { dg-message "leak of 'p'|leak of 'ptr'" } */
44 }
45
46 /* A simpler version of the above. */
47
48 int test_2 (int n)
49 {
50 int buf[10];
51 int *ptr;
52 int result;
53
54 if (n > 10)
55 ptr = (int *)malloc (sizeof (int) * n);
56 else
57 ptr = buf;
58
59 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
60
61 result = do_stuff_2 (ptr, n);
62
63 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
64
65 return result; /* { dg-message "leak of 'ptr'" } */
66 }