public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101831] New: Spurious maybe-uninitialized warning on std::array::size
@ 2021-08-09 16:13 m101010a at gmail dot com
  2021-08-16 19:18 ` [Bug tree-optimization/101831] " msebor at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: m101010a at gmail dot com @ 2021-08-09 16:13 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101831
           Summary: Spurious maybe-uninitialized warning on
                    std::array::size
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m101010a at gmail dot com
  Target Milestone: ---

When calling std::array::size on an uninitialized array, gcc emits a warning:

$ cat x.cpp
#include <array>
int f() {
        std::array<char, 1> a;
        return a.size();
}
$ g++ -v -S -o /dev/null -Wmaybe-uninitialized x.cpp
Using built-in specs.
COLLECT_GCC=g++
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl
--with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit
--enable-cet=auto --enable-checking=release --enable-clocale=gnu
--enable-default-pie --enable-default-ssp --enable-gnu-indirect-function
--enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id
--enable-lto --enable-multilib --enable-plugin --enable-shared
--enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-libunwind-exceptions --disable-werror
gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.1.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-S' '-o' '/dev/null' '-Wmaybe-uninitialized'
'-shared-libgcc' '-mtune=generic' '-march=x86-64' '-dumpdir' '/dev/'
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/cc1plus -quiet -v -D_GNU_SOURCE x.cpp
-quiet -dumpdir /dev/ -dumpbase x.cpp -dumpbase-ext .cpp -mtune=generic
-march=x86-64 -Wmaybe-uninitialized -version -o /dev/null
GNU C++17 (GCC) version 11.1.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 11.1.0, GMP version 6.2.1, MPFR version
4.1.0, MPC version 1.2.1, isl version isl-0.24-GMP

warning: MPFR header version 4.1.0 differs from library version 4.1.0-p13.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/../../../../include/c++/11.1.0

/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/../../../../include/c++/11.1.0/x86_64-pc-linux-gnu

/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/../../../../include/c++/11.1.0/backward
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include-fixed
 /usr/include
End of search list.
GNU C++17 (GCC) version 11.1.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 11.1.0, GMP version 6.2.1, MPFR version
4.1.0, MPC version 1.2.1, isl version isl-0.24-GMP

warning: MPFR header version 4.1.0 differs from library version 4.1.0-p13.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 45eb9a71cc15bfdc579557bea4b77e51
x.cpp: In function ‘int f()’:
x.cpp:4:22: warning: ‘a’ may be used uninitialized [-Wmaybe-uninitialized]
    4 |         return a.size();
      |                ~~~~~~^~
In file included from x.cpp:1:
/usr/include/c++/11.1.0/array:176:7: note: by argument 1 of type ‘const
std::array<char, 1>*’ to ‘constexpr std::array<_Tp, _Nm>::size_type
std::array<_Tp, _Nm>::size() const [with _Tp = char; long unsigned int _Nm =
1]’ declared here
  176 |       size() const noexcept { return _Nm; }
      |       ^~~~
x.cpp:3:29: note: ‘a’ declared here
    3 |         std::array<char, 1> a;
      |                             ^
COMPILER_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-S' '-o' '/dev/null' '-Wmaybe-uninitialized'
'-shared-libgcc' '-mtune=generic' '-march=x86-64' '-dumpdir' '/dev/'

It also happens on the following reduced test case:

struct array {
  int d;
  int e() const { return 1; }
};
int f() {
  array a;
  return a.e();
}

This does not happen when optimization is enabled, and does not happen if
"-fsyntax-only" is used instead of "-S -o /dev/null".  This also happens with
GCC 11.2, but does not happen with GCC 10.

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

* [Bug tree-optimization/101831] Spurious maybe-uninitialized warning on std::array::size
  2021-08-09 16:13 [Bug c++/101831] New: Spurious maybe-uninitialized warning on std::array::size m101010a at gmail dot com
@ 2021-08-16 19:18 ` msebor at gcc dot gnu.org
  2022-01-21 21:32 ` kim.walisch at gmail dot com
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-08-16 19:18 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
                 CC|                            |msebor at gcc dot gnu.org
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-08-16

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
-Wmaybe-uninitialized is also issued when an uninitialized object is passed by
reference to a const-qualified argument.  This includes passing the address of
an such object to the implicit this pointer in calls to member functions.  This
form of the warning runs very early on and before any function calls have been
inlined, so it can't tell that the function doesn't actually read from the
uninitialized object.  The same effect can be reproduced in C in a call to a
non-member function (see below).  It's possible to run the early uninitialized
pass later but probably not without introducing some false negatives.  I'm not
sure that the std::array use case is common enough to justify the  potential
for the false negatives (or conversely, that the potential is significant
enough not to avoid the false positives).  So confirmed.  It requires some
thought and testing.

$ cat a.c && gcc -S -Wall -Wextra a.c
inline __attribute__ ((always_inline))
int f (const int *p) { (void)&p; return 0; }

int g (void)
{
  int i;
  return f (&i);
}
a.c: In function ‘g’:
a.c:7:10: warning: ‘i’ may be used uninitialized [-Wmaybe-uninitialized]
    7 |   return f (&i);
      |          ^~~~~~
a.c:2:5: note: by argument 1 of type ‘const int *’ to ‘f’ declared here
    2 | int f (const int *p) { (void)&p; return 0; }
      |     ^
a.c:6:7: note: ‘i’ declared here
    6 |   int i;
      |       ^

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

* [Bug tree-optimization/101831] Spurious maybe-uninitialized warning on std::array::size
  2021-08-09 16:13 [Bug c++/101831] New: Spurious maybe-uninitialized warning on std::array::size m101010a at gmail dot com
  2021-08-16 19:18 ` [Bug tree-optimization/101831] " msebor at gcc dot gnu.org
@ 2022-01-21 21:32 ` kim.walisch at gmail dot com
  2022-01-24 21:31 ` [Bug tree-optimization/101831] [11/12 Regression] " msebor at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: kim.walisch at gmail dot com @ 2022-01-21 21:32 UTC (permalink / raw)
  To: gcc-bugs

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

kim.walisch at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kim.walisch at gmail dot com

--- Comment #2 from kim.walisch at gmail dot com ---
> I'm not sure that the std::array use case is common enough to justify the  potential for the false negatives

I just hit the same GCC warning on completely valid code. Both Clang & MSVC
correctly do not issue any warning.

void Foo::func()
{
  std::array<uint64_t, 8> pos;
  assert(pos.size() == static_global_array.size());
  ...
}

In member function ‘void Foo::func()’:
warning: ‘pos’ may be used uninitialized [-Wmaybe-uninitialized]
  312 |   assert(pos.size() == buffers_.size());

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

* [Bug tree-optimization/101831] [11/12 Regression] Spurious maybe-uninitialized warning on std::array::size
  2021-08-09 16:13 [Bug c++/101831] New: Spurious maybe-uninitialized warning on std::array::size m101010a at gmail dot com
  2021-08-16 19:18 ` [Bug tree-optimization/101831] " msebor at gcc dot gnu.org
  2022-01-21 21:32 ` kim.walisch at gmail dot com
@ 2022-01-24 21:31 ` msebor at gcc dot gnu.org
  2022-01-27 21:34 ` msebor at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-01-24 21:31 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |11.2.0, 12.0
            Summary|Spurious                    |[11/12 Regression] Spurious
                   |maybe-uninitialized warning |maybe-uninitialized warning
                   |on std::array::size         |on std::array::size

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
Declaring std::array::size() with attribute ((const)) avoids the warning so
that might be a solution.

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

* [Bug tree-optimization/101831] [11/12 Regression] Spurious maybe-uninitialized warning on std::array::size
  2021-08-09 16:13 [Bug c++/101831] New: Spurious maybe-uninitialized warning on std::array::size m101010a at gmail dot com
                   ` (2 preceding siblings ...)
  2022-01-24 21:31 ` [Bug tree-optimization/101831] [11/12 Regression] " msebor at gcc dot gnu.org
@ 2022-01-27 21:34 ` msebor at gcc dot gnu.org
  2022-01-28  2:03 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-01-27 21:34 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |msebor at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

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

* [Bug tree-optimization/101831] [11/12 Regression] Spurious maybe-uninitialized warning on std::array::size
  2021-08-09 16:13 [Bug c++/101831] New: Spurious maybe-uninitialized warning on std::array::size m101010a at gmail dot com
                   ` (3 preceding siblings ...)
  2022-01-27 21:34 ` msebor at gcc dot gnu.org
@ 2022-01-28  2:03 ` pinskia at gcc dot gnu.org
  2022-02-01 18:54 ` msebor at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-28  2:03 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |11.3

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

* [Bug tree-optimization/101831] [11/12 Regression] Spurious maybe-uninitialized warning on std::array::size
  2021-08-09 16:13 [Bug c++/101831] New: Spurious maybe-uninitialized warning on std::array::size m101010a at gmail dot com
                   ` (4 preceding siblings ...)
  2022-01-28  2:03 ` pinskia at gcc dot gnu.org
@ 2022-02-01 18:54 ` msebor at gcc dot gnu.org
  2022-02-02  0:22 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-02-01 18:54 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
libstdc++ patch:
https://gcc.gnu.org/pipermail/gcc-patches/2022-February/589612.html

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

* [Bug tree-optimization/101831] [11/12 Regression] Spurious maybe-uninitialized warning on std::array::size
  2021-08-09 16:13 [Bug c++/101831] New: Spurious maybe-uninitialized warning on std::array::size m101010a at gmail dot com
                   ` (5 preceding siblings ...)
  2022-02-01 18:54 ` msebor at gcc dot gnu.org
@ 2022-02-02  0:22 ` cvs-commit at gcc dot gnu.org
  2022-02-02  0:24 ` [Bug tree-optimization/101831] [11 " msebor at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-02-02  0:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:756eabacfcd767e39eea63257a026f61a4c4e661

commit r12-6992-g756eabacfcd767e39eea63257a026f61a4c4e661
Author: Martin Sebor <msebor@redhat.com>
Date:   Tue Feb 1 17:19:11 2022 -0700

    Declare std::array members with attribute const [PR101831].

    Resolves:
    PR libstdc++/101831 - Spurious maybe-uninitialized warning on
std::array::size

    libstdc++-v3/ChangeLog:

            PR libstdc++/101831
            * include/std/array (begin): Declare const member function
attribute
            const.
            (end, rbegin, rend, size, max_size, empty, data): Same.
            * testsuite/23_containers/array/capacity/empty.cc: Add test cases.
            * testsuite/23_containers/array/capacity/max_size.cc: Same.
            * testsuite/23_containers/array/capacity/size.cc: Same.
            * testsuite/23_containers/array/iterators/begin_end.cc: New test.

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

* [Bug tree-optimization/101831] [11 Regression] Spurious maybe-uninitialized warning on std::array::size
  2021-08-09 16:13 [Bug c++/101831] New: Spurious maybe-uninitialized warning on std::array::size m101010a at gmail dot com
                   ` (6 preceding siblings ...)
  2022-02-02  0:22 ` cvs-commit at gcc dot gnu.org
@ 2022-02-02  0:24 ` msebor at gcc dot gnu.org
  2022-04-21  7:50 ` rguenth at gcc dot gnu.org
  2023-05-29 10:05 ` jakub at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-02-02  0:24 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[11/12 Regression] Spurious |[11 Regression] Spurious
                   |maybe-uninitialized warning |maybe-uninitialized warning
                   |on std::array::size         |on std::array::size

--- Comment #6 from Martin Sebor <msebor at gcc dot gnu.org> ---
Fixed in r12-6992.  Let me backport it after a bit.

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

* [Bug tree-optimization/101831] [11 Regression] Spurious maybe-uninitialized warning on std::array::size
  2021-08-09 16:13 [Bug c++/101831] New: Spurious maybe-uninitialized warning on std::array::size m101010a at gmail dot com
                   ` (7 preceding siblings ...)
  2022-02-02  0:24 ` [Bug tree-optimization/101831] [11 " msebor at gcc dot gnu.org
@ 2022-04-21  7:50 ` rguenth at gcc dot gnu.org
  2023-05-29 10:05 ` jakub at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-04-21  7:50 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.3                        |11.4

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 11.3 is being released, retargeting bugs to GCC 11.4.

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

* [Bug tree-optimization/101831] [11 Regression] Spurious maybe-uninitialized warning on std::array::size
  2021-08-09 16:13 [Bug c++/101831] New: Spurious maybe-uninitialized warning on std::array::size m101010a at gmail dot com
                   ` (8 preceding siblings ...)
  2022-04-21  7:50 ` rguenth at gcc dot gnu.org
@ 2023-05-29 10:05 ` jakub at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-29 10:05 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.4                        |11.5

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 11.4 is being released, retargeting bugs to GCC 11.5.

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

end of thread, other threads:[~2023-05-29 10:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-09 16:13 [Bug c++/101831] New: Spurious maybe-uninitialized warning on std::array::size m101010a at gmail dot com
2021-08-16 19:18 ` [Bug tree-optimization/101831] " msebor at gcc dot gnu.org
2022-01-21 21:32 ` kim.walisch at gmail dot com
2022-01-24 21:31 ` [Bug tree-optimization/101831] [11/12 Regression] " msebor at gcc dot gnu.org
2022-01-27 21:34 ` msebor at gcc dot gnu.org
2022-01-28  2:03 ` pinskia at gcc dot gnu.org
2022-02-01 18:54 ` msebor at gcc dot gnu.org
2022-02-02  0:22 ` cvs-commit at gcc dot gnu.org
2022-02-02  0:24 ` [Bug tree-optimization/101831] [11 " msebor at gcc dot gnu.org
2022-04-21  7:50 ` rguenth at gcc dot gnu.org
2023-05-29 10:05 ` jakub 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).