xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-realloc-usage.cpp (revision 01303f6d1bba5f8640aab022469f9e9a9d66f877)
1 // RUN: %check_clang_tidy %s bugprone-suspicious-realloc-usage %t
2 
3 void *realloc(void *, __SIZE_TYPE__);
4 
5 namespace std {
6   using ::realloc;
7 }
8 
9 namespace n1 {
10   void *p;
11 }
12 
13 namespace n2 {
14   void *p;
15 }
16 
17 struct P {
18   void *p;
19   void *q;
20   P *pp;
21   void *&f();
22 };
23 
24 struct P1 {
25   static void *p;
26   static void *q;
27 };
28 
29 template<class>
30 struct P2 {
31   static void *p;
32   static void *q;
33 };
34 
35 template<class A, class B>
templ(void * p)36 void templ(void *p) {
37   A::p = realloc(A::p, 10);
38   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'A::p' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage]
39   p = realloc(A::p, 10);
40   A::q = realloc(A::p, 10);
41   A::p = realloc(B::p, 10);
42   P2<A>::p = realloc(P2<A>::p, 1);
43   // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: 'P2<A>::p' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage]
44   P2<A>::p = realloc(P2<B>::p, 1);
45 }
46 
47 void *&getPtr();
48 P &getP();
49 
warn(void * p,P * p1,int * pi)50 void warn(void *p, P *p1, int *pi) {
51   p = realloc(p, 111);
52   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'p' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage]
53 
54   p = std::realloc(p, sizeof(int));
55   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'p' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage]
56 
57   p1->p = realloc(p1->p, 10);
58   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 'p1->p' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage]
59 
60   p1->pp->p = realloc(p1->pp->p, 10);
61   // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: 'p1->pp->p' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage]
62 
63   pi = (int*)realloc(pi, 10);
64   // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: 'pi' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage]
65 
66   templ<P1, P2<int>>(p);
67 }
68 
no_warn(void * p,P * p1,P * p2)69 void no_warn(void *p, P *p1, P *p2) {
70   void *q = realloc(p, 10);
71   q = realloc(p, 10);
72   p1->q = realloc(p1->p, 10);
73   p2->p = realloc(p1->p, 20);
74   n1::p = realloc(n2::p, 30);
75   p1->pp->p = realloc(p1->p, 10);
76   getPtr() = realloc(getPtr(), 30);
77   getP().p = realloc(getP().p, 20);
78   p1->f() = realloc(p1->f(), 30);
79 }
80 
no_warn_if_copy_exists_before1(void * p)81 void no_warn_if_copy_exists_before1(void *p) {
82   void *q = p;
83   p = realloc(p, 111);
84 }
85 
no_warn_if_copy_exists_before2(void * p,void * q)86 void no_warn_if_copy_exists_before2(void *p, void *q) {
87   q = p;
88   p = realloc(p, 111);
89 }
90 
91 void *g_p;
92 
no_warn_if_copy_exists_before3()93 void no_warn_if_copy_exists_before3() {
94   void *q = g_p;
95   g_p = realloc(g_p, 111);
96 }
97 
warn_if_copy_exists_after(void * p)98 void warn_if_copy_exists_after(void *p) {
99   p = realloc(p, 111);
100   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'p' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage]
101   void *q = p;
102 }
103 
test_null_child(void * p)104 void test_null_child(void *p) {
105   for (;;)
106     break;
107   p = realloc(p, 111);
108   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'p' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage]
109 }
110