public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "bernie at codewiz dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/105438] New: Incorrect array-bounds warning with array size carried over from a previous template instantiation
Date: Sat, 30 Apr 2022 08:24:55 +0000	[thread overview]
Message-ID: <bug-105438-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 105438
           Summary: Incorrect array-bounds warning with array size carried
                    over from a previous template instantiation
           Product: gcc
           Version: 11.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bernie at codewiz dot org
  Target Milestone: ---

Minified testcase (almost every line is necessary to reproduce):

```
/* g++ -Warray-bounds -O2 repro.cc */

int longer[7] = {};
int shorter[2] = {};
int out[10] = {};

template <int N>
void configure(const int(&in)[N], const int nrows = N)
{
    if (nrows <= 10)
    {
        for (int i = 0; i < nrows; i++)
        {
            out[i] = in[i];
        }
    }
}

int main()
{
  configure(longer);
  configure(shorter);
}
```

Output:

```
$ g++ -Warray-bounds -O2 repro.cc
repro.cc: In function 'int main()':
repro.cc:13:24: warning: array subscript 'const int [7][0]' is partly outside
array bounds of 'int [2]' [-Warray-bounds]
   13 |             out[i] = in[i];
      |                      ~~^
repro.cc:3:5: note: while referencing 'shorter'
    3 | int shorter[2] = {};
      |     ^~~~~~~
repro.cc:13:24: warning: array subscript 'const int [7][0]' is partly outside
array bounds of 'int [2]' [-Warray-bounds]
   13 |             out[i] = in[i];
      |                      ~~^
repro.cc:3:5: note: while referencing 'shorter'
    3 | int shorter[2] = {};
      |     ^~~~~~~
```

Static analysis appears to be using the length of the longer array for the call
using the shorter array.

The warning disappears by:
 * commenting out the first call to configure() suppresses the warning
 * swapping the two calls to configure()
 * commenting out if statement also eliminates the warning
 * making longer and shorter the same size
 * using N as loop counter instead of nrows

             reply	other threads:[~2022-04-30  8:24 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-30  8:24 bernie at codewiz dot org [this message]
2022-04-30  8:27 ` [Bug c++/105438] " bernie at codewiz dot org
2022-05-02  6:05 ` bernie at codewiz dot org
2022-05-02  6:07 ` bernie at codewiz dot org
2022-05-02  6:11 ` bernie at codewiz dot org
2022-05-02  6:54 ` [Bug ipa/105438] " rguenth at gcc dot gnu.org
2022-05-02  6:57 ` [Bug ipa/105438] [11/12/13 Regression] " pinskia at gcc dot gnu.org
2022-05-03 12:27 ` [Bug ipa/105438] [11/12/13 Regression] Incorrect array-bounds warning with array size carried over from a previous template instantiation since r11-4987-g602c6cfc79ce4ae6 since r11-4987-g602c6cfc79ce4ae6 marxin at gcc dot gnu.org
2022-05-03 12:42 ` marxin at gcc dot gnu.org
2022-07-26 11:39 ` [Bug ipa/105438] [11/12/13 Regression] Incorrect array-bounds warning with array size carried over from a previous template instantiation " rguenth at gcc dot gnu.org
2023-01-22  0:55 ` bernie at codewiz dot org
2023-02-09 11:37 ` marxin at gcc dot gnu.org
2023-05-29 10:06 ` [Bug ipa/105438] [11/12/13/14 " jakub at gcc dot gnu.org
2023-07-20  7:23 ` bernie at codewiz dot org
2023-07-27  9:22 ` rguenth 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-105438-4@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).