[Bug c++/108088] New: False positive for -Wfree-nonheap-object when using std::variant

falemagn at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Dec 13 19:08:53 GMT 2022


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

            Bug ID: 108088
           Summary: False positive for -Wfree-nonheap-object when using
                    std::variant
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: falemagn at gmail dot com
  Target Milestone: ---

Created attachment 54083
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54083&action=edit
Preprocessed file

The following is the most reduced example I could come up with.

    #include <variant>
    #include <string>
    #include <vector>

    using variant = std::variant<
        std::string,
        std::vector<char>
    >;

    extern std::size_t &ext_index;

    void func() {
        ext_index = variant(std::string()).index();
    }

"ext_index" being a reference is all it takes to trigger the issue. If it'd be
an object, the issue would not triggered. The issue would be triggered also if
ext_index were a pointer.

As far as I could tell, ff "std::string" is substituted with a type whose
destructor doesn't involve any deallocation, then the issue is not triggered.

The issue is also not triggered if std::string is substituted with another
std::vector specialization.

You can see it all on godbolt: https://godbolt.org/z/aaxY1jW1q

This is the error (unecessary lines removed):

        In member function 'void std::__new_allocator<_Tp>::deallocate(_Tp*,
size_type) [with _Tp = char]',
                inlined from 'static void
std::allocator_traits<std::allocator<_CharT> >::deallocate(allocator_type&,
pointer, size_type) [with _Tp = char]' at /opt/compiler-explorer/gcc-12.1.0/

        [...]

                inlined from 'std::variant<_Types>::~variant() [with _Types =
{std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>
>, std::vector<char, std::allocator<char> >}]' at
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/variant:1407:28,
                inlined from 'void func()' at <source>:14:17:
       
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/new_allocator.h:158:33:
error: 'void operator delete(void*, std::size_t)' called on unallocated object
'<anonymous>' [-Werror=free-nonheap-object]
          158 |         _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p,
__n));
                  |                                 ^
        <source>: In function 'void func()':
        <source>:14:38: note: declared here
           14 |     ext_index = variant(std::string()).index();
                  |                                      ^


More information about the Gcc-bugs mailing list