~nux-team/nuxplayground/nux-visualstudio

« back to all changes in this revision

Viewing changes to libsigc++/tests/test_copy_invalid_slot.cc

  • Committer: Jay Taoko
  • Date: 2012-07-12 21:09:46 UTC
  • Revision ID: jay.taoko@canonical.com-20120712210946-5xpkonw92ivp1xl0
VisualStudio build files for Nux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <iostream>
 
2
#include <cstdlib>
 
3
#include <cstring>
 
4
#include <sigc++/sigc++.h>
 
5
 
 
6
#include <stdlib.h>
 
7
#include <string.h>
 
8
 
 
9
static void Foo(sigc::trackable&) {}
 
10
 
 
11
int main(int, char**)
 
12
{
 
13
   sigc::trackable *t = new sigc::trackable();
 
14
   std::cout << "sigc::trackable instance at " << t << std::endl;
 
15
   sigc::slot<void> foo = sigc::bind(sigc::ptr_fun(Foo), sigc::ref(*t));
 
16
   // This invalidates foo.
 
17
   delete t;
 
18
 
 
19
   // Try to crash if the invalid slot parameter is used by libsigc++,
 
20
   // and get a debugger backtrace at the point that it happens.
 
21
   //
 
22
   // Comment this out to get a meaningful backtrace from valgrind.
 
23
   //
 
24
   // Try to pollute the memory previously occupied by the sigc::trackable
 
25
   // instance. The hope is that with a regular memory allocator (i.e. not
 
26
   // valgrind), we end up with buffer == (void *)t.
 
27
   void *buffer = malloc(sizeof(sigc::trackable));
 
28
   memset(buffer, 0xFF, sizeof(sigc::trackable));
 
29
   std::cout << "         Polluted buffer at " << buffer << std::endl;
 
30
 
 
31
   // Now copy foo: up to libsigc++ version 2.0.11, the copy constructor fails
 
32
   // because the pointer value it dereferences does not point to a
 
33
   // sigc::trackable anymore, it now points to a polluted buffer.
 
34
   sigc::slot<void> bar = foo;
 
35
 
 
36
   free(buffer);
 
37
   return 0;
 
38
}