Skip several analyzer socket tests on hppa*-*-hpux*
[official-gcc.git] / gcc / testsuite / c-c++-common / analyzer / malloc-vs-local-2.c
blob9001fe66cd78a8ab024595938191db4e66c0a8c9
1 #include <stdlib.h>
2 #include "analyzer-decls.h"
4 extern int foo (int);
6 static int __attribute__((noinline))
7 do_stuff_2 (int *p, int n)
9 return 0;
12 /* As malloc-vs-local.c, but hand-inlining the logic. */
14 /* Repeated (n > 10) predicate. */
16 int test_repeated_predicate_1 (int n)
18 int buf[10];
19 int *ptr;
20 int result;
22 if (n > 10)
23 ptr = (int *)malloc (sizeof (int) * n);
24 else
25 ptr = buf;
27 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
30 int *p = ptr;
31 int sum = 0;
32 int i;
33 for (i = 0; i < n; i++)
34 p[i] = i; /* { dg-warning "dereference of possibly-NULL" } */
35 for (i = 0; i < n; i++)
36 sum += foo (p[i]); /* { dg-bogus "uninitialized" } */
37 result = sum;
40 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
42 if (n > 10)
43 free (ptr); /* { dg-bogus "not on the heap" } */
45 return result; /* { dg-bogus "leak" } */
48 /* As above, but with just one loop. */
50 int test_repeated_predicate_1a (int n)
52 int buf[10];
53 int *ptr;
54 int result;
56 if (n > 10)
57 ptr = (int *)malloc (sizeof (int) * n);
58 else
59 ptr = buf;
61 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
64 int *p = ptr;
65 int sum = 0;
66 int i;
67 for (i = 0; i < n; i++)
68 p[i] = i; /* { dg-warning "dereference of possibly-NULL" } */
69 result = sum;
72 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
74 if (n > 10)
75 free (ptr); /* { dg-bogus "not on the heap" } */
77 return result; /* { dg-bogus "leak" } */
80 /* A simpler version of the above. */
82 int test_repeated_predicate_2 (int n)
84 int buf[10];
85 int *ptr;
86 int result;
88 if (n > 10)
89 ptr = (int *)malloc (sizeof (int) * n);
90 else
91 ptr = buf;
93 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
95 result = do_stuff_2 (ptr, n);
97 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
99 if (n > 10)
100 free (ptr); /* { dg-bogus "not on the heap" } */
102 return result; /* { dg-bogus "leak" } */
105 /* A predicate that sets a flag for the 2nd test. */
107 int test_explicit_flag (int n)
109 int buf[10];
110 int *ptr;
111 int result;
112 int need_to_free = 0;
114 if (n > 10)
116 ptr = (int *)malloc (sizeof (int) * n);
117 need_to_free = 1;
119 else
120 ptr = buf;
122 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
125 int *p = ptr;
126 int sum = 0;
127 int i;
128 for (i = 0; i < n; i++)
129 p[i] = i; /* { dg-warning "dereference of possibly-NULL" } */
130 for (i = 0; i < n; i++)
131 sum += foo (p[i]); /* { dg-bogus "uninitialized" } */
132 result = sum;
135 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
137 if (need_to_free)
138 free (ptr); /* { dg-bogus "not on the heap" } */
140 return result; /* { dg-bogus "leak" } */
143 /* Pointer comparison. */
145 int test_pointer_comparison (int n)
147 int buf[10];
148 int *ptr;
149 int result;
151 if (n > 10)
152 ptr = (int *)malloc (sizeof (int) * n);
153 else
154 ptr = buf;
156 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
159 int *p = ptr;
160 int sum = 0;
161 int i;
162 for (i = 0; i < n; i++)
163 p[i] = i; /* { dg-warning "dereference of possibly-NULL" } */
164 for (i = 0; i < n; i++)
165 sum += foo (p[i]); /* { dg-bogus "uninitialized" } */
166 result = sum;
169 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
171 if (ptr != buf)
172 free (ptr); /* { dg-bogus "not on the heap" } */
174 return result; /* { dg-bogus "leak" } */