public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Pinski <pinskia@gmail.com>
To: Alejandro Colomar <alx.manpages@gmail.com>
Cc: gcc@gcc.gnu.org
Subject: Re: [BUG] missing warning for pointer arithmetic out of bounds
Date: Tue, 13 Dec 2022 11:18:30 -0800	[thread overview]
Message-ID: <CA+=Sn1nhOdZWCy9u_DXuzZZoB5EmhTC5kCwnF4hQ9_6NQiD3XQ@mail.gmail.com> (raw)
In-Reply-To: <3597df95-929b-dfe2-d356-7938a99165ac@gmail.com>

On Tue, Dec 13, 2022 at 11:16 AM Alejandro Colomar via Gcc
<gcc@gcc.gnu.org> wrote:
>
>
>
> On 12/13/22 20:08, Alejandro Colomar wrote:
> > Hi!
> >
> > For the following program:
> >
> >
> >      $ cat buf.c
> >      #include <stdio.h>
> >
> >      int main(void)
> >      {
> >          char *p, buf[5];
> >
> >          p = buf + 6;
> >          printf("%p\n", p);
> >      }
> >
> >
> > There are no warnings in gcc, as I would expect:
>
> I just re-read my text, and it is ambiguous.  I meant that I expect warnings.

GCC only warns during VRP which is only enabled at -O2:

<source>:8:12: warning: array subscript 6 is outside array bounds of
'char[5]' [-Warray-bounds=]
    8 |          p = buf + 6;
      |          ~~^~~~~~~~~
<source>:6:19: note: at offset 6 into object 'buf' of size 5
    6 |          char *p, buf[5];
      |                   ^~~

Thanks,
Andrew


>
>
> >
> >      $ gcc -Wall -Wextra buf.c -O0
> >
> > Clang does warn, however:
> >
> >      $ clang -Weverything -Wall -Wextra buf.c -O0
> >      buf.c:8:17: warning: format specifies type 'void *' but the argument has
> > type 'char *' [-Wformat-pedantic]
> >          printf("%p\n", p);
> >                  ~~     ^
> >                  %s
> >      buf.c:7:6: warning: the pointer incremented by 6 refers past the end of the
> > array (that contains 5 elements) [-Warray-bounds-pointer-arithmetic]
> >          p = buf + 6;
> >              ^     ~
> >      buf.c:5:2: note: array 'buf' declared here
> >          char *p, buf[5];
> >          ^
> >      2 warnings generated.
> >
> > Cheers,
> >
> > Alex
> >
> >
>
> --
> <http://www.alejandro-colomar.es/>

  reply	other threads:[~2022-12-13 19:18 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-13 19:08 Alejandro Colomar
2022-12-13 19:15 ` Alejandro Colomar
2022-12-13 19:18   ` Andrew Pinski [this message]
2022-12-18 12:48     ` Optimization levels for getting all warnings (was: [BUG] missing warning for pointer arithmetic out of bounds) Alejandro Colomar
2022-12-13 19:22   ` [BUG] missing warning for pointer arithmetic out of bounds David Malcolm
2022-12-13 19:22 ` Paul Koning
2022-12-13 19:24   ` Alejandro Colomar
2022-12-13 20:45   ` Jonathan Wakely

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='CA+=Sn1nhOdZWCy9u_DXuzZZoB5EmhTC5kCwnF4hQ9_6NQiD3XQ@mail.gmail.com' \
    --to=pinskia@gmail.com \
    --cc=alx.manpages@gmail.com \
    --cc=gcc@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).