Jump to content

  • Log In with Google      Sign In   
  • Create Account

Are you sure it's alive?

  • You cannot reply to this topic
10 replies to this topic

#1 TheComet   Crossbones+   -  Reputation: 2445

Like
6Likes
Like

Posted 17 October 2013 - 10:55 AM

Was reviewing some code in a project, and found a file where a developer had inserted the following in every single method:

class EventMapper {
   public:

   /*
    * constructor
    */
   EventMapper() {
      if( !this ) return;
      /* --SNIP--*/
   }

   /*
    * destructor
    */
   ~EventMapper() {
      if( !this ) return;
      /* --SNIP-- */
   }

   /*
    * initialises the event mapper
    */
   void initialise() {
      if( !this ) return;
      /* --SNIP-- */
   }

"Windows 10 doesn't only include spyware, it is designed as spyware" -- Gaius Publius, The Big Picture RT Interview

"[...] we will access, disclose and preserve personal data, including your content (such as the content of your emails, other private communications or files in private folders), when we have a good faith belief that doing so is necessary" -- Windows 10 Privacy Statement


#2 ApochPiQ   Moderators   -  Reputation: 19893

Like
8Likes
Like

Posted 17 October 2013 - 11:21 AM

This is an awesome way to mask a null-pointer dereference bug and make it even more of a pain to find!


Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#3 Nypyren   Crossbones+   -  Reputation: 9674

Like
0Likes
Like

Posted 17 October 2013 - 02:03 PM

Perhaps that coder was a "consultant":

 

http://www.despair.com/consulting.html



#4 swiftcoder   Senior Moderators   -  Reputation: 17438

Like
0Likes
Like

Posted 17 October 2013 - 02:56 PM

I'm also wondering how often you will get a null this pointer in practice.

 

It seems quite likely that you would instead receive an arbitrary (but still incorrect) pointer value...


Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]


#5 ApochPiQ   Moderators   -  Reputation: 19893

Like
4Likes
Like

Posted 17 October 2013 - 03:00 PM

I'm also wondering how often you will get a null this pointer in practice.
 
It seems quite likely that you would instead receive an arbitrary (but still incorrect) pointer value...


if ((unsigned)(this) < 0x1000) return;

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#6 TheComet   Crossbones+   -  Reputation: 2445

Like
-2Likes
Like

Posted 18 October 2013 - 04:04 AM

I'm also wondering how often you will get a null this pointer in practice.

 

It seems quite likely that you would instead receive an arbitrary (but still incorrect) pointer value...

 

You should never get a null or incorrect "this" pointer. The fact that you have access to "this" already means the object exists in memory and is valid.


"Windows 10 doesn't only include spyware, it is designed as spyware" -- Gaius Publius, The Big Picture RT Interview

"[...] we will access, disclose and preserve personal data, including your content (such as the content of your emails, other private communications or files in private folders), when we have a good faith belief that doing so is necessary" -- Windows 10 Privacy Statement


#7 wintertime   Crossbones+   -  Reputation: 3771

Like
0Likes
Like

Posted 18 October 2013 - 05:44 AM

#include <iostream>

class Test {
public:
  void printthis();
};

void Test::printthis() {
  std::cout << this << std::endl;
}

void lalala(Test* p) {
  p->printthis();
}

int main() {
  lalala(0);
  return 0;
}

If that guy also got such wrong code in his project... biggrin.png



#8 Hodgman   Moderators   -  Reputation: 47703

Like
3Likes
Like

Posted 18 October 2013 - 06:57 AM

I think I wrote that exact same hack back in the 90's when I was first learning C++.

Somewhere I had a NULL pointer and was calling functions on it, which means you end up inside that function with a NULL this value.

I diagnosed this symptom (this is null), and "fixed" it by inserting the if statement from the OP... nevermind the actual cause of the bug... unsure.png



#9 swiftcoder   Senior Moderators   -  Reputation: 17438

Like
0Likes
Like

Posted 18 October 2013 - 07:16 AM

You should never get a null or incorrect "this" pointer. The fact that you have access to "this" already means the object exists in memory and is valid.

 

Unfortunately, not true. Try the following program:

#include <iostream>
 
struct X
{
  void print() {
    std::cout << "pointer: " << (void*)this << std::endl;
  }
};
 
int main() {
  X *x = NULL;
  x->print();
 
  X *y;
  y->print();
}

On my machine, that produces the following:

$ clang++ null.c++ 
$ ./a.out 
pointer: 0
pointer: 0x7fff5d4e5b68

Which goes back to my argument that the common case is not in fact NULL, but an arbitrary pointer value, because in my mind it is much more likely to accidentally call a function on an undefined pointer than a pointer which I have explicitly initialised to NULL (unless of course your dev platform always nulls pointers by default).


Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]


#10 ApochPiQ   Moderators   -  Reputation: 19893

Like
3Likes
Like

Posted 18 October 2013 - 11:40 AM

You should never get a null or incorrect "this" pointer. The fact that you have access to "this" already means the object exists in memory and is valid.




Besides the null pointer example already given, consider the following:

struct Foo
{
    int Member;
    void DoStuff()
    {
        std::cout << this << std::endl;
    }
};

struct Bar
{
    int padding;
    int morepadding;
    Foo data;
};

int main()
{
    Bar* theobject = NULL;
    theobject->data.DoStuff();
}
My compiler prints 0x00000008 on a 32-bit build, for instance.
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#11 MaxDZ8   Crossbones+   -  Reputation: 4857

Like
0Likes
Like

Posted 21 October 2013 - 01:20 AM

I also did it a few times in the past, before I learnt to properly design object lifetimes.

But I still wonder about the delete this thing... that looked beyond words.


Previously "Krohm"






PARTNERS