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

            Bug ID: 106446
           Summary: -Warray-bounds false positive on downcast under
                    condition
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: coillol at yandex dot ru
  Target Milestone: ---

Minimized example:

---
struct B { int x = 1; };

struct D1 : B {};

struct Data {
  int val = 666;
};

struct D2 : Data, B {
  static bool is_me(const B &b);
};

static int bar(const B &b) {
  if (D2::is_me(b))
    return static_cast<const D2 &>(b).val;
  return 777;
};

int foo() {
  return bar(D1{});
}
---

Compiling using `g++
(Compiler-Explorer-Build-gcc-789c4b9bb015c361bc1a6adfcd0abadce555e562-binutils-2.38)
13.0.0 20220725 (experimental)` with `-O2 -Wall` there is a warning:

--
In function 'int bar(const B&)',
    inlined from 'int foo()' at <source>:20:13:
<source>:15:39: warning: array subscript 0 is outside array bounds of 'D1 [1]'
[-Warray-bounds]
   15 |     return static_cast<const D2 &>(b).val;
      |                                       ^~~
<source>: In function 'int foo()':
<source>:20:17: note: at offset -4 into object '<anonymous>' of size 4
   20 |   return bar(D1{});
      |    
--

Such behavior is observed with from gcc >= 10.1. Less minimized example --
https://godbolt.org/z/vb786jhfn -- can be reproduced only with gcc >= 12.1.

Reply via email to