public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105438] New: Incorrect array-bounds warning with array size carried over from a previous template instantiation
@ 2022-04-30  8:24 bernie at codewiz dot org
  2022-04-30  8:27 ` [Bug c++/105438] " bernie at codewiz dot org
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: bernie at codewiz dot org @ 2022-04-30  8:24 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-07-27  9:22 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-30  8:24 [Bug c++/105438] New: Incorrect array-bounds warning with array size carried over from a previous template instantiation bernie at codewiz dot org
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

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).