Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / interprocess / example / doc_ipc_message.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_ipc_message
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <cstdlib> //std::system
15 #include <sstream>
16 //<-
17 #include "../test/get_process_id_name.hpp"
18 //->
19
20 int main (int argc, char *argv[])
21 {
22    using namespace boost::interprocess;
23    if(argc == 1){  //Parent process
24       //Remove shared memory on construction and destruction
25       struct shm_remove 
26       {
27       //<-
28       #if 1
29          shm_remove() {  shared_memory_object::remove(test::get_process_id_name()); }
30          ~shm_remove(){  shared_memory_object::remove(test::get_process_id_name()); }
31       #else
32       //->
33          shm_remove() {  shared_memory_object::remove("MySharedMemory"); }
34          ~shm_remove(){  shared_memory_object::remove("MySharedMemory"); }
35       //<-
36       #endif
37       //->
38       } remover;
39
40       //Create a managed shared memory segment
41       //<-
42       #if 1
43       managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
44       #else
45       //->
46       managed_shared_memory segment(create_only, "MySharedMemory", 65536);
47       //<-
48       #endif
49       //->
50
51       //Allocate a portion of the segment (raw memory)
52       std::size_t free_memory = segment.get_free_memory();
53       void * shptr = segment.allocate(1024/*bytes to allocate*/);
54
55       //Check invariant
56       if(free_memory <= segment.get_free_memory())
57          return 1;
58
59       //An handle from the base address can identify any byte of the shared 
60       //memory segment even if it is mapped in different base addresses
61       managed_shared_memory::handle_t handle = segment.get_handle_from_address(shptr);
62       std::stringstream s;
63       s << argv[0] << " " << handle;
64       //<-
65       s << " " << test::get_process_id_name();
66       //->
67       s << std::ends;
68       //Launch child process
69       if(0 != std::system(s.str().c_str()))
70          return 1;
71       //Check memory has been freed
72       if(free_memory != segment.get_free_memory())
73          return 1;
74    }
75    else{
76       //Open managed segment
77       //<-
78       #if 1
79       managed_shared_memory segment(open_only, argv[2]);
80       #else
81       //->
82       managed_shared_memory segment(open_only, "MySharedMemory");
83       //<-
84       #endif
85       //->
86
87       //An handle from the base address can identify any byte of the shared 
88       //memory segment even if it is mapped in different base addresses
89       managed_shared_memory::handle_t handle = 0;
90
91       //Obtain handle value
92       std::stringstream s; s << argv[1]; s >> handle;
93
94       //Get buffer local address from handle
95       void *msg = segment.get_address_from_handle(handle);
96
97       //Deallocate previously allocated memory
98       segment.deallocate(msg);
99    }
100    return 0;
101 }
102 //]
103 #include <boost/interprocess/detail/config_end.hpp>