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

            Bug ID: 117259
           Summary: warning: 'j.6' may be used uninitialized
                    [-Wmaybe-unitialized] with -fsanitize=undefined
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m.gcc.gnu.org at alias dot viem.se
  Target Milestone: ---

/*
Accessing dynamically initialized array of global member function pointer using
a non const index variable gives false and ill-formed warning if compiling with
-fsanitize=undefined (bounds) and -O1.

Warning disappears if either:
1. changing to static initialization by removing user defined B constructor
2. using const index variable (with same value)
3. using -O0
4. using no -fsanitize or something else than -fsanitize=undefined,
   -fsanitize=bounds, or, -fsanitize=bounds-strict
5. using gcc 10 or older

$ gcc -fsanitize=undefined -Wmaybe-uninitialized -O1 -c bug.cpp
bug.cpp: In member function 'void A::f()':
bug.cpp:48:15: warning: 'j.6' may be used uninitialized [-Wmaybe-uninitialized]
   48 |   (this->*bs[j].g)(); // warning: 'j.6' may be used uninitialized
      |           ~~~~^
bug.cpp:48:15: note: 'j.6' was declared here
   48 |   (this->*bs[j].g)(); // warning: 'j.6' may be used uninitialized
      |           ~~~~^

gcc 11.1 up to gcc 14.2 show warning.
gcc 12.1 up to gcc 14.2 also show note.

 */

struct A {
  struct B {
    void (A::*g) ();

    B(void (A::*func)()) : g(func) {};
  };

  static const B bs[1];

  void f();
  void h(){}
};

const A::B A::bs[1] = {{&A::h}};

void A::f() {
  (this->*bs[0].g)(); // no warning
  const unsigned i = 0;
  (this->*bs[i].g)(); // no warning
  unsigned j = 0;
  (this->*bs[j].g)(); // warning: 'j.6' may be used uninitialized
}

Reply via email to