public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Paul Koning <paulkoning@comcast.net>
Cc: Alejandro Colomar <alx.manpages@gmail.com>,
	"gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Re: [BUG] missing warning for pointer arithmetic out of bounds
Date: Tue, 13 Dec 2022 20:45:44 +0000	[thread overview]
Message-ID: <CAH6eHdSNmtS84H+UDne-QtCrCnYZrTbFKB-qjjShBSuy_vLkAQ@mail.gmail.com> (raw)
In-Reply-To: <B9E38906-88DC-45CE-94AB-72DEF6A590B2@comcast.net>

On Tue, 13 Dec 2022, 19:23 Paul Koning via Gcc, <gcc@gcc.gnu.org> wrote:
>
>
>
> > On Dec 13, 2022, at 2:08 PM, Alejandro Colomar via Gcc <gcc@gcc.gnu.org> 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:
> >
> >    $ 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;
> >            ^     ~
>
> I thought void * is a generic pointer that accepts any pointer argument.

Yes, but printf doesn't take a void* argument.

>
>   So a warning about char* being passed in seems to be flat out wrong.

I wouldn't say flat out wrong. It's wrong because the C standard
guarantees that void* and char* have the same representation and
alignment requirements, so no conversion is needed to pass a char*
through a varargs ellipsis where a void* is expected. But if it was
int* or struct X* then the warning would be correct (but not a problem
in practice on common arches, which is why Clang only enabels it with
-Wformat-pedantic).


>
> >    buf.c:5:2: note: array 'buf' declared here
> >        char *p, buf[5];
> >        ^
> >    2 warnings generated.
>
> That was discussed just days ago: C says that a pointer one past the end of the array is legal.  So here too it looks like Clang is wrong and GCC is right.


No, clang is totally right here.

      parent reply	other threads:[~2022-12-13 20:45 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
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 [this message]

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=CAH6eHdSNmtS84H+UDne-QtCrCnYZrTbFKB-qjjShBSuy_vLkAQ@mail.gmail.com \
    --to=jwakely.gcc@gmail.com \
    --cc=alx.manpages@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=paulkoning@comcast.net \
    /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).