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

            Bug ID: 83468
           Summary: -Wuninitialized warning not emitted when it should
           Product: gcc
           Version: 7.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hannesroest at gmx dot ch
  Target Milestone: ---

In the following code, gcc does not emit a warning / error even when compiled
with -Wuninitialized -Werror -Wmaybe-uninitialized :

#include <iostream>

class klass 
{
public: void test2(double k) {std::cout << k << std::endl; k += 42; }
public: void test3(double& k) {k = 42;}
};

int main(int argc, const char** argv)
{
    double q;
    klass* ptr;
    ptr = new klass();
    ptr->test2(q);
    ptr->test3(q);
    ptr->test2(q);
}

where it should emit a warning about the line "ptr->test2(q);" where q is used
unitialized and even written to stdout. It clearly depends on two factors, ptr
being a pointer and the fact that test3 is getting called. Note that if the
line "ptr->test3(q);" is commented out or if an object instead of a pointer is
used, then gcc correctly emits a warning. Also if the line "ptr->test3(q);" is
replaced by "q = 42;" it will correctly emit a warning. 

We ran into this issue in an automated test environment where the unitialized
value of the variable "q" then had the value of a specific memory location,
which made the error hard to track down and we expected "-Wuninitialized" to
catch this.

The issue is present in gcc 7.2 and current trunk. Clang correctly emits the
warning in the above code.

Also note that a slight variation of this will depend on the optimization
level:

#include <iostream>

class klass 
{
public: void test(double& k) {k += 42;}
};

int main(int argc, const char** argv)
{
    double q;
    klass* ptr;
    ptr = new klass();
    q += 42;
    ptr->test(q);
    std::cout << q << std::endl;
}

will compile fine without optimization but will emit the warning with -O1, -O2,
-O3

Reply via email to