News for hackers. Stuff that matters.
Re:Null Pointer Dereferencing Causes Undefined Behavior by notacoward on Monday April 20, 2015 @05:17PM (#9410933)
The problem comes from the assumption that NULL is the same as zero. Even the author makes that assumption, when he gives an example that explicitly uses zero and then talks about it as if it had used NULL. In fact, the two have never been the same. I've worked on machines that represented NULL with a different value/bitpattern, and yes, it smoked out a lot of these bad assumptions. NULL can never point to valid memory, by language definition. An explicit zero can, from a language standpoint, even if the runtime/OS might disagree. ;) Thus, uses like that in offsetof are quite valid.
Re:Null Pointer Dereferencing Causes Undefined Behavior by kstenerud on Monday April 20, 2015 @06:00PM (#9411120)
No, the problem comes from the assumption that you can legally fetch the "address" of a structure member via pointer dereference without checking for null so long as you don't actually try to access the resulting address.

    struct usb_line6 *line6 = &podhd->line6;
The assumption is that this is OK so long as you don't try to access the contents of line6 without doing some upstream null checking first. This assumption is false. The very act of dereferencing is undefined, regardless of whether you access the resulting address or not.

The assumption fails whether null is implemented as 0, 200, 0x8000000000000000, 0xffffffffffffffff.

Re:Null Pointer Dereferencing Causes Undefined Behavior by ploxiln on Monday April 20, 2015 @09:12PM (#9411777)
There was no actual dereferencing, this is just pointer arithmetic. it's "line6 = podhd + offset". There isn't really a better way to do it.
Re:Null Pointer Dereferencing Causes Undefined Behavior by kstenerud on Tuesday April 21, 2015 @10:56AM (#9414436)
Most underlying implementations won't actually dereference here (which is why it usually works), but as far as the spec is concerned, you are dereferencing it, which is undefined.

That becomes a problem with modern optimizing compilers, because they make assumptions about your code based on what's allowed according to the spec.

Re:Null Pointer Dereferencing Causes Undefined Behavior by ehmmm on Monday April 20, 2015 @09:19PM (#9411797)
There is no pointer arithmetic in the line:

    struct usb_line6 *line6 = &podhd->line6;
Offset is calculated "below the hood". Pointer arithmetic is defined with arithmetic operands: +,-

-> and . are not arithmetic operands.

Re:Null Pointer Dereferencing Causes Undefined Behavior by jwatte on Monday April 20, 2015 @10:58PM (#9412044)
You sound very sure of yourself.

If you look at the definition of the -> and [], you might learn something!

Re:Null Pointer Dereferencing Causes Undefined Behavior by ehmmm on Tuesday April 21, 2015 @12:41AM (#9412313)
Clearly, you don't have anything constructive to say, so you have to resort to ad hominem.

[] operator doesn't have any place in this debate.

Re:Null Pointer Dereferencing Causes Undefined Behavior by spoiler on Tuesday April 21, 2015 @06:16AM (#9413094)
Well, as far as I understand it, `->` works in a similar fashion to `[]`; the difference being that one behaves on arrays while the other on structs.

What @jwatte tried to say is that `->` works like this:

    // foo is struct thingity*
    &foo->bar == foo + offsetof(struct thingity, bar);
The `[]` does a similar thing:

    // s is char*
    *(s+i) == s[i]
Re:Null Pointer Dereferencing Causes Undefined Behavior by ehmmm on Tuesday April 21, 2015 @08:42PM (#9418042)
Sure, &foo->bar looks like foo + offsetof(struct thingity, bar), but there is no pointer arithmetic involved since C doesn't specify how the member access ( -> or . ) is actually calculated.

It does however specify that [] operator is equivalent to pointer arithmetic. But we are talking ->,. operators and not [].

Re:Null Pointer Dereferencing Causes Undefined Behavior by pcwalton on Monday April 20, 2015 @05:20PM (#9410951)
But NULL must, broadly speaking, be the same as zero.

C standard 3.2.2.3: "An integral constant expression with the value 0, or such an expression cast to type void * , is called a null pointer constant."

C standard 4.1.5: "The macros are... NULL, which expands to an implementation-defined null pointer constant."

See [1] for more info.

[1]: http://stackoverflow.com/questions/2599207/can-a-conforming-...

Re:Null Pointer Dereferencing Causes Undefined Behavior by comex on Monday April 20, 2015 @05:28PM (#9410981)
The constant 0 has to convert to NULL, but nonconstant zeroes don't, and NULL doesn't have to be 0 when converted back to an integer. This would break code like the example offsetof implementation.

See also http://stackoverflow.com/questions/2597142/when-was-the-null...

Re:Null Pointer Dereferencing Causes Undefined Behavior by EpicEng on Monday April 20, 2015 @06:31PM (#9411246)
NULL must compare equal to 0. That doesn't mean that it is represented by all 0 bits. NULL can have a value of anything add long as NULL == 0 is true.
Re:Null Pointer Dereferencing Causes Undefined Behavior by notacoward on Monday April 20, 2015 @05:26PM (#9410973)
Casting zero to a pointer yields a null pointer. That doesn't preclude other-valued null pointers, or ensure that a cast the other way will yield zero. That's where the "undefined" part comes from.
Re:Null Pointer Dereferencing Causes Undefined Behavior by _kst_ on Monday April 20, 2015 @06:11PM (#9411172)
Casting a constant zero to a pointer type yields a null pointer. The conversion is done at compile time, so the compiler knows that a null pointer is the required result.

There is no requirement for an integer-to-pointer conversion to retain the bit pattern of the integer value, any more than an integer-to-floating-point conversion is required to do so.

In most modern implementations, conversions among integer and pointer types of the same size are trivial, simply reinterpreting the bits as a value of a different type.

A conforming C or C++ compiler can use, for example, the bit pattern 0xFFFFFFFF to represent a null pointer. Converting a constant 0 to a pointer type would yield a pointer with the representation 0xFFFFFFFF. Converting a non-constant 0 to a pointer type does not necessarily yield a null pointer; the result is implementation-defined, and may be indeterminate garbage.

Re:Null Pointer Dereferencing Causes Undefined Behavior by mpyne on Monday April 20, 2015 @05:51PM (#9411082)
The way I like to think about it is that integer zero is the name in the C/C++ language (and runtime) for the actual null pointer, nothing more or less. In particular you can't assume anything about the integer representation of the actual null pointer.