1 /* Exercise basic C++ only cases of -Wuse-after-free without optimization.
3 { dg-options "-O0 -Wall" } */
5 typedef __SIZE_TYPE__ size_t;
7 extern "C" void free (void *);
8 extern "C" void* realloc (void *, size_t);
17 struct List { struct List *next; };
19 void nowarn_delete (void *vp, struct List *lp)
27 operator delete (evp);
32 operator delete (evpa[0]);
38 operator delete (evpa[0]);
49 struct List *next = lp->next;
51 operator delete (next);
55 void nowarn_delete_arg (void *p, void *q)
62 void nowarn_delete_extern (void)
70 void nowarn_delete_assign (void)
78 void warn_double_delete_arg (void *p)
80 operator delete (p); // { dg-message "call to 'void operator delete\\(void\\*\\)'" "note" }
81 operator delete (p); // { dg-warning "\\\-Wuse-after-free" }
84 void warn_delete_free_arg (void *p)
86 operator delete (p); // { dg-message "call to 'void operator delete\\(void\\*\\)'" "note" }
87 free (p); // { dg-warning "\\\-Wuse-after-free" }
90 void warn_free_delete_arg (void *p)
92 free (p); // { dg-message "call to 'void free\\(void\\*\\)'" "note" }
93 operator delete (p); // { dg-warning "\\\-Wuse-after-free" }
96 void warn_mismatched_double_delete_arg (void *p, void *q)
98 operator delete (p); // { dg-message "call to 'void operator delete\\(void\\*\\)'" "note" }
99 operator delete[] (p); // { dg-warning "\\\-Wuse-after-free" }
101 operator delete[] (q); // { dg-message "call to 'void operator delete \\\[]\\(void\\*\\)'" "note" }
102 operator delete (q); // { dg-warning "\\\-Wuse-after-free" }
105 void warn_double_delete_extern (void)
107 /* GCC assumes operator delete() clobbers global memory and the warning is
108 too simplistic to see through that assumption. */
109 extern void *ep, *eq;
112 operator delete (ep); // { dg-message "call to 'operator delete'" "pr??????" { xfail *-*-* } }
113 operator delete (eq); // { dg-warning "\\\-Wuse-after-free" "pr??????" { xfail *-*-* } }
117 void warn_deref_after_delete (int *p, int i)
119 int *q0 = p, *q1 = p + 1, *qi = p + i;
120 operator delete (p); // { dg-message "call to 'void operator delete\\(void\\*\\)'" "note" }
121 *p = 0; // { dg-warning "\\\-Wuse-after-free" }
123 *q0 = 0; // { dg-warning "\\\-Wuse-after-free" }
124 *q1 = 0; // { dg-warning "\\\-Wuse-after-free" }
125 *qi = 0; // { dg-warning "\\\-Wuse-after-free" }
128 void warn_array_ref_after_delete (int *p, int i)
130 operator delete (p); // { dg-message "call to 'void operator delete\\(void\\*\\)'" "note" }
131 p[i] = 0; // { dg-warning "\\\-Wuse-after-free" }
134 void nowarn_delete_list (struct List *head)
136 for (struct List *p = head, *q; p; p = q)
143 void warn_delete_list (struct List *head)
145 struct List *p = head;
146 for (; p; p = p->next) // { dg-warning "\\\[-Wuse-after-free" }
147 operator delete (p); // { dg-message "call to 'void operator delete\\(void\\*\\)'" "note" }
150 void warn_delete (void *vp)
153 operator delete (vp); // { dg-message "call to 'void operator delete\\(void\\*\\)'" "note" }
154 evp = vp; // { dg-warning "-Wuse-after-free" }
155 evpa[0] = vp; // { dg-warning "-Wuse-after-free" }