1 /* PR ????? - No warning on attempts to access free object
2 Verify that freeing unallocated objects referenced either directly
3 or through pointers is diagnosed. In most cases this doesn't require
6 { dg-options "-Wall -Wfree-nonheap-object" }
7 { dg-require-effective-target alloca } */
9 typedef __INTPTR_TYPE__
intptr_t;
10 typedef __SIZE_TYPE__
size_t;
12 extern void free (void*);
13 extern void* malloc (size_t);
14 extern void* realloc (void *p
, size_t);
16 void sink (void*, ...);
25 void nowarn_free (void *p
, void **pp
, size_t n
, intptr_t iptr
)
39 p
= realloc (p
, n
* 2);
55 void warn_free_extern_arr (void)
57 free (ecarr
); // { dg-warning "\\\[-Wfree-nonheap-object" }
60 void warn_free_extern_arr_offset (int i
)
63 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
67 void warn_free_cstint (void)
71 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
75 void warn_free_func (void)
77 void *p
= warn_free_func
;
79 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
83 void warn_free_string (int i
)
88 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
93 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
98 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
105 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
109 void warn_free_local_arr (int i
)
114 free (a
); // { dg-warning "\\\[-Wfree-nonheap-object" }
121 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
128 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
133 void warn_free_vla (int n
, int i
)
136 int vla
[n
], *p
= vla
;
138 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
142 int vla
[n
+ 1], *p
= vla
+ 1;
144 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
147 int vla
[n
+ 2], *p
= vla
+ i
;
149 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
154 void nowarn_free_extern_ptrarr (void)
159 void nowarn_free_extern_ptrarr_offset (int i
)
166 void warn_free_extern_ptrarr (void)
168 free (eparr
); // { dg-warning "\\\[-Wfree-nonheap-object" }
171 void warn_free_extern_ptrarr_offset (int i
)
174 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
178 void nowarn_free_local_ptrarr (int i
)
188 void nowarn_free_extern_ptr (void)
193 void nowarn_free_extern_ptr_offset (int i
)
199 void nowarn_free_parm_offset (char *p
, int i
)
205 void nowarn_free_parm_neg_offset (char *p
, int i
)
219 extern struct Members em
;
221 void nowarn_free_member_ptr (struct Members
*pm
, int i
)
234 void nowarn_free_struct_cast (intptr_t *p
)
236 struct Members
*q
= (struct Members
*)*p
;
238 free (q
); // { dg-bogus "\\\[-Wfree-nonheap-object" }
242 void warn_free_member_array (void)
245 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
248 void warn_free_member_array_off (int i
)
251 free (p
); // { dg-warning "\\\[-Wfree-nonheap-object" }
255 // Range information requires optimization.
256 #pragma GCC optimize "1"
258 void warn_free_extern_ptr_pos_offset (int i
)
264 free (q
); // { dg-warning "\\\[-Wfree-nonheap-object" }
267 void warn_free_parm_pos_offset (char *p
, int i
)
273 free (q
); // { dg-warning "\\\[-Wfree-nonheap-object" }