]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/g++.dg/warn/Wmismatched-dealloc-3.C
Add -Wuse-after-free [PR80532].
[thirdparty/gcc.git] / gcc / testsuite / g++.dg / warn / Wmismatched-dealloc-3.C
1 /* Verify that passing a pointer to a deallocation function that was
2 previously passed to a mismatched reallocation function is diagnosed
3 by -Wmismatched-dealloc (and not by some other warning).
4 { dg-do compile }
5 { dg-options "-Wall" } */
6
7 #define A(...) __attribute__ ((malloc (__VA_ARGS__)))
8
9 typedef __SIZE_TYPE__ size_t;
10
11 extern "C"
12 {
13 void free (void *);
14 void* realloc (void *, size_t);
15 }
16
17 // User-defined allocator/deallocator just like like realloc.
18 int* int_realloc (size_t, int *);
19 A (int_realloc, 2) int* int_realloc (size_t, int *);
20
21
22 void sink (void *);
23
24
25 void* warn_realloc_op_delete (void *p)
26 {
27 void *q = realloc (p, 5); // { dg-message "call to 'void\\* realloc\\(void\\*, size_t\\)'" "note" }
28
29 operator delete (p); // { dg-warning "'void operator delete\\(void\\*\\)' called on pointer 'p' passed to mismatched allocation function 'void\\* realloc\\(void\\*, size_t\\)' \\\[-Wmismatched-dealloc" }
30 return q;
31 }
32
33 void* warn_realloc_op_delete_cond (void *p)
34 {
35 void *q = realloc (p, 5); // { dg-message "call to 'void\\* realloc\\(void\\*, size_t\\)'" "note" }
36
37 if (!q)
38 operator delete (p); // { dg-warning "'void operator delete\\(void\\*\\)' called on pointer 'p' passed to mismatched allocation function 'void\\* realloc\\(void\\*, size_t\\)'" }
39 return q;
40 }
41
42 void* warn_realloc_array_delete_char (char *p)
43 {
44 char *q;
45 q = (char*)realloc (p, 7); // { dg-message "call to 'void\\* realloc\\(void\\*, size_t\\)'" "note" }
46
47 if (!q)
48 delete[] (p); // { dg-warning "'void operator delete \\\[]\\(void\\*\\)' called on pointer 'p' passed to mismatched allocation function 'void\\* realloc\\(void\\*, size_t\\)'" }
49 return q;
50 }
51
52
53 int* warn_int_realloc_op_delete (int *p)
54 {
55 int *q;
56 q = int_realloc (5, p); // { dg-message "call to 'int\\* int_realloc\\(size_t, int\\*\\)'" "note" }
57
58 operator delete (p); // { dg-warning "'void operator delete\\(void\\*\\)' called on pointer 'p' passed to mismatched allocation function 'int\\* int_realloc\\(size_t, int\\*\\)' \\\[-Wmismatched-dealloc" }
59 return q;
60 }
61
62
63 int* warn_int_realloc_free (int *p)
64 {
65 int *q;
66 q = int_realloc (5, p); // { dg-message "call to 'int\\* int_realloc\\(size_t, int\\*\\)'" "note" }
67
68 free (p); // { dg-warning "'void free\\(void\\*\\)' called on pointer 'p' passed to mismatched allocation function 'int\\* int_realloc\\(size_t, int\\*\\)' \\\[-Wmismatched-dealloc" }
69 return q;
70 }