public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/108088] New: False positive for -Wfree-nonheap-object when using std::variant
@ 2022-12-13 19:08 falemagn at gmail dot com
  2022-12-13 19:20 ` [Bug tree-optimization/108088] " pinskia at gcc dot gnu.org
  2022-12-13 20:46 ` redi at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: falemagn at gmail dot com @ 2022-12-13 19:08 UTC (permalink / raw)
  To: gcc-bugs

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();
                  |                                      ^

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

* [Bug tree-optimization/108088] False positive for -Wfree-nonheap-object when using std::variant
  2022-12-13 19:08 [Bug c++/108088] New: False positive for -Wfree-nonheap-object when using std::variant falemagn at gmail dot com
@ 2022-12-13 19:20 ` pinskia at gcc dot gnu.org
  2022-12-13 20:46 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-13 19:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
-O2 -std=c++17 -Wall -Werror=free-nonheap-object

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

* [Bug tree-optimization/108088] False positive for -Wfree-nonheap-object when using std::variant
  2022-12-13 19:08 [Bug c++/108088] New: False positive for -Wfree-nonheap-object when using std::variant falemagn at gmail dot com
  2022-12-13 19:20 ` [Bug tree-optimization/108088] " pinskia at gcc dot gnu.org
@ 2022-12-13 20:46 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2022-12-13 20:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
*** Bug 108089 has been marked as a duplicate of this bug. ***

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

end of thread, other threads:[~2022-12-13 20:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-13 19:08 [Bug c++/108088] New: False positive for -Wfree-nonheap-object when using std::variant falemagn at gmail dot com
2022-12-13 19:20 ` [Bug tree-optimization/108088] " pinskia at gcc dot gnu.org
2022-12-13 20:46 ` redi 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).