Re: C Standard Regarding Null Pointer Dereferencing



Shao Miller wrote:
On Jul 21, 8:44 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
Open and shut case.

Obviously it "feels" like it should be undefined behaviour, since
we've all been trying to avoid the act of a "null pointer dereference"
for a long time.

See 6.5.3.2.

"The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object. If the operand has type ‘‘pointer to type’’, the result has type ‘‘type’’. If an
invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined."

NULL is an invalid value - it is guaranteed not to point to any object or function. (See 6.3.2.3.)

Therefore, using * on a null pointer invokes UB.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
.



Relevant Pages

  • Re: pointer arithmetic
    ... ram@xxxxxxxxxxxxxxxxxx (Stefan Ram) writes: ... The unary * operator denotes indirection. ... operand has type "pointer to type", ...
    (comp.lang.c)
  • Re: null references
    ... To say that null and invalid references don't exist suggests that if you would just change to the function would be unable to fail and therefore correctly manages the semaphore, but this is a bogus argument. ... The reference version of the function can still throw an access violation and still needs to be coded for exception safety. ... If you define the above function as taking a reference arg then the undefined behaviour occurs the moment someone tries to dereference an invalid pointer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: access_ok macor
    ... The problem which I have is that if I run socketpair, getsockname, getpeername LTP ... tests with invalid salen pointer there are addresses close to 0x0. ... This sounds like a classic NULL pointer dereference that is handled correctly ... err = get_user; ...
    (Linux-Kernel)
  • Re: interpreting a null pointer as an empty (null string)
    ... there requires doing something that has undefined behavior (or, at least, ... which is the usual destination of the null pointer. ... Looks like this is explicitly disallowed by the Standard, ... null pointers being "invalid" as operands of the indirection operator; ...
    (comp.lang.c)
  • Re: if (pointer) ?
    ... operator or an integer constant cast to pointer type, ... lvalue designating an object of static storage duration. ... A function designator (implicitly yielding the address of the ... proposed replacement is worded in terms of syntax. ...
    (comp.std.c)