1 /* PR ????? - No warning on attempts to access free object
2 Verify that freeing unallocated objects referenced indirectly through
3 pointers obtained from function calls is diagnosed.
4 { dg-do compile }
5 { dg-options "-O2 -Wall -Wfree-nonheap-object" } */
6
7 typedef __SIZE_TYPE__ size_t;
8
9 extern void free (void*);
10 extern char* memchr (const void*, int, size_t);
11 extern char* strchr (const char*, int);
12
13 void sink (void*, ...);
14
15 extern char ecarr[];
16 extern void* eparr[];
17
18 extern char *eptr;
19
20
warn_free_memchr_ecarr(int x,size_t n)21 void warn_free_memchr_ecarr (int x, size_t n)
22 {
23 char *p = memchr (ecarr, x, n);
24 sink (p);
25 free (p); // { dg-warning "\\\[-Wfree-nonheap-object" }
26 }
27
warn_free_memchr_ecarr_offset(int i,int j,int x,size_t n)28 void warn_free_memchr_ecarr_offset (int i, int j, int x, size_t n)
29 {
30 char *p = memchr (ecarr + i, x, n);
31 char *q = p + j;
32 sink (p, q);
33 free (q); // { dg-warning "\\\[-Wfree-nonheap-object" }
34 }
35
36
warn_free_memchr_local_arr(int x,size_t n)37 void warn_free_memchr_local_arr (int x, size_t n)
38 {
39 char a[8];
40 sink (a);
41
42 char *p = memchr (a, x, n);
43 sink (p);
44 free (p); // { dg-warning "\\\[-Wfree-nonheap-object" }
45 }
46
warn_free_memchr_local_arr_offset(int i,int j,int x,size_t n)47 void warn_free_memchr_local_arr_offset (int i, int j, int x, size_t n)
48 {
49 char a[8];
50 sink (a);
51
52 char *p = memchr (a + i, x, n);
53 char *q = p + j;
54 sink (p, q);
55 free (q); // { dg-warning "\\\[-Wfree-nonheap-object" }
56 }
57
58