Imported Upstream version 2.99.2
[platform/upstream/libsigc++.git] / tests / test_copy_invalid_slot.cc
1 #include "testutilities.h"
2 #include <sstream>
3 #include <cstdlib>
4 #include <sigc++/sigc++.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <functional> //For std::ref().
8
9 namespace
10 {
11 std::ostringstream result_stream;
12
13 void Foo(sigc::trackable&)
14 {
15   result_stream << "Foo(x)";
16 }
17
18 } // end anonymous namespace
19
20 int main(int argc, char* argv[])
21 {
22   auto util = TestUtilities::get_instance();
23
24   if (!util->check_command_args(argc, argv))
25     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
26
27   std::ostringstream pointer_stream;
28   auto t = new sigc::trackable();
29   pointer_stream << t;
30   result_stream << "sigc::trackable instance at " << pointer_stream.str();
31   util->check_result(result_stream, "sigc::trackable instance at " + pointer_stream.str());
32   pointer_stream.str("");
33
34   sigc::slot<void()> foo = sigc::bind(sigc::ptr_fun(Foo), std::ref(*t));
35   foo();
36   util->check_result(result_stream, "Foo(x)");
37
38   // This invalidates foo.
39   delete t;
40
41   // Try to crash if the invalid slot parameter is used by libsigc++,
42   // and get a debugger backtrace at the point that it happens.
43   //
44   // Comment this out to get a meaningful backtrace from valgrind.
45   //
46   // Try to pollute the memory previously occupied by the sigc::trackable
47   // instance. The hope is that with a regular memory allocator (i.e. not
48   // valgrind), we end up with buffer == (void *)t.
49   void* buffer = malloc(sizeof(sigc::trackable));
50   memset(buffer, 0xFF, sizeof(sigc::trackable));
51   pointer_stream << buffer;
52   result_stream << "         Polluted buffer at " << pointer_stream.str();
53   util->check_result(result_stream, "         Polluted buffer at " + pointer_stream.str());
54   pointer_stream.str("");
55
56   // Now copy foo: up to libsigc++ version 2.0.11, the copy constructor fails
57   // because the pointer value it dereferences does not point to a
58   // sigc::trackable anymore, it now points to a polluted buffer.
59   sigc::slot<void()> bar = foo;
60   bar();
61   util->check_result(result_stream, "");
62
63   free(buffer);
64
65   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
66 }