Skip several analyzer socket tests on hppa*-*-hpux*
[official-gcc.git] / gcc / testsuite / c-c++-common / analyzer / attr-malloc-6.c
blob1665d4128f59fb3e2e123a254220d173096e44eb
1 /* Adapted from gcc.dg/Wmismatched-dealloc.c. */
3 #define A(...) __attribute__ ((malloc (__VA_ARGS__)))
5 typedef struct FILE FILE;
6 typedef __SIZE_TYPE__ size_t;
8 void free (void*);
9 void* malloc (size_t);
10 void* realloc (void*, size_t);
12 int fclose (FILE*);
13 FILE* freopen (const char*, const char*, FILE*);
14 int pclose (FILE*);
16 A (fclose) A (freopen, 3)
17 FILE* fdopen (int);
18 A (fclose) A (freopen, 3)
19 FILE* fopen (const char*, const char*);
20 A (fclose) A (freopen, 3)
21 FILE* fmemopen(void *, size_t, const char *);
22 A (fclose) A (freopen, 3)
23 FILE* freopen (const char*, const char*, FILE*);
24 A (pclose) A (freopen, 3)
25 FILE* popen (const char*, const char*);
26 A (fclose) A (freopen, 3)
27 FILE* tmpfile (void);
29 void sink (FILE*);
32 void release (void*);
33 A (release) FILE* acquire (void);
35 void nowarn_fdopen (void)
38 FILE *q = fdopen (0);
39 if (!q)
40 return;
42 fclose (q);
46 FILE *q = fdopen (0);
47 if (!q)
48 return;
50 q = freopen ("1", "r", q);
51 fclose (q);
55 FILE *q = fdopen (0);
56 if (!q)
57 return;
59 sink (q);
64 void warn_fdopen (void)
67 FILE *q = fdopen (0); // { dg-message "allocated here" }
68 release (q); // { dg-warning "'release' called on 'q' returned from a mismatched allocation function" }
71 FILE *q = fdopen (0); // { dg-message "allocated here" }
72 free (q); // { dg-warning "'free' called on 'q' returned from a mismatched allocation function" }
76 FILE *q = fdopen (0); // { dg-message "allocated here" }
77 q = (FILE *) realloc (q, 7); // { dg-warning "'realloc' called on 'q' returned from a mismatched allocation function" }
78 sink (q);
83 void nowarn_fopen (void)
86 FILE *q = fopen ("1", "r");
87 sink (q);
88 fclose (q);
92 FILE *q = fopen ("2", "r");
93 sink (q);
94 q = freopen ("3", "r", q);
95 sink (q);
96 fclose (q);
100 FILE *q = fopen ("4", "r");
101 sink (q);
106 void warn_fopen (void)
109 FILE *q = fopen ("1", "r");
110 release (q); // { dg-warning "'release' called on 'q' returned from a mismatched allocation function" }
111 fclose (q);
114 FILE *q = fdopen (0);
115 free (q); // { dg-warning "'free' called on 'q' returned from a mismatched allocation function" }
119 FILE *q = fdopen (0);
120 q = (FILE *) realloc (q, 7); // { dg-warning "'realloc' called on 'q' returned from a mismatched allocation function" }
121 sink (q);
126 void test_popen (void)
129 FILE *p = popen ("1", "r");
130 sink (p);
131 pclose (p);
135 FILE *p;
136 p = popen ("2", "r"); // { dg-message "allocated here" }
137 fclose (p); // { dg-warning "'fclose' called on 'p' returned from a mismatched allocation function" }
141 /* freopen() can close a stream open by popen() but pclose() can't
142 close the stream returned from freopen(). */
143 FILE *p = popen ("2", "r");
144 p = freopen ("3", "r", p); // { dg-message "allocated here" }
145 pclose (p); // { dg-warning "'pclose' called on 'p' returned from a mismatched allocation function" }
150 void test_tmpfile (void)
153 FILE *p = tmpfile ();
154 fclose (p);
158 FILE *p = tmpfile ();
159 p = freopen ("1", "r", p);
160 fclose (p);
164 FILE *p = tmpfile (); // { dg-message "allocated here" }
165 pclose (p); // { dg-warning "'pclose' called on 'p' returned from a mismatched allocation function" }
170 void warn_malloc (void)
173 FILE *p = (FILE *) malloc (100); // { dg-message "allocated here" }
174 fclose (p); // { dg-warning "'p' should have been deallocated with 'free' but was deallocated with 'fclose'" }
178 FILE *p = (FILE *) malloc (100); // { dg-message "allocated here" }
179 p = freopen ("1", "r", p);// { dg-warning "'p' should have been deallocated with 'free' but was deallocated with 'freopen'" }
180 fclose (p);
184 FILE *p = (FILE *) malloc (100); // { dg-message "allocated here" }
185 pclose (p); // { dg-warning "'p' should have been deallocated with 'free' but was deallocated with 'pclose'" }
190 void test_acquire (void)
193 FILE *p = acquire ();
194 release (p);
198 FILE *p = acquire ();
199 release (p);
203 FILE *p = acquire (); // { dg-message "allocated here \\(expects deallocation with 'release'\\)" }
204 fclose (p); // { dg-warning "'p' should have been deallocated with 'release' but was deallocated with 'fclose'" }
208 FILE *p = acquire (); // { dg-message "allocated here \\(expects deallocation with 'release'\\)" }
209 pclose (p); // { dg-warning "'p' should have been deallocated with 'release' but was deallocated with 'pclose'" }
213 FILE *p = acquire (); // { dg-message "allocated here \\(expects deallocation with 'release'\\)" }
214 p = freopen ("1", "r", p); // { dg-warning "'p' should have been deallocated with 'release' but was deallocated with 'freopen'" }
215 sink (p);
219 FILE *p = acquire (); // { dg-message "allocated here \\(expects deallocation with 'release'\\)" }
220 free (p); // { dg-warning "'p' should have been deallocated with 'release' but was deallocated with 'free'" }
224 FILE *p = acquire (); // { dg-message "allocated here \\(expects deallocation with 'release'\\)" }
225 p = (FILE *) realloc (p, 123); // { dg-warning "'p' should have been deallocated with 'release' but was deallocated with 'realloc'" }
226 sink (p);