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

            Bug ID: 115016
           Summary: False positive -Wfree-nonheap-object using std::vector
                    with -O3
           Product: gcc
           Version: 14.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lloyd at randombit dot net
  Target Milestone: ---

Following source

-----
#include <vector>

void serialize(const std::vector<char>& m_response) {
   std::vector<char> buf;
   buf.reserve(1 + m_response.size());
   buf.push_back(1);
}
-----

Hits a "-Wfree-nonheap-object" warning with GCC 14 *only* with `-O3`. With
`-O2` or lower, no warning is emitted. Interestingly it also does not trigger
the warning if the vector contains another type, such as int.

Godbolt link with repro source showing the warning
https://godbolt.org/z/GKMWbobW8

Full warning text:

In file included from
/usr/include/c++/14.1.1/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from /usr/include/c++/14.1.1/bits/allocator.h:46,
                 from /usr/include/c++/14.1.1/vector:63,
                 from ser.cpp:1:
In member function ‘void std::__new_allocator<_Tp>::deallocate(_Tp*, size_type)
[with _Tp = char]’,
    inlined from ‘static void std::allocator_traits<std::allocator<_Tp1>
>::deallocate(allocator_type&, pointer, size_type) [with _Tp = char]’ at
/usr/include/c++/14.1.1/bits/alloc_traits.h:513:23,
    inlined from ‘std::vector<_Tp, _Alloc>::_M_realloc_append(_Args&&
...)::_Guard::~_Guard() [with _Args = {char}; _Tp = char; _Alloc =
std::allocator<char>]’ at /usr/include/c++/14.1.1/bits/vector.tcc:616:18,
    inlined from ‘void std::vector<_Tp, _Alloc>::_M_realloc_append(_Args&& ...)
[with _Args = {char}; _Tp = char; _Alloc = std::allocator<char>]’ at
/usr/include/c++/14.1.1/bits/vector.tcc:688:7,
    inlined from ‘std::vector<_Tp, _Alloc>::reference std::vector<_Tp,
_Alloc>::emplace_back(_Args&& ...) [with _Args = {char}; _Tp = char; _Alloc =
std::allocator<char>]’ at /usr/include/c++/14.1.1/bits/vector.tcc:123:21,
    inlined from ‘void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with
_Tp = char; _Alloc = std::allocator<char>]’ at
/usr/include/c++/14.1.1/bits/stl_vector.h:1301:21,
    inlined from ‘void serialize(const std::vector<char>&)’ at ser.cpp:6:17:
/usr/include/c++/14.1.1/bits/new_allocator.h:172:33: warning: ‘void operator
delete(void*, std::size_t)’ called on pointer ‘<unknown>’ with nonzero offset
[1, 9223372036854775807] [-Wfree-nonheap-object]
  172 |         _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n));
      |                                 ^
In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const
void*) [with _Tp = char]’,
    inlined from ‘static _Tp* std::allocator_traits<std::allocator<_Tp1>
>::allocate(allocator_type&, size_type) [with _Tp = char]’ at
/usr/include/c++/14.1.1/bits/alloc_traits.h:478:28,
    inlined from ‘std::_Vector_base<_Tp, _Alloc>::pointer
std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = char;
_Alloc = std::allocator<char>]’ at
/usr/include/c++/14.1.1/bits/stl_vector.h:380:33,
    inlined from ‘void std::vector<_Tp, _Alloc>::reserve(size_type) [with _Tp =
char; _Alloc = std::allocator<char>]’ at
/usr/include/c++/14.1.1/bits/vector.tcc:79:33,
    inlined from ‘void serialize(const std::vector<char>&)’ at ser.cpp:5:15:
/usr/include/c++/14.1.1/bits/new_allocator.h:151:55: note: returned from ‘void*
operator new(std::size_t)’
  151 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n *
sizeof(_Tp)));
      |               


$ g++ -v
Using built-in specs.
COLLECT_GCC=/bin/g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/14.1.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure
--enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust
--enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues
--with-build-config=bootstrap-lto --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-libstdcxx-backtrace --enable-link-serialization=1
--enable-linker-build-id --enable-lto --enable-multilib --enable-plugin
--enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-werror
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.1.1 20240507 (GCC)

Reply via email to