1 #include "testutilities.h"
4 #include <sigc++/sigc++.h>
7 #include <functional> //For std::ref().
11 std::ostringstream result_stream;
13 void Foo(sigc::trackable&)
15 result_stream << "Foo(x)";
18 } // end anonymous namespace
20 int main(int argc, char* argv[])
22 auto util = TestUtilities::get_instance();
24 if (!util->check_command_args(argc, argv))
25 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
27 std::ostringstream pointer_stream;
28 auto t = new sigc::trackable();
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("");
34 sigc::slot<void()> foo = sigc::bind(sigc::ptr_fun(Foo), std::ref(*t));
36 util->check_result(result_stream, "Foo(x)");
38 // This invalidates foo.
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.
44 // Comment this out to get a meaningful backtrace from valgrind.
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("");
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;
61 util->check_result(result_stream, "");
65 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;