public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Martin Sebor <msebor@gmail.com>
Cc: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] handle VLA of zero length arrays and vice versa (PR 99121)
Date: Thu, 18 Feb 2021 10:30:03 +0100	[thread overview]
Message-ID: <20210218093003.GB4020736@tucnak> (raw)
In-Reply-To: <58c2f386-65c0-f9c6-a57a-a7a5941350ff@gmail.com>

On Wed, Feb 17, 2021 at 02:11:43PM -0700, Martin Sebor wrote:
> On 2/17/21 1:47 PM, Jakub Jelinek wrote:
> > On Wed, Feb 17, 2021 at 01:27:55PM -0700, Martin Sebor wrote:
> > 
> > Not in this patch, but I've looked at what maxobjsize is and wonder why
> > the roundtrip tree -> HOST_WIDE_INT -> offset_int:
> >    const offset_int maxobjsize = tree_to_shwi (max_object_size ());
> > Can't it be
> >    const offset_int maxobjsize = wi::to_offset (max_object_size ());
> > ?
> 
> Yes, that's what it is elsewhere so this should do the same.  I've
> changed it.

Ok.

> > Doesn't arrbounds[1] == 0 mean there will be warnings for any accesses?
> > For eltsize == 0 I think you shouldn't warn when nelts isn't known,
> > instead of always warning, arr[100000000] will have the same address as
> > arr[0] ...
> 
> This branch is entered for VLAs of zero-length arrays where we want
> to warn, like this:
> 
> void f (void*);
> 
> void g (int n)
> {
>   int a[n][0];
>   ((int*)a)[0] = 0;
>   f (a);
> }

For this you do want to warn, but not the way you warn with the patch:
xxx.c: In function ‘g’:
xxx.c:6:12: warning: array subscript 0 is outside array bounds of ‘int[<Uec60>][0]’ [-Warray-bounds]
    6 |   ((int*)a)[0] = 0;
      |   ~~~~~~~~~^~~
xxx.c:5:7: note: while referencing ‘a’
    5 |   int a[n][0];
      |       ^

The message doesn't make it clear which of the two subscripts is out of
bounds, yes, for [0] it would be outside of bounds, but for the VLA index
no index < n would be outside of bounds.

Consider a different (GNU C, in C++ struct S has non-zero size) testcase:
void f (void*);

void g (int n)
{
  struct S {} a[n];
  ((int*)a)[0] = 0;
  f (a);
}
yyy.c:6:12: warning: array subscript 0 is outside array bounds of ‘struct S[<Ucc60>]’ [-Warray-bounds]
    6 |   ((int*)a)[0] = 0;
      |   ~~~~~~~~~^~~
yyy.c:5:15: note: while referencing ‘a’
    5 |   struct S {} a[n];
      |               ^
I bet that means you are really complaining about the VLA bound rather than
the [0] bound even in the first case, because the wording is otherwise the
same.  And for g (154) the array subscript 0 is certainly not a problem,
so the warning would need to be worded differently in that case.

	Jakub


  reply	other threads:[~2021-02-18  9:30 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-17  3:34 Martin Sebor
2021-02-17 13:56 ` Jakub Jelinek
2021-02-17 20:27   ` Martin Sebor
2021-02-17 20:47     ` Jakub Jelinek
2021-02-17 21:11       ` Martin Sebor
2021-02-18  9:30         ` Jakub Jelinek [this message]
2021-02-18 16:24           ` Martin Sebor
2021-02-18 18:00             ` Jakub Jelinek
2021-02-18 18:03               ` Jakub Jelinek
2021-02-18 20:55                 ` Martin Sebor
2021-03-09  2:37                   ` Martin Sebor
2021-03-12 13:27                     ` Jakub Jelinek
2021-03-13 21:46                       ` 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=20210218093003.GB4020736@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=msebor@gmail.com \
    /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).