Re: C Standard Regarding Null Pointer Dereferencing



Shao Miller <sha0.miller@xxxxxxxxx> writes:

On Jul 23, 12:43 pm, Tim Rentsch <t...@xxxxxxxxxxxxxxxxxx> wrote:

The expression '*(char *)0' is undefined behavior if it
is evaluated. Any subsequent cast is irrelevant to the
question about whether the behavior is defined.

If and only if you do not take the text for the unary '*' operator
literally. That text describes undefined behaviour when a null
pointer value has been assigned to the pointer. Here we have a null
pointer, not a nuller pointer value assigned to a pointer.

We agreed that it's possible that that text might be imprecise, and
might need to be addressed, did we not? But it's also possible that
it's precise, and there is no undefined behaviour until casting to
'(void)'.

Would you agree?

I don't. The wording could be better, but there is no
doubt about the meaning. The Standard is written in
formal English but it is not a math textbook, and it's
at best a waste of time to read it like one.

If you want to get technical, it can NEVER be the case
that the operand of an indirection operator has been
assigned. In the expression '*p', where p has been
declared to be of some pointer type, the operand 'p'
has already been converted to a value by virtue of
6.3.2.1p2. There is no difference between '*p' and
'*(char*)0' in this regard -- both operate on values,
not objects. So it's completely nonsensical to try to
understand "has been assigned" as applying to one class
of operand expression but not another. They are all
just values.
.



Relevant Pages

  • Re: end of array
    ... You can make a pointer point to anything. ... arithmetic invokes undefined behaviour and anything can happen. ... I will probably get a segmentation fault or a memory fault. ... Just calculating a pointer like that can cause UB? ...
    (comp.lang.c)
  • Re: C99 clarification on pointers to aggregate object and its sub objects
    ... you can increment the pointer before the behaviour becomes undefined. ... memset p, 0, sizeof ); ... undefined behaviour, then ii and iii are both undefined, aren't they? ...
    (comp.std.c)
  • Re: system() without waiting
    ... Okay, "results in" undefined behaviour, then. ... In SUSv3 the execlpfunction has a prototype that ends with ", ... null pointer is a pointer that results from conversion of a null pointer ... null-terminated character strings. ...
    (comp.unix.programmer)
  • Re: Buffer overflows and asctime()
    ... The only change is that currently the undefined behaviour *allows* asctime() to return a null pointer, whereas this version, if pasted into the standard, would *require* it. ... But of course it can return a null pointer, if you have invoked undefined behaviour by giving it values that make the C99 sample implementation overflow its buffer. ...
    (comp.std.c)
  • Re: pointer to itself
    ... convert the pointer back to int** and then follow it. ... The only exceptions to this are converting to ... You might invoke undefined behaviour apart from the specific case I mentioned, and if you invoke undefined behaviour there is no guarantee what you will get back, it could be an enraged rhinoceros instead of any kind of pointer. ... E.g. if int 8 byte alignment and int* 4 byte alignment, ...
    (comp.lang.c)