Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / interprocess / example / doc_scoped_ptr.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2006-2009. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 #include <boost/interprocess/detail/workaround.hpp>
12 //[doc_scoped_ptr
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <boost/interprocess/smart_ptr/scoped_ptr.hpp>
15 //<-
16 #include "../test/get_process_id_name.hpp"
17 //->
18
19 using namespace boost::interprocess;
20
21 class my_class
22 {};
23
24 class my_exception
25 {};
26
27 //A functor that destroys the shared memory object
28 template<class T>
29 class my_deleter
30 {
31    private:
32    //A typedef to save typing
33    typedef managed_shared_memory::segment_manager segment_manager;
34    //This my_deleter is created in the stack, not in shared memory,
35    //so we can use raw pointers
36    segment_manager *mp_segment_manager;
37
38    public:
39    //This typedef will specify the pointer type that
40    //scoped_ptr will store
41    typedef T *pointer;
42    //Constructor
43    my_deleter(segment_manager *s_mngr)
44    : mp_segment_manager(s_mngr){}
45
46    void operator()(pointer object_to_delete)
47    {  mp_segment_manager->destroy_ptr(object_to_delete);  }
48 };
49
50 int main ()
51 {
52    //Create shared memory
53    //Remove shared memory on construction and destruction
54    struct shm_remove
55    {
56    //<-
57    #if 1
58       shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
59       ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
60    #else
61    //->
62       shm_remove() { shared_memory_object::remove("MySharedMemory"); }
63       ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
64    //<-
65    #endif
66    //->
67    } remover;
68
69    //<-
70    #if 1
71    managed_shared_memory shmem(create_only, test::get_process_id_name(), 10000);
72    #else
73    //->
74    managed_shared_memory shmem(create_only, "MySharedMemory", 10000);
75    //<-
76    #endif
77    //->
78
79    //In the first try, there will be no exceptions
80    //in the second try we will throw an exception
81    for(int i = 0; i < 2; ++i){
82       //Create an object in shared memory
83       my_class * my_object = shmem.construct<my_class>("my_object")();
84       my_class * my_object2 = shmem.construct<my_class>(anonymous_instance)();
85       shmem.destroy_ptr(my_object2);
86       
87       //Since the next shared memory allocation can throw
88       //assign it to a scoped_ptr so that if an exception occurs
89       //we destroy the object automatically
90       my_deleter<my_class> d(shmem.get_segment_manager());
91
92       try{
93          scoped_ptr<my_class, my_deleter<my_class> > s_ptr(my_object, d);
94          //Let's emulate a exception capable operation
95          //In the second try, throw an exception
96          if(i == 1){
97             throw(my_exception());
98          }
99          //If we have passed the dangerous zone
100          //we can release the scoped pointer
101          //to avoid destruction
102          s_ptr.release();
103       }
104       catch(const my_exception &){}
105       //Here, scoped_ptr is destroyed
106       //so it we haven't thrown an exception
107       //the object should be there, otherwise, destroyed
108       if(i == 0){
109          //Make sure the object is alive
110          if(!shmem.find<my_class>("my_object").first){
111             return 1;
112          }
113          //Now we can use it and delete it manually
114          shmem.destroy<my_class>("my_object");
115       }
116       else{
117          //Make sure the object has been deleted
118          if(shmem.find<my_class>("my_object").first){
119             return 1;
120          }
121       }
122    }
123    return 0;
124 }
125 //]
126 #include <boost/interprocess/detail/config_end.hpp>