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;
10 void* realloc (void*, size_t);
13 FILE* freopen (const char*, const char*, FILE*);
16 A (fclose
) A (freopen
, 3)
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)
33 A (release
) FILE* acquire (void);
35 void nowarn_fdopen (void)
50 q
= freopen ("1", "r", 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" }
83 void nowarn_fopen (void)
86 FILE *q
= fopen ("1", "r");
92 FILE *q
= fopen ("2", "r");
94 q
= freopen ("3", "r", q
);
100 FILE *q
= fopen ("4", "r");
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" }
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" }
126 void test_popen (void)
129 FILE *p
= popen ("1", "r");
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 ();
158 FILE *p
= tmpfile ();
159 p
= freopen ("1", "r", 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'" }
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 ();
198 FILE *p
= acquire ();
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'" }
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'" }