Bug 98266 - [11 Regression] bogus array subscript is partly outside array bounds on virtual inheritance
Summary: [11 Regression] bogus array subscript is partly outside array bounds on virtu...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 11.0
: P1 normal
Target Milestone: 11.0
Assignee: Martin Sebor
URL:
Keywords: diagnostic, patch
Depends on:
Blocks: Warray-bounds
  Show dependency treegraph
 
Reported: 2020-12-13 23:32 UTC by Martin Sebor
Modified: 2021-03-08 20:39 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-12-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2020-12-13 23:32:06 UTC
As reported in https://bugzilla.redhat.com/show_bug.cgi?id=1907216, GCC 11 issues a spurious -Warray-bounds on the following code (reduced from the original test case):

$ cat rhbz1738791.C && g++ -O2 -S -Wall rhbz1738791.C
struct A
{
  virtual ~A() noexcept;
  const char* s;
};

struct B: virtual A { };

struct C: virtual B { };

struct D: virtual A { };

struct E: virtual B, virtual D
{
  E (const char*);
};

void f (E);

void g_function ()
{
  f (E (""));
}
rhbz1738791.C: In function ‘void g_function()’:
rhbz1738791.C:11:8: warning: array subscript ‘D[1]’ is partly outside array bounds of ‘E [1]’ [-Warray-bounds]
   11 | struct D: virtual A { };
      |        ^
rhbz1738791.C:22:11: note: while referencing ‘<anonymous>’
   22 |   f (E (""));
      |           ^
rhbz1738791.C:11:8: warning: array subscript ‘D[1]’ is partly outside array bounds of ‘E [1]’ [-Warray-bounds]
   11 | struct D: virtual A { };
      |        ^
rhbz1738791.C:22:11: note: while referencing ‘<anonymous>’
   22 |   f (E (""));
      |           ^
Comment 1 Martin Sebor 2020-12-13 23:33:44 UTC
The problem is most likely the same as in pr97595 (except in gimple-array-bounds.cc).
Comment 3 Martin Sebor 2021-02-23 23:12:28 UTC
See pr22488 for the underlying problem with the difference between DECL_SIZE and TYPE_SIZE of classes with virtual bases.
Comment 4 GCC Commits 2021-03-08 20:39:00 UTC
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:f3daa6c0fd8d79ae45eac2dd0f274da1aa71c958

commit r11-7563-gf3daa6c0fd8d79ae45eac2dd0f274da1aa71c958
Author: Martin Sebor <msebor@redhat.com>
Date:   Mon Mar 8 13:37:21 2021 -0700

    PR middle-end/98266 - bogus array subscript is partly outside array bounds on virtual inheritance
    
    gcc/ChangeLog:
    
            PR middle-end/98266
            * gimple-array-bounds.cc (inbounds_vbase_memaccess_p): New function.
            (array_bounds_checker::check_array_bounds): Call it.
    
    gcc/testsuite/ChangeLog:
    
            PR middle-end/98266
            * g++.dg/warn/Warray-bounds-15.C: New test.
            * g++.dg/warn/Warray-bounds-18.C: New test.
            * g++.dg/warn/Warray-bounds-19.C: New test.
            * g++.dg/warn/Warray-bounds-20.C: New test.
            * g++.dg/warn/Warray-bounds-21.C: New test.
Comment 5 Martin Sebor 2021-03-08 20:39:27 UTC
Fixed in r11-7563.