This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Null pointer dereference and side effects
- From: Jeffrey A Law <law at redhat dot com>
- To: Archie Cobbs <archie at dellroad dot org>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Mon, 19 Jul 2004 09:29:34 -0600
- Subject: Re: Null pointer dereference and side effects
- Organization: Red Hat, Inc
- References: <200407181927.i6IJRV44038684@arch20m.dellroad.org>
- Reply-to: law at redhat dot com
On Sun, 2004-07-18 at 13:27, Archie Cobbs wrote:
> I have a question about GCC optimizations and null pointers.
> Consider this program:
>
> #include <stdlib.h>
>
> int
> main(int ac, char **av)
> {
> int *r1;
> int *r2;
>
> r1 = ∾
> *r1 = 0;
> r2 = (void *)0; /* or (void *)1, etc. */
> if (*r1 != *r2)
> return 1;
> return 1;
> }
>
> When run & compiled with gcc -O2 (gcc 3.2.2) it does not segfault.
> Clearly this is because the optimizer realizes that *r1 need not be
> read in order to know that the function should return 1.
>
> However, in my application I'm relying on the side effects of
> dereferencing a null pointer (ie., SEGV signal), so that causes
> bugs for me.
>
> I understand that according to the C standard dereferencing a null
> pointer results in "undefined" behavior, so gcc is not behaving
> incorrectly.
>
> However, is there some way to get the desired semantics? It appears
> omitting the -O2 does it, but I'd prefer to not do that.
-fno-delete-null-pointer-checks.
jeff