Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / smart_ptr / test / intrusive_ptr_move_test.cpp
1 #include <boost/config.hpp>
2
3 #if defined(BOOST_MSVC)
4
5 #pragma warning(disable: 4786)  // identifier truncated in debug info
6 #pragma warning(disable: 4710)  // function not inlined
7 #pragma warning(disable: 4711)  // function selected for automatic inline expansion
8 #pragma warning(disable: 4514)  // unreferenced inline removed
9 #pragma warning(disable: 4355)  // 'this' : used in base member initializer list
10 #pragma warning(disable: 4511)  // copy constructor could not be generated
11 #pragma warning(disable: 4512)  // assignment operator could not be generated
12
13 #if (BOOST_MSVC >= 1310)
14 #pragma warning(disable: 4675)  // resolved overload found with Koenig lookup
15 #endif
16
17 #endif
18
19 //
20 //  intrusive_ptr_move_test.cpp
21 //
22 //  Copyright (c) 2002-2005 Peter Dimov
23 //
24 // Distributed under the Boost Software License, Version 1.0. (See
25 // accompanying file LICENSE_1_0.txt or copy at
26 // http://www.boost.org/LICENSE_1_0.txt)
27 //
28
29 #include <boost/detail/lightweight_test.hpp>
30 #include <boost/intrusive_ptr.hpp>
31 #include <boost/detail/atomic_count.hpp>
32 #include <boost/config.hpp>
33 #include <algorithm>
34 #include <functional>
35
36 #if defined( BOOST_HAS_RVALUE_REFS )
37
38 namespace N
39 {
40
41 class base
42 {
43 private:
44
45     boost::detail::atomic_count use_count_;
46
47     base(base const &);
48     base & operator=(base const &);
49
50 protected:
51
52     base(): use_count_(0)
53     {
54         ++instances;
55     }
56
57     virtual ~base()
58     {
59         --instances;
60     }
61
62 public:
63
64     static long instances;
65
66     long use_count() const
67     {
68         return use_count_;
69     }
70
71 #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
72
73     inline friend void intrusive_ptr_add_ref(base * p)
74     {
75         ++p->use_count_;
76     }
77
78     inline friend void intrusive_ptr_release(base * p)
79     {
80         if(--p->use_count_ == 0) delete p;
81     }
82
83 #else
84
85     void add_ref()
86     {
87         ++use_count_;
88     }
89
90     void release()
91     {
92         if(--use_count_ == 0) delete this;
93     }
94
95 #endif
96 };
97
98 long base::instances = 0;
99
100 } // namespace N
101
102 #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
103
104 namespace boost
105 {
106
107 inline void intrusive_ptr_add_ref(N::base * p)
108 {
109     p->add_ref();
110 }
111
112 inline void intrusive_ptr_release(N::base * p)
113 {
114     p->release();
115 }
116
117 } // namespace boost
118
119 #endif
120
121 //
122
123 struct X: public virtual N::base
124 {
125 };
126
127 struct Y: public X
128 {
129 };
130
131 int main()
132 {
133     BOOST_TEST( N::base::instances == 0 );
134
135     {
136         boost::intrusive_ptr<X> p( new X );
137         BOOST_TEST( N::base::instances == 1 );
138
139         boost::intrusive_ptr<X> p2( std::move( p ) );
140         BOOST_TEST( N::base::instances == 1 );
141         BOOST_TEST( p.get() == 0 );
142
143         p2.reset();
144         BOOST_TEST( N::base::instances == 0 );
145     }
146
147     {
148         boost::intrusive_ptr<X> p( new X );
149         BOOST_TEST( N::base::instances == 1 );
150
151         boost::intrusive_ptr<X> p2;
152         p2 = std::move( p );
153         BOOST_TEST( N::base::instances == 1 );
154         BOOST_TEST( p.get() == 0 );
155
156         p2.reset();
157         BOOST_TEST( N::base::instances == 0 );
158     }
159
160     {
161         boost::intrusive_ptr<X> p( new X );
162         BOOST_TEST( N::base::instances == 1 );
163
164         boost::intrusive_ptr<X> p2( new X );
165         BOOST_TEST( N::base::instances == 2 );
166         p2 = std::move( p );
167         BOOST_TEST( N::base::instances == 1 );
168         BOOST_TEST( p.get() == 0 );
169
170         p2.reset();
171         BOOST_TEST( N::base::instances == 0 );
172     }
173
174     return boost::report_errors();
175 }
176
177 #else // !defined( BOOST_HAS_RVALUE_REFS )
178
179 int main()
180 {
181     return 0;
182 }
183
184 #endif