public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/103993] New: Incorrect error generated by mismatched-new-delete
@ 2022-01-12 16:39 jjlindal at gmail dot com
  2022-01-12 16:41 ` [Bug c++/103993] " jjlindal at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: jjlindal at gmail dot com @ 2022-01-12 16:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103993
           Summary: Incorrect error generated by mismatched-new-delete
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jjlindal at gmail dot com
  Target Milestone: ---

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-11.2.1-20211203/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-gnu-indirect-function --enable-cet --with-tune=generic
--with-arch_32=i686 --build=x86_64-redhat-linux
--with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.1 20211203 (Red Hat 11.2.1-7) (GCC) 

$ g++ -std=c++17 -D_J_HAS_PROC -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED
-D_XFT_NO_COMPAT_ -I/usr/include/freetype2  -D_J_UNIX -D_J_HAS_XINERAMA -Wall
-Werror -I../include -I/usr/include/freetype2 -I/usr/include/libxml2 -O2  -c -o
code/JSimpleProcess.o code/JSimpleProcess.cpp -save-temps
In file included from /usr/local/include/ace/Svc_Handler.h:344,
                 from ../include/jx-af/jcore/JNetworkProtocolBase.h:11,
                 from ../include/jx-af/jcore/JMessageProtocol.h:11,
                 from ../include/jx-af/jcore/JSimpleProcess.h:16,
                 from code/JSimpleProcess.cpp:18:
In static member function ‘static void ACE_Svc_Handler<PEER_STREAM,
SYNCH_TRAITS>::operator delete(void*) [with PEER_STREAM = ACE_LSOCK_Stream;
SYNCH_TRAITS = ACE_MT_SYNCH]’,
    inlined from ‘JSimpleProcess::JSimpleProcess(pid_t, int, bool)’ at
code/JSimpleProcess.cpp:322:30:
/usr/local/include/ace/Svc_Handler.cpp:117:3: error: ‘void operator delete
[](void*)’ called on pointer returned from a mismatched allocation function
[-Werror=mismatched-new-delete]
  117 |   ::delete [] static_cast <char *> (obj);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
code/JSimpleProcess.cpp: In constructor ‘JSimpleProcess::JSimpleProcess(pid_t,
int, bool)’:
code/JSimpleProcess.cpp:322:37: note: returned from ‘static void*
ACE_Svc_Handler<PEER_STREAM, SYNCH_TRAITS>::operator new(size_t) [with
PEER_STREAM = ACE_LSOCK_Stream; SYNCH_TRAITS = ACE_MT_SYNCH]’
  322 |         itsLink = new ProcessLink(fd);
      |                                     ^
In file included from /usr/local/include/ace/Svc_Handler.h:344,
                 from ../include/jx-af/jcore/JNetworkProtocolBase.h:11,
                 from ../include/jx-af/jcore/JMessageProtocol.h:11,
                 from ../include/jx-af/jcore/JSimpleProcess.h:16,
                 from code/JSimpleProcess.cpp:18:
In static member function ‘static void ACE_Svc_Handler<PEER_STREAM,
SYNCH_TRAITS>::operator delete(void*) [with PEER_STREAM = ACE_LSOCK_Stream;
SYNCH_TRAITS = ACE_MT_SYNCH]’,
    inlined from ‘JSimpleProcess::JSimpleProcess(pid_t, int, bool)’ at
code/JSimpleProcess.cpp:322:30:
/usr/local/include/ace/Svc_Handler.cpp:117:3: error: ‘void operator delete
[](void*)’ called on pointer returned from a mismatched allocation function
[-Werror=mismatched-new-delete]
  117 |   ::delete [] static_cast <char *> (obj);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
code/JSimpleProcess.cpp: In constructor ‘JSimpleProcess::JSimpleProcess(pid_t,
int, bool)’:
code/JSimpleProcess.cpp:322:37: note: returned from ‘static void*
ACE_Svc_Handler<PEER_STREAM, SYNCH_TRAITS>::operator new(size_t) [with
PEER_STREAM = ACE_LSOCK_Stream; SYNCH_TRAITS = ACE_MT_SYNCH]’
  322 |         itsLink = new ProcessLink(fd);
      |                                     ^
cc1plus: all warnings being treated as errors

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

* [Bug c++/103993] Incorrect error generated by mismatched-new-delete
  2022-01-12 16:39 [Bug c++/103993] New: Incorrect error generated by mismatched-new-delete jjlindal at gmail dot com
@ 2022-01-12 16:41 ` jjlindal at gmail dot com
  2022-01-12 18:31 ` [Bug middle-end/103993] -Wismatched-new-delete due to difference in inlining decisions msebor at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jjlindal at gmail dot com @ 2022-01-12 16:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from JohnJiming Lindal <jjlindal at gmail dot com> ---
Created attachment 52171
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52171&action=edit
preprocessed output

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

* [Bug middle-end/103993] -Wismatched-new-delete due to difference in inlining decisions
  2022-01-12 16:39 [Bug c++/103993] New: Incorrect error generated by mismatched-new-delete jjlindal at gmail dot com
  2022-01-12 16:41 ` [Bug c++/103993] " jjlindal at gmail dot com
@ 2022-01-12 18:31 ` msebor at gcc dot gnu.org
  2022-04-25 21:13 ` andre at kostur dot net
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-01-12 18:31 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |100406
                 CC|                            |msebor at gcc dot gnu.org
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=100861
            Summary|Incorrect error generated   |-Wismatched-new-delete due
                   |by mismatched-new-delete    |to difference in inlining
                   |                            |decisions
           Keywords|                            |diagnostic
          Component|c++                         |middle-end

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
The reported mismatch is between

  void operator delete [](void*)

and

  void* ACE_Svc_Handler<PEER_STREAM, SYNCH_TRAITS>::operator new(size_t)

i.e., the array form of the global operator delete is being called on the
result of a scalar member operator new.  The deallocation function
corresponding to a member array new is a member array delete, so the warning
isn't incorrect per se.

The warning is based on the compiler's view of the optimized code, after
inlining and other transformations.  If the program provides overloads of
operators new and delete for some type and GCC inlines a call to one but not
the other, the warning will trigger because it sees a mismatch.  To avoid this
problem either prevent the inlining of both the allocation and the deallocation
function (using attribute noinline), or force both to be inlined (by declaring
them inline and attribute always_inline).  The diff below shows the latter
solution.  It's possible for GCC do the former automatically (and it does that
for a subset of these cases) but given it's not without a performance cost it
seems preferable to leave the choice up to the programmer.  Another approach
was suggested in pr101829.  See also pr100861 for a discussions of another
similar issue.

(When reporting a problem it's helpful to explain why you think it's in GCC
rather than in the submitted test case so we don't have to guess.)

$ diff -u JSimpleProcess.C JSimpleProcess-fix.C
--- JSimpleProcess.C    2022-01-12 11:06:52.787461649 -0700
+++ JSimpleProcess-fix.C        2022-01-12 11:18:30.085356124 -0700
@@ -78365,7 +78365,8 @@
   ;
   return;
 }
-template <typename PEER_STREAM, typename SYNCH_TRAITS> void *
+template <typename PEER_STREAM, typename SYNCH_TRAITS>
+inline __attribute__ ((always_inline)) void *
 ACE_Svc_Handler<PEER_STREAM, SYNCH_TRAITS>::operator new (size_t n)
 {
   ;
@@ -78378,10 +78379,11 @@
   else
     {
       dynamic_instance->set ();
-      return ::new char[n];
+      return ::operator new (n);
     }
 }
-template <typename PEER_STREAM, typename SYNCH_TRAITS> void *
+template <typename PEER_STREAM, typename SYNCH_TRAITS>
+inline __attribute__ ((always_inline)) void *
 ACE_Svc_Handler<PEER_STREAM, SYNCH_TRAITS>::operator new (size_t n,
                                                           const
::std::nothrow_t&) throw()
 {
@@ -78395,15 +78397,16 @@
   else
     {
       dynamic_instance->set ();
-      return ::new(::std::nothrow) char[n];
+      return ::operator new(n, ::std::nothrow);
     }
 }
-template <typename PEER_STREAM, typename SYNCH_TRAITS> void
+template <typename PEER_STREAM, typename SYNCH_TRAITS>
+inline __attribute__ ((always_inline)) void
 ACE_Svc_Handler<PEER_STREAM, SYNCH_TRAITS>::operator delete (void *p,
                                          const ::std::nothrow_t&) throw()
 {
   ;
-  ::delete [] static_cast <char *> (p);
+  ::operator delete (p);
 }
 template <typename PEER_STREAM, typename SYNCH_TRAITS> void
 ACE_Svc_Handler<PEER_STREAM, SYNCH_TRAITS>::destroy ()
@@ -78412,11 +78415,12 @@
   if (this->mod_ == 0 && this->dynamic_ && this->closing_ == false)
     delete this;
 }
-template <typename PEER_STREAM, typename SYNCH_TRAITS> void
+template <typename PEER_STREAM, typename SYNCH_TRAITS>
+inline __attribute__ ((always_inline)) void
 ACE_Svc_Handler<PEER_STREAM, SYNCH_TRAITS>::operator delete (void *obj)
 {
   ;
-  ::delete [] static_cast <char *> (obj);
+  ::operator delete (obj);
 }
 template <typename PEER_STREAM, typename SYNCH_TRAITS>
 ACE_Svc_Handler<PEER_STREAM, SYNCH_TRAITS>::ACE_Svc_Handler
(ACE_Thread_Manager *tm,


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100406
[Bug 100406] bogus/missing -Wmismatched-new-delete

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

* [Bug middle-end/103993] -Wismatched-new-delete due to difference in inlining decisions
  2022-01-12 16:39 [Bug c++/103993] New: Incorrect error generated by mismatched-new-delete jjlindal at gmail dot com
  2022-01-12 16:41 ` [Bug c++/103993] " jjlindal at gmail dot com
  2022-01-12 18:31 ` [Bug middle-end/103993] -Wismatched-new-delete due to difference in inlining decisions msebor at gcc dot gnu.org
@ 2022-04-25 21:13 ` andre at kostur dot net
  2022-06-29 18:08 ` ed at catmur dot uk
  2023-04-29 19:30 ` cdfrey at foursquare dot net
  4 siblings, 0 replies; 6+ messages in thread
From: andre at kostur dot net @ 2022-04-25 21:13 UTC (permalink / raw)
  To: gcc-bugs

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

andre at kostur dot net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andre at kostur dot net

--- Comment #3 from andre at kostur dot net ---
Here's a smaller, minimal example of the issue (as described in the subject):

#include <cstdlib>
struct Foo {
   static void * operator new( size_t s ) __attribute__( ( noinline ) ) {
      return ::operator new( s );
   }
   static void operator delete( void * p ) { return ::operator delete( p ); }
};

int
main() {
   Foo * f = new Foo();
   delete f;
}

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

* [Bug middle-end/103993] -Wismatched-new-delete due to difference in inlining decisions
  2022-01-12 16:39 [Bug c++/103993] New: Incorrect error generated by mismatched-new-delete jjlindal at gmail dot com
                   ` (2 preceding siblings ...)
  2022-04-25 21:13 ` andre at kostur dot net
@ 2022-06-29 18:08 ` ed at catmur dot uk
  2023-04-29 19:30 ` cdfrey at foursquare dot net
  4 siblings, 0 replies; 6+ messages in thread
From: ed at catmur dot uk @ 2022-06-29 18:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Ed Catmur <ed at catmur dot uk> ---
And another example, provoked by throwing new (this only happens at -Og):

#include <cstdlib>
struct D {
    D();
    static void* operator new (std::size_t s) {
        if (void* p = ::malloc(s))
            return p;
        throw "bad_alloc";
    }
    static void operator delete (void* p) { ::free(p); }
};
int main() { new D; }

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

* [Bug middle-end/103993] -Wismatched-new-delete due to difference in inlining decisions
  2022-01-12 16:39 [Bug c++/103993] New: Incorrect error generated by mismatched-new-delete jjlindal at gmail dot com
                   ` (3 preceding siblings ...)
  2022-06-29 18:08 ` ed at catmur dot uk
@ 2023-04-29 19:30 ` cdfrey at foursquare dot net
  4 siblings, 0 replies; 6+ messages in thread
From: cdfrey at foursquare dot net @ 2023-04-29 19:30 UTC (permalink / raw)
  To: gcc-bugs

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

cdfrey at foursquare dot net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |cdfrey at foursquare dot net

--- Comment #5 from cdfrey at foursquare dot net ---
This happens for me.  Here is a test case, for gcc 13.1.0, which gives the
warning with this command:

/home/gcc/rootdir/gcc-13.1.0/bin/g++ -Wall -O2 --std=c++17 -Wall -Wextra
-Werror -fmax-errors=4   -c -o gcc-test-case.o gcc-test-case.cc
gcc-test-case.cc: In function ‘int main()’:
gcc-test-case.cc:49:16: error: ‘static void Test::operator delete(void*)’
called on pointer returned from a mismatched allocation function
[-Werror=mismatched-new-delete]
   49 |         delete t;
      |                ^
In static member function ‘static void* Test::operator new(size_t)’,
    inlined from ‘int main()’ at gcc-test-case.cc:48:10:
gcc-test-case.cc:17:30: note: returned from ‘void* malloc(size_t)’
   17 |                 return malloc(size);
      |                        ~~~~~~^~~~~~
gcc-test-case.cc: In function ‘int main()’:
gcc-test-case.cc:48:17: error: ‘static void Test::operator delete(void*)’
called on pointer returned from a mismatched allocation function
[-Werror=mismatched-new-delete]
   48 |         t = new Test;
      |                 ^~~~
In static member function ‘static void* Test::operator new(size_t)’,
    inlined from ‘int main()’ at gcc-test-case.cc:48:10:
gcc-test-case.cc:17:30: note: returned from ‘void* malloc(size_t)’
   17 |                 return malloc(size);
      |                        ~~~~~~^~~~~~
cc1plus: all warnings being treated as errors
make: *** [<builtin>: gcc-test-case.o] Error 1


The code:

#include <iostream>     // cout statements needed to trigger warning
#include <new>
#include <stdlib.h>
using namespace std;

struct Test
{
        double a, b, c;

        Test() {
                cout << "Test::Test()\n";
        }

        void* operator new(size_t size)
        {
                cout << "Test::operator new(" << size << ")\n";
                return malloc(size);
        }

        void* operator new(size_t size, align_val_t al)
        {
                cout << "Test::operator new(" << size << ", align: " <<
(size_t)al << ")\n";
                return aligned_alloc((size_t)al, size);
        }

        void* operator new(size_t size, const std::nothrow_t &) throw()
        {
                cout << "Test::operator new(" << size << ", nothrow)\n";
                return malloc(size);
        }

        void operator delete(void *p)
        {
                cout << "Test::operator delete()\n";
                free(p);
        }

        void operator delete(void *p, align_val_t al)
        {
                cout << "Test::operator delete(p, align: " << (size_t)al <<
")\n";
                free(p);
        }
};

int main(void)
{
        Test *t;
        t = new Test;
        delete t;
}

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

end of thread, other threads:[~2023-04-29 19:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-12 16:39 [Bug c++/103993] New: Incorrect error generated by mismatched-new-delete jjlindal at gmail dot com
2022-01-12 16:41 ` [Bug c++/103993] " jjlindal at gmail dot com
2022-01-12 18:31 ` [Bug middle-end/103993] -Wismatched-new-delete due to difference in inlining decisions msebor at gcc dot gnu.org
2022-04-25 21:13 ` andre at kostur dot net
2022-06-29 18:08 ` ed at catmur dot uk
2023-04-29 19:30 ` cdfrey at foursquare dot net

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).