public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@mengyan1223.wang>
To: Jonny Grant <jg@jguk.org>, gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: gcc warn when pointers not checked non-null before de-referencing.
Date: Mon, 14 Jun 2021 13:15:39 +0800	[thread overview]
Message-ID: <97eb7315fd136ff8a818925b1704760a856ffe64.camel@mengyan1223.wang> (raw)
In-Reply-To: <0a9ccbb7-135a-b342-e5cb-35b7c6a44a00@jguk.org>

On Sun, 2021-06-13 at 23:30 +0100, Jonny Grant wrote:
> Hello
> 
> This isn't real code, just an example to show.
> 
> I've tried with:  -Wall -Wextra -O2  and some other warnings, but
> couldn't get this to generate a warning that *g was possibly de-
> referenced. May I ask, does GCC have a way to get warnings when pointers
> are not checked?
> I had a look but -Wnull-dereference didn't help.
> 
> #include <stdlib.h>
> 
> #include <cstddef>
> void f(int * g)
> {
>     *g = 1;
> 
>     if(NULL == g)
>     {
>         exit(1);
>     }
> }
> 
> Best regards Jonny

It was explained by Chris Lattner at
http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html

GCC (<= 4.4) had -Wunreachable-code which might work for this case.  But
it was too unreliable (as Chris said, generally there is no reliable way
to do this) and removed in later releases.
-- 
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University


  reply	other threads:[~2021-06-14  5:15 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-13 22:30 Jonny Grant
2021-06-14  5:15 ` Xi Ruoyao [this message]
2021-06-16 13:01   ` Jonny Grant
2021-06-16 13:36     ` Xi Ruoyao
2021-07-03 15:36       ` Jonny Grant
2021-07-06 15:39         ` Xi Ruoyao
2021-07-19 18:20           ` Jonny Grant
2021-06-16 17:59     ` Segher Boessenkool
2021-06-17 20:44       ` Jonny Grant
2021-06-18  4:16         ` Xi Ruoyao
2021-07-03 14:14           ` Jonny Grant
2021-07-03 16:22             ` Segher Boessenkool
2021-07-06 10:33               ` Jonny Grant
2021-06-18  8:38         ` Liu Hao
2021-06-18 14:53         ` Segher Boessenkool
2021-06-14 15:19 ` Martin Sebor

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=97eb7315fd136ff8a818925b1704760a856ffe64.camel@mengyan1223.wang \
    --to=xry111@mengyan1223.wang \
    --cc=gcc-help@gcc.gnu.org \
    --cc=jg@jguk.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).