https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107287
Bug ID: 107287
Summary: -Wuninitialized false positive (in all GCC versions)
Product: gcc
Version: 12.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: kim.walisch at gmail dot com
Target Milestone: ---
Hi,
In my primecount C++ project I have hit a -Wuninitialized false positive with
GCC. It happens basically with every g++ version >= 8 && <= 12 that I have
tested accross Ubuntu 18.04, 20.04 and 22.04 (x64). Unfortunately I have not
been able to create a minimal test case that reliably triggers this issue on
all GCC versions. However, I was able to create a minimal test case on
godbolt.org that triggers the issue on GCC trunk:
#include <iostream>
#include <vector>
extern double get_time();
long func(std::vector<int>& vect, bool print)
{
double time;
if (print)
time = get_time();
int sum = 0;
for (int n : vect)
sum += n;
if (print)
std::cout << time;
return sum;
}
When compiled with "x86-64 gcc (trunk)" and "-O3 -Wall -Werror -pedantic" this
produces the following warning (see https://godbolt.org/z/qv3W8j44b):
In file included from
/opt/compiler-explorer/gcc-trunk-20221017/include/c++/13.0.0/iostream:41,
from <source>:1:
In member function 'std::basic_ostream<_CharT, _Traits>::__ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char;
_Traits = std::char_traits<char>]',
inlined from 'long int func(std::vector<int>&, bool)' at <source>:18:22:
/opt/compiler-explorer/gcc-trunk-20221017/include/c++/13.0.0/ostream:223:25:
error: 'time' may be used uninitialized [-Werror=maybe-uninitialized]
223 | { return _M_insert(__f); }
| ~~~~~~~~~^~~~~
<source>: In function 'long int func(std::vector<int>&, bool)':
<source>:9:12: note: 'time' was declared here
9 | double time;
| ^~~~
cc1plus: all warnings being treated as errors
Compiler returned: 1
Using my primecount project this GCC warning can be reproduced reliably using:
git clone https://github.com/kimwalisch/primecount.git
git checkout git checkout 57f18f9207dc00aaadcd3f4c1e76e320e1387061
cmake . -DCMAKE_CXX_FLAGS="-Wall -Wextra -pedantic -Werror"
make -j8
My code is basically identical to the minimal test case above
(https://github.com/kimwalisch/primecount/blob/57f18f9207dc00aaadcd3f4c1e76e320e1387061/src/phi.cpp#L386):
int64_t phi(int64_t x,
int64_t a,
int threads,
bool is_print)
{
double time;
if (is_print)
{
print("");
print("=== phi(x, a) ===");
time = get_time();
}
int64_t sum = phi_OpenMP(x, a, threads);
if (is_print)
print("phi", sum, time);
return sum;
}
Also note that there is no warning if my code is compiled with Clang and
-Wuninitialized.
Regards,
Kim Walisch