News for hackers. Stuff that matters.
Re:Dereferencing NULL Pointer, without a Seg Fault by eridius on Tuesday July 10, 2012 @09:52PM (#4227322)
Misleading headline and final sentence. The whole point of the article is that it's not dereferencing NULL. It's just passing NULL as a hidden parameter to a function, which in turn ignores that parameter.

I also found it rather odd that the author spent so much time defining what a pointer is and what dereferencing is, when the article won't be of any interest whatsoever to someone who doesn't already know these things.

And lastly, when I saw the headline about dereferencing NULL, I expected it to be a discussion regarding the circumstances in which you can actually map address 0 to valid memory (and therefore dereference NULL without crashing), and the consequences thereof.

Re:Dereferencing NULL Pointer, without a Seg Fault by sharkbot on Tuesday July 10, 2012 @10:25PM (#4227406)
Or the author could have added 'virtual' to the method declaration and discussed why that code now crashes due to a NULL dereference...
Re:Dereferencing NULL Pointer, without a Seg Fault by repsilat on Tuesday July 10, 2012 @10:33PM (#4227435)
It's disappointing that this works so reliably, even with the simplest of examples. I would have thought that if the compiler could resolve the function call statically it would do away with the vtable lookup, but neither g++ nor Clang do so at any level of optimisation.
Re:Dereferencing NULL Pointer, without a Seg Fault by klodolph on Wednesday July 11, 2012 @12:37AM (#4227743)
Well, you see, that would change the behavior of the program. In order for it to resolve the vtable statically, which it CAN do, it would have to know what kind of vtable the NULL cowPointer has. And the true answer is that the program is already wrong once it accesses the vtable.

To put it another way, such an optimizer would transform incorrect programs into correct programs.

If you write correct programs, the optimization is useless since it only transforms incorrect programs. If you write incorrect programs, the optimization is dangerous since it makes it harder for you to figure out that your program is wrong.

Re:Dereferencing NULL Pointer, without a Seg Fault by repsilat on Wednesday July 11, 2012 @04:31AM (#4228381)
> In order for it to resolve the vtable statically, which it CAN do, it would have to know what kind of vtable the NULL cowPointer has.

Yeah. In retrospect my point doesn't really make any sense at all. Not sure what I was thinking there.

Re:Dereferencing NULL Pointer, without a Seg Fault by DerekL on Tuesday July 10, 2012 @10:34PM (#4227444)
True, it's not dereferencing NULL at the machine level, but in C++ terms, it really is dereferencing the pointer. Evaluating the expression "p" (or "p->m", which is equivalent to "(p).m") is dereferencing. What it isn't doing is reading or writing at or near location 0.
Re:Dereferencing NULL Pointer, without a Seg Fault by repsilat on Tuesday July 10, 2012 @10:25PM (#4227407)
According to [1] this is (strictly speaking) undefined behaviour. It also won't work with virtual function calls, because they'll try to access the vtable and segfault.

If your compiler isn't cheeky you can use this stuff in funny ways with constructs like `if(this)` in your member functions. Technically such a check could be optimised out because `this==null` can only result from undefined behaviour, but in practice it probably won't be.

1: http://stackoverflow.com/questions/2474018/when-does-invokin...

Re:Dereferencing NULL Pointer, without a Seg Fault by prophetjohn on Wednesday July 11, 2012 @12:04AM (#4227671)
This reminds me of a conversation I had with a coworker recently about how possible it would be a for an object to check itself for null. Based on this explanation, it seems like this would definitely be possible in C++ (our conversation was in the context of Java, but it's interesting from an academic perspective)

    class NullChecker {
    public:
      bool isNull() { return !!this; }
    };

    int main() {
      NullChecker *nc = 0;
      if(nc->isNull()) doSomething();
      return 0;
    }
Forgive my C++ syntax as it's been a while, but this seems like it would work to me.
Re:Dereferencing NULL Pointer, without a Seg Fault by z92 on Wednesday July 11, 2012 @02:18AM (#4228016)
Tried it. It works.

Technically I look at methods as functions with a hidden first parameter that accepts a pointer to the class object's data. If you don't try to dereference that pointer, then the function should work like static member.

I was wondering if the compiler will add some check code preventing me from calling any method on a NULL pointed obj. But obviously its permitting it.

Of course virtual methods are different. Then you are calling a pointer to a function, the address of which should be stored in your obj data structure.

Re:Dereferencing NULL Pointer, without a Seg Fault by alttab on Tuesday July 10, 2012 @10:25PM (#4227408)
Actually, the last example won't even run because cout doesn't have the >> stream operator. Not sure how identical code samples in the same article could get those mixed up, unless the author typed each example out manually.

Not really contributing to the conversation, but it jumped out at me.

Re:Dereferencing NULL Pointer, without a Seg Fault by zwass on Tuesday July 10, 2012 @09:54PM (#4227327)
Seems to me that the NULL pointer is never dereferenced...
Re:Dereferencing NULL Pointer, without a Seg Fault by DannyBee on Wednesday July 11, 2012 @12:54AM (#4227787)
Fun fact: There are a number of platforms that made NULL dereference valid (usually returning zero), because it enables you to do fun compiler optimizations that are not safe otherwise. Some of these optimizations relate to being able to insert new loads or hoist existing ones, some of these relate to being able to remove null pointer checks. (for example, in:

  int **a = NULL;
  int *b = NULL;
  int c = 0;
  ...
  if (a ! = NULL)
     b = *a;
  if (b != NULL)
     c = *b;
you can remove all the NULL checks if *NULL = 0)

AIX was an example of such a platform (at least until version 5.3, i haven't used it since then).

Re:Dereferencing NULL Pointer, without a Seg Fault by darkstalker on Tuesday July 10, 2012 @10:28PM (#4227417)
thought the article was gonna conclude doing something like:

  void Cow::milk()
  {
      if (!this) throw std::runtime_error("NULL isn't a cow!");
      // rest of the code
  }
Re:Dereferencing NULL Pointer, without a Seg Fault by rayiner on Wednesday July 11, 2012 @12:31AM (#4227731)
TL;DR version: the '->' operator is punned in C++ to mean both "load through a pointer" and "call a function with the pointer as the first argument." Unsurprisingly, nothing blows up when you use the second form with a null pointer.

V8 actually uses this to implement a pretty cool trick. V8 objects are tagged--tag bits distinguish valid pointers from integer immediate values. The integer class has no data, it stores its information in the 'this' pointer.

Re:Dereferencing NULL Pointer, without a Seg Fault by mkopinsky on Thursday July 12, 2012 @11:13PM (#4238042)
This V8 business sounds like some form of black magic. Do you have more info on this?
Re:Dereferencing NULL Pointer, without a Seg Fault by Tuna-Fish on Tuesday July 10, 2012 @11:47PM (#4227632)
I was expecting mmap(1, ...).

Of course, I don't think that works on modern systems anymore, with vm.mmap_min_addr and friends.

Re:Dereferencing NULL Pointer, without a Seg Fault by chollida1 on Tuesday July 10, 2012 @11:23PM (#4227580)
I'd pointed this out in stackoverflow a few years ago as an example of what not to do:)

http://stackoverflow.com/questions/1640309/c-static-members-...

Re:Dereferencing NULL Pointer, without a Seg Fault by xfs on Tuesday July 10, 2012 @10:50PM (#4227485)
For an actual NULL pointer dereference resulting in arbitrary code execution: CVE-2009-0385.
Re:Dereferencing NULL Pointer, without a Seg Fault by smsm42 on Wednesday July 11, 2012 @12:37AM (#4227744)
Well, technically it's not a NULL pointer dereference, it's (NULL+user controlled data) pointer dereference, which is quite a different matter. See e.g. http://www.securityfocus.com/archive/1/archive/1/500514/100/... for the explanation of the code.
Re:Dereferencing NULL Pointer, without a Seg Fault by lmm on Wednesday July 11, 2012 @05:31AM (#4228501)
I've come to prefer the python style of object methods, where "self" is an explicit first parameter; it makes it a lot clearer what's going on.
Re:Dereferencing NULL Pointer, without a Seg Fault by daemonize on Tuesday July 10, 2012 @10:12PM (#4227380)
I think showing the actual disassembly would help people understand. it might lose some so maybe as a side bar.
Re:Dereferencing NULL Pointer, without a Seg Fault by danellis on Tuesday July 10, 2012 @10:20PM (#4227394)
Showing some C-like pseudo-code would probably do the trick nicely.
Re:Dereferencing NULL Pointer, without a Seg Fault by malkia on Tuesday July 10, 2012 @10:55PM (#4227499)
p->m() ~= m(p) - e.g. the object is pushed as the first argument to the method.

The actual "m" is found by looking at the "p" class if it's non-virtual, and at the "v-table" in p if it's "virtual".

If "m" is static, no class lookup, or object v-table lookup is done, and the call is p->m() ~= m()

Re:Dereferencing NULL Pointer, without a Seg Fault by sporx on Tuesday July 10, 2012 @09:57PM (#4227341)
/facepalm