public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Archie Cobbs <archie@dellroad.org>
To: gcc-help@gcc.gnu.org
Subject: Null pointer dereference and side effects
Date: Sun, 18 Jul 2004 19:30:00 -0000	[thread overview]
Message-ID: <200407181927.i6IJRV44038684@arch20m.dellroad.org> (raw)

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 = &ac;
	    *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.

Thanks,
-Archie

__________________________________________________________________________
Archie Cobbs      *        CTO, Awarix        *      http://www.awarix.com

             reply	other threads:[~2004-07-18 19:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-18 19:30 Archie Cobbs [this message]
2004-07-19 12:37 ` Eljay Love-Jensen
2004-07-19 14:30   ` Archie Cobbs
2004-07-19 15:28 ` Jeffrey A Law
2004-07-19 15:45   ` Archie Cobbs
2004-07-19 16:04     ` Eljay Love-Jensen
2004-07-20 15:30       ` Archie Cobbs
2004-07-20  9:55 ` Alexandre Oliva
2004-07-20 15:15   ` Archie Cobbs
2004-07-21  6:44     ` Alexandre Oliva
2004-07-21 11:33       ` Eljay Love-Jensen
2004-07-22 20:24         ` Alexandre Oliva

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200407181927.i6IJRV44038684@arch20m.dellroad.org \
    --to=archie@dellroad.org \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).