public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107287] New: -Wuninitialized false positive (in all GCC versions)
@ 2022-10-17  8:23 kim.walisch at gmail dot com
  2022-10-17 11:32 ` [Bug c++/107287] " rguenth at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: kim.walisch at gmail dot com @ 2022-10-17  8:23 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/107287] -Wuninitialized false positive (in all GCC versions)
  2022-10-17  8:23 [Bug c++/107287] New: -Wuninitialized false positive (in all GCC versions) kim.walisch at gmail dot com
@ 2022-10-17 11:32 ` rguenth at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-10-17 11:32 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Blocks|                            |24639
   Last reconfirmed|                            |2022-10-17
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  I think we get confused by unrelated predicates, but eventually the
way we factor the initialized problem simply isn't suited for the CFG we see.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=24639
[Bug 24639] [meta-bug] bug to track all Wuninitialized issues

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-17 11:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-17  8:23 [Bug c++/107287] New: -Wuninitialized false positive (in all GCC versions) kim.walisch at gmail dot com
2022-10-17 11:32 ` [Bug c++/107287] " rguenth at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).