add original source
This commit is contained in:
44
source/listing_3.2.cpp
Normal file
44
source/listing_3.2.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <mutex>
|
||||
|
||||
class some_data
|
||||
{
|
||||
int a;
|
||||
std::string b;
|
||||
public:
|
||||
void do_something()
|
||||
{}
|
||||
};
|
||||
|
||||
class data_wrapper
|
||||
{
|
||||
private:
|
||||
some_data data;
|
||||
std::mutex m;
|
||||
public:
|
||||
template<typename Function>
|
||||
void process_data(Function func)
|
||||
{
|
||||
std::lock_guard<std::mutex> l(m);
|
||||
func(data);
|
||||
}
|
||||
};
|
||||
|
||||
some_data* unprotected;
|
||||
|
||||
void malicious_function(some_data& protected_data)
|
||||
{
|
||||
unprotected=&protected_data;
|
||||
}
|
||||
|
||||
data_wrapper x;
|
||||
|
||||
void foo()
|
||||
{
|
||||
x.process_data(malicious_function);
|
||||
unprotected->do_something();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
foo();
|
||||
}
|
||||
Reference in New Issue
Block a user