public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "konrad at silmor dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/105523] Wrong warning array subscript [0] is outside array bounds
Date: Tue, 25 Apr 2023 19:07:48 +0000	[thread overview]
Message-ID: <bug-105523-4-jMWrioqc9l@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105523-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105523

--- Comment #30 from Konrad Rosenbaum <konrad at silmor dot de> ---
(In reply to Andrew Pinski from comment #28)
> (In reply to Wilhelm M from comment #26)
> > As you can see in my opening bug report, there is no nullptr reference nor
> > dereferencing a pointer with value 0.
> 
> Yes but as I mentioned by the time the warning happens, the IR has lost
> information if the constant came from a null pointer or from some other
> constant that the user used. So a heuristic needs to be used and the idea
> was it would be the lower bytes by default and that the lower bytes would be
> defined by a "page size" and that is how the naming of the option happened.

To be honest, this sounds like a missing feature or even a design bug:
shouldn't the IR be aware of what original address (type) a pointer/array
originated from?

If it makes a difference whether I access a[70] or a[700000000] then the
heuristic is not very helpful. If a useful option has a completely unintuitive
name that is also a problem.

Null pointers usually derive from nullptr, NULL or the integer constant 0. I
think it would be worth a flag or two to mark code pathes that transmit real
null pointers or offsets to real null pointers. Anything derived from a
non-zero integer is something that the programmer bears full responsibility for
- if he wants to aim at his foot, let him!

Even if something derives from integer 0 (as opposed to nullptr or NULL), there
is a chance that it was something that is intended (on the MCU I'm currently
using this is the register that sets the first 8 GPIO pins) - usually
"volatile" will be a good indicator of that, because memory mapped registers
are always volatile.

A few examples I can think of:


struct MyStruct { int a,b,c; };

auto *nps = (MyStruct*)nullptr; // -> originates from a nullptr, definitely a
problem!
auto *npi = &nps->c; // -> still derived from nullptr, still a problem!

MyStruct myvar;
auto *vps = &myvar ; // -> dereferences a real variable, definitely not a
problem
auto *vpi = &vps->c; // -> still not a problem

auto *sps = (MyStruct*)0x101; // -> static address, very likely not a problem
or at least the programmer's problem, not the compiler's
auto *spi = &sps->c; // same here

auto *xps = (MyStruct*)0x00; // -> static address, but 0x00 -> probably nullptr
auto *xpi = &xps->c; // -> derived from probably nullptr -> probably a problem

struct VMyStruct { volatile int a,b,c; };
auto *yps = (VMyStruct*)0x00; // -> static null address, but has volatile
members, may or may not be problematic
auto *ypi = &yps->c; // -> derived from static null address, but access is
volatile, it's the programmer's problem, no warning

auto *zps = (VMyStruct*)NULL; // NULL=((void*)0) -> derived from non-volatile
pointer to 0, count it like nullptr!

No heuristic at the point of use will be able to properly catch and categorize
those. There will have to be some flags that get passed along with the
(changing) pointer value.

Even then there should be a way to tell the compiler whether (SomeType*)0 is a
null pointer or a pointer to a legitimate memory location. Can pointer values
have attributes?

The question that remains is: is this change worth it and might it help to
catch other problems?

  parent reply	other threads:[~2023-04-25 19:07 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-08 12:12 [Bug c++/105523] New: " klaus.doldinger64 at googlemail dot com
2022-05-08 19:53 ` [Bug target/105523] " pinskia at gcc dot gnu.org
2022-05-09  4:00 ` klaus.doldinger64 at googlemail dot com
2022-05-09  4:54 ` klaus.doldinger64 at googlemail dot com
2022-05-09  6:59 ` klaus.doldinger64 at googlemail dot com
2022-05-11 11:50 ` rudi at heitbaum dot com
2022-10-21  5:57 ` lh_mouse at 126 dot com
2022-11-23 17:49 ` gjl at gcc dot gnu.org
2022-11-23 17:53 ` pinskia at gcc dot gnu.org
2022-11-23 17:56 ` pinskia at gcc dot gnu.org
2022-11-23 18:04 ` lh_mouse at 126 dot com
2022-11-23 18:07 ` pinskia at gcc dot gnu.org
2022-11-23 18:11 ` konrad at silmor dot de
2022-11-24  3:35 ` lh_mouse at 126 dot com
2022-12-07  6:32 ` ahmad at a3f dot at
2023-01-22 23:44 ` westfw at westfw dot info
2023-03-31 10:02 ` dcrocker at eschertech dot com
2023-03-31 20:32 ` pinskia at gcc dot gnu.org
2023-04-24 15:21 ` david at westcontrol dot com
2023-04-24 18:41 ` gjl at gcc dot gnu.org
2023-04-25  7:18 ` david at westcontrol dot com
2023-04-25  7:30 ` david at westcontrol dot com
2023-04-25  7:30 ` lh_mouse at 126 dot com
2023-04-25  7:46 ` david at westcontrol dot com
2023-04-25 16:30 ` pinskia at gcc dot gnu.org
2023-04-25 16:45 ` david at westcontrol dot com
2023-04-25 16:57 ` klaus.doldinger64 at googlemail dot com
2023-04-25 16:58 ` klaus.doldinger64 at googlemail dot com
2023-04-25 17:04 ` pinskia at gcc dot gnu.org
2023-04-25 19:02 ` gjl at gcc dot gnu.org
2023-04-25 19:07 ` konrad at silmor dot de [this message]
2023-04-28  5:17 ` lh_mouse at 126 dot com
2023-04-28  5:25 ` pinskia at gcc dot gnu.org
2023-06-19  8:23 ` cvs-commit at gcc dot gnu.org
2023-08-01 13:45 ` gjl at gcc dot gnu.org
2023-08-09 18:53 ` gjl at gcc dot gnu.org
2024-02-12 17:08 ` cvs-commit at gcc dot gnu.org
2024-02-12 17:10 ` gjl at gcc dot gnu.org

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=bug-105523-4-jMWrioqc9l@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).