Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / interprocess / example / doc_managed_copy_on_write.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/os_file_functions.hpp>
12 //[doc_managed_copy_on_write
13 #include <boost/interprocess/managed_mapped_file.hpp>
14 #include <fstream> //std::fstream
15 #include <iterator>//std::distance
16
17 //<-
18 #include "../test/get_process_id_name.hpp"
19 //->
20
21 int main()
22 {
23    using namespace boost::interprocess;
24
25    //Define file names
26    //<-
27    #if 1
28    std::string managed_file(boost::interprocess::detail::get_temporary_path());
29    managed_file += "/"; managed_file += test::get_process_id_name();
30    const char *ManagedFile = managed_file.c_str();
31    std::string managed_file2(boost::interprocess::detail::get_temporary_path());
32    managed_file2 += "/"; managed_file2 += test::get_process_id_name();  managed_file2 += "_2";
33    const char *ManagedFile2 = managed_file2.c_str();
34    #else
35    //->
36    const char *ManagedFile  = "MyManagedFile";
37    const char *ManagedFile2 = "MyManagedFile2";
38    //<-
39    #endif
40    //->
41
42    //Try to erase any previous managed segment with the same name
43    file_mapping::remove(ManagedFile);
44    file_mapping::remove(ManagedFile2);
45    remove_file_on_destroy destroyer1(ManagedFile);
46    remove_file_on_destroy destroyer2(ManagedFile2);
47
48    {
49       //Create an named integer in a managed mapped file
50       managed_mapped_file managed_file(create_only, ManagedFile, 65536);
51       managed_file.construct<int>("MyInt")(0u);
52
53       //Now create a copy on write version
54       managed_mapped_file managed_file_cow(open_copy_on_write, ManagedFile);
55
56       //Erase the int and create a new one
57       if(!managed_file_cow.destroy<int>("MyInt"))
58          throw int(0);
59       managed_file_cow.construct<int>("MyInt2");
60
61       //Check changes
62       if(managed_file_cow.find<int>("MyInt").first && !managed_file_cow.find<int>("MyInt2").first)
63          throw int(0);
64
65       //Check the original is intact
66       if(!managed_file.find<int>("MyInt").first && managed_file.find<int>("MyInt2").first)
67          throw int(0);
68
69       {  //Dump the modified copy on write segment to a file
70          std::fstream file(ManagedFile2, std::ios_base::out | std::ios_base::binary);
71          if(!file)
72             throw int(0);
73          file.write(static_cast<const char *>(managed_file_cow.get_address()), managed_file_cow.get_size());
74       }
75
76       //Now open the modified file and test changes
77       managed_mapped_file managed_file_cow2(open_only, ManagedFile2);
78       if(managed_file_cow2.find<int>("MyInt").first && !managed_file_cow2.find<int>("MyInt2").first)
79          throw int(0);
80    }
81    {
82       //Now create a read-only version
83       managed_mapped_file managed_file_ro(open_read_only, ManagedFile);
84       
85       //Check the original is intact
86       if(!managed_file_ro.find<int>("MyInt").first && managed_file_ro.find<int>("MyInt2").first)
87          throw int(0);
88
89       //Check the number of named objects using the iterators
90       if(std::distance(managed_file_ro.named_begin(),  managed_file_ro.named_end())  != 1 &&
91          std::distance(managed_file_ro.unique_begin(), managed_file_ro.unique_end()) != 0 )
92          throw int(0);
93    }
94    return 0;
95 }
96 //]
97 #include <boost/interprocess/detail/config_end.hpp>