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

            Bug ID: 104603
           Summary: wrong detection of g++ -Warray-bounds about downcast
           Product: gcc
           Version: 10.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: herumi at nifty dot com
  Target Milestone: ---

Created attachment 52477
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52477&action=edit
a minimal sample of the bug

g++-10.3.0 and g++-11.2 -O2 -Warray-bounds on Ubuntu 20.04.3 LTS show wrong
warnings to the attachment code.

g++ -O2 -Warray-bounds -DA t.cpp does not show the warnings.
g++-9 -O2 -Warray-bounds does not, too.

---
>cat t.cpp
struct Base {
  bool isX_;
  Base(bool isX = false) : isX_(isX) { }
  bool isX() const { return isX_; }
  bool operator==(const Base& rhs) const;
};

struct X : public Base {
  X(const Base& b) : Base(true), b_(b) { }
  bool operator==(const X& rhs) const { return b_ == rhs.b_; }
  Base b_;
};

inline bool Base::operator==(const Base& rhs) const
{
    return isX() && rhs.isX() && static_cast<const X&>(*this) ==
static_cast<const X&>(rhs);
}

Base base;

#ifndef A
void f()
{
  X(base) == X(base);
}
#endif

int main()
{
#ifdef A
  X(base) == X(base);
#endif
}
---

---
% g++-10 --version
g++-10 (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0
% g++-10 -O2 -Warray-bounds array-bounds-bug.cpp
array-bounds-bug.cpp: In function 'void f()':
array-bounds-bug.cpp:18:29: warning: array subscript 2 is outside array bounds
of 'X [1]' [-Warray-bounds]
   18 |   bool isX() const { return isX_; }
      |                             ^~~~
array-bounds-bug.cpp:38:9: note: while referencing '<anonymous>'
   38 |   X(base) == X(base);
      |         ^
array-bounds-bug.cpp:18:29: warning: array subscript 2 is outside array bounds
of 'X [1]' [-Warray-bounds]
   18 |   bool isX() const { return isX_; }
      |                             ^~~~
array-bounds-bug.cpp:38:20: note: while referencing '<anonymous>'
   38 |   X(base) == X(base);
      |                    ^
array-bounds-bug.cpp:18:29: warning: array subscript 3 is outside array bounds
of 'X [1]' [-Warray-bounds]
   18 |   bool isX() const { return isX_; }
      |                             ^~~~
array-bounds-bug.cpp:38:9: note: while referencing '<anonymous>'
   38 |   X(base) == X(base);
      |         ^
array-bounds-bug.cpp:18:29: warning: array subscript 3 is outside array bounds
of 'X [1]' [-Warray-bounds]
   18 |   bool isX() const { return isX_; }
      |                             ^~~~
array-bounds-bug.cpp:38:20: note: while referencing '<anonymous>'
   38 |   X(base) == X(base);
      |                    ^
array-bounds-bug.cpp:18:29: warning: array subscript 4 is outside array bounds
of 'X [1]' [-Warray-bounds]
   18 |   bool isX() const { return isX_; }
      |                             ^~~~
array-bounds-bug.cpp:38:9: note: while referencing '<anonymous>'
   38 |   X(base) == X(base);
      |         ^
array-bounds-bug.cpp:18:29: warning: array subscript 4 is outside array bounds
of 'X [1]' [-Warray-bounds]
   18 |   bool isX() const { return isX_; }
      |                             ^~~~
array-bounds-bug.cpp:38:20: note: while referencing '<anonymous>'
   38 |   X(base) == X(base);
      |                    ^
array-bounds-bug.cpp:18:29: warning: array subscript 5 is outside array bounds
of 'X [1]' [-Warray-bounds]
   18 |   bool isX() const { return isX_; }
      |                             ^~~~
array-bounds-bug.cpp:38:9: note: while referencing '<anonymous>'
   38 |   X(base) == X(base);
      |         ^
array-bounds-bug.cpp:18:29: warning: array subscript 5 is outside array bounds
of 'X [1]' [-Warray-bounds]
   18 |   bool isX() const { return isX_; }
      |                             ^~~~
array-bounds-bug.cpp:38:20: note: while referencing '<anonymous>'
   38 |   X(base) == X(base);
      |                    ^
array-bounds-bug.cpp:24:51: warning: array subscript 2 is outside array bounds
of 'X [1]' [-Warray-bounds]
   24 |   bool operator==(const X& rhs) const { return b_ == rhs.b_; }
      |                                                ~~~^~~~~~~~~
array-bounds-bug.cpp:38:9: note: while referencing '<anonymous>'
   38 |   X(base) == X(base);
      |         ^
array-bounds-bug.cpp:24:58: warning: array subscript 2 is outside array bounds
of 'X [1]' [-Warray-bounds]
   24 |   bool operator==(const X& rhs) const { return b_ == rhs.b_; }
      |                                                      ~~~~^~
array-bounds-bug.cpp:38:20: note: while referencing '<anonymous>'
   38 |   X(base) == X(base);
---

Reply via email to