Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / boost / serialization / array.hpp
1 #ifndef BOOST_SERIALIZATION_ARRAY_HPP
2 #define BOOST_SERIALIZATION_ARRAY_HPP
3
4 // (C) Copyright 2005 Matthias Troyer and Dave Abrahams
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8
9 #include <iostream>
10 #include <cstddef> // std::size_t
11 #include <cstddef>
12 #include <boost/config.hpp> // msvc 6.0 needs this for warning suppression
13 #if defined(BOOST_NO_STDC_NAMESPACE)
14 namespace std{ 
15     using ::size_t; 
16 } // namespace std
17 #endif
18
19 #include <boost/serialization/nvp.hpp>
20 #include <boost/serialization/split_member.hpp>
21 #include <boost/serialization/wrapper.hpp>
22 #include <boost/mpl/always.hpp>
23 #include <boost/mpl/apply.hpp>
24 #include <boost/mpl/bool.hpp>
25 #include <boost/type_traits/remove_const.hpp>
26 #include <boost/array.hpp>
27
28 namespace boost { namespace serialization {
29
30 // traits to specify whether to use  an optimized array serialization
31
32 #ifdef __BORLANDC__
33 // workaround for Borland compiler
34 template <class Archive>
35 struct use_array_optimization {
36   template <class T> struct apply : boost::mpl::false_ {};
37 };
38
39 #else
40 template <class Archive>
41 struct use_array_optimization : boost::mpl::always<boost::mpl::false_> {};
42 #endif
43
44 template<class T>
45 class array :
46     public wrapper_traits<const array< T > >
47 {
48 public:    
49     typedef T value_type;
50     
51     array(value_type* t, std::size_t s) :
52         m_t(t),
53         m_element_count(s)
54     {}
55     array(const array & rhs) :
56         m_t(rhs.m_t),
57         m_element_count(rhs.m_element_count)
58     {}
59     array & operator=(const array & rhs){
60         m_t = rhs.m_t;
61         m_element_count = rhs.m_element_count;
62     }
63
64     // default implementation
65     template<class Archive>
66     void serialize_optimized(Archive &ar, const unsigned int, mpl::false_ ) const
67     {
68       // default implemention does the loop
69       std::size_t c = count();
70       value_type * t = address();
71       while(0 < c--)
72             ar & boost::serialization::make_nvp("item", *t++);
73     }
74
75     // optimized implementation
76     template<class Archive>
77     void serialize_optimized(Archive &ar, const unsigned int version, mpl::true_ )
78     {
79       boost::serialization::split_member(ar, *this, version);
80     }
81
82     // default implementation
83     template<class Archive>
84     void save(Archive &ar, const unsigned int version) const
85     {
86       ar.save_array(*this,version);
87     }
88
89     // default implementation
90     template<class Archive>
91     void load(Archive &ar, const unsigned int version)
92     {
93       ar.load_array(*this,version);
94     }
95     
96     // default implementation
97     template<class Archive>
98     void serialize(Archive &ar, const unsigned int version)
99     {
100       typedef BOOST_DEDUCED_TYPENAME 
101           boost::serialization::use_array_optimization<Archive>::template apply<
102                     BOOST_DEDUCED_TYPENAME remove_const< T >::type 
103                 >::type use_optimized;
104       serialize_optimized(ar,version,use_optimized());
105     }
106     
107     value_type* address() const
108     {
109       return m_t;
110     }
111
112     std::size_t count() const
113     {
114       return m_element_count;
115     }
116     
117 private:
118     value_type* m_t;
119     std::size_t m_element_count;
120 };
121
122 template<class T>
123 inline
124 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
125 const
126 #endif
127 array< T > make_array( T* t, std::size_t s){
128     return array< T >(t, s);
129 }
130
131 template <class Archive, class T, std::size_t N>
132 void serialize(Archive& ar, boost::array<T,N>& a, const unsigned int /* version */)
133 {
134   ar & boost::serialization::make_nvp("elems",a.elems);
135 }
136
137 } } // end namespace boost::serialization
138
139 #ifdef __BORLANDC__
140 // ignore optimizations for Borland
141 #define BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(Archive)      
142 #else
143 #define BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(Archive)           \
144 namespace boost { namespace serialization {                           \
145 template <> struct use_array_optimization<Archive> {                  \
146   template <class ValueType>                                          \
147   struct apply : boost::mpl::apply1<Archive::use_array_optimization   \
148       , BOOST_DEDUCED_TYPENAME boost::remove_const<ValueType>::type   \
149     >::type {};                                                       \
150 }; }}
151 #endif // __BORLANDC__
152
153 #endif //BOOST_SERIALIZATION_ARRAY_HPP