Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / interprocess / example / doc_named_alloc.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_named_alloc
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <cstdlib> //std::system
15 #include <cstddef>
16 #include <cassert>
17 #include <utility>
18 //<-
19 #include "../test/get_process_id_name.hpp"
20 //->
21
22 int main(int argc, char *argv[])
23 {
24    using namespace boost::interprocess;
25    typedef std::pair<double, int> MyType;
26
27    if(argc == 1){  //Parent process
28       //Remove shared memory on construction and destruction
29       struct shm_remove 
30       {
31       //<-
32       #if 1
33          shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
34          ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
35       #else
36       //->
37          shm_remove() { shared_memory_object::remove("MySharedMemory"); }
38          ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
39       //<-
40       #endif
41       //->
42       } remover;
43
44       //Construct managed shared memory
45       //<-
46       #if 1
47       managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
48       #else
49       //->
50       managed_shared_memory segment(create_only, "MySharedMemory", 65536);
51       //<-
52       #endif
53       //->
54
55       //Create an object of MyType initialized to {0.0, 0}
56       MyType *instance = segment.construct<MyType>
57          ("MyType instance")  //name of the object
58          (0.0, 0);            //ctor first argument
59
60       //Create an array of 10 elements of MyType initialized to {0.0, 0}
61       MyType *array = segment.construct<MyType>
62          ("MyType array")     //name of the object
63          [10]                 //number of elements
64          (0.0, 0);            //Same two ctor arguments for all objects
65
66       //Create an array of 3 elements of MyType initializing each one
67       //to a different value {0.0, 0}, {1.0, 1}, {2.0, 2}...
68       float float_initializer[3] = { 0.0, 1.0, 2.0 };
69       int   int_initializer[3]   = { 0, 1, 2 };
70
71       MyType *array_it = segment.construct_it<MyType>
72          ("MyType array from it")   //name of the object
73          [3]                        //number of elements
74          ( &float_initializer[0]    //Iterator for the 1st ctor argument
75          , &int_initializer[0]);    //Iterator for the 2nd ctor argument
76
77       //Launch child process
78       std::string s(argv[0]); s += " child ";
79       //<-
80       s += test::get_process_id_name();
81       //->
82       if(0 != std::system(s.c_str()))
83          return 1;
84
85       //<-
86       (void)instance;
87       (void)array;
88       (void)array_it;
89       //->
90
91       //Check child has destroyed all objects
92       if(segment.find<MyType>("MyType array").first ||
93          segment.find<MyType>("MyType instance").first ||
94          segment.find<MyType>("MyType array from it").first)
95          return 1;
96    }
97    else{
98       //Open managed shared memory
99       //<-
100       #if 1
101       managed_shared_memory segment(open_only, argv[2]);
102       #else
103       //->
104       managed_shared_memory segment(open_only, "MySharedMemory");
105       //<-
106       #endif
107       //->
108
109       std::pair<MyType*, std::size_t> res;
110
111       //Find the array
112       res = segment.find<MyType> ("MyType array");   
113       //Length should be 10
114       if(res.second != 10) return 1;
115
116       //Find the object
117       res = segment.find<MyType> ("MyType instance");   
118       //Length should be 1
119       if(res.second != 1) return 1;
120
121       //Find the array constructed from iterators
122       res = segment.find<MyType> ("MyType array from it");
123       //Length should be 3
124       if(res.second != 3) return 1;
125
126       //We're done, delete all the objects
127       segment.destroy<MyType>("MyType array");
128       segment.destroy<MyType>("MyType instance");
129       segment.destroy<MyType>("MyType array from it");
130    }
131    return 0;
132 }
133 //]
134 #include <boost/interprocess/detail/config_end.hpp>