public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100485] New: False positive in -Wmismatched-new-delete
@ 2021-05-08  9:32 fiesh at zefix dot tv
  2021-05-09 17:49 ` [Bug c++/100485] " msebor at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: fiesh at zefix dot tv @ 2021-05-08  9:32 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100485
           Summary: False positive in -Wmismatched-new-delete
           Product: gcc
           Version: 11.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fiesh at zefix dot tv
  Target Milestone: ---

The following code triggers -Wmismatched-new-delete:

----------------------------------------

extern "C" void free(void *);
class Base
{
public:
        Base();
        void * operator new(unsigned long, const int &);
        void operator delete(void * ptr, const int &) { free(ptr); }
};
void f()
{
        new (0) Base;
}

----------------------------------------

Supplying `__attribute__((malloc (free, 1)))` to `operator new` also doesn't
help.

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

* [Bug c++/100485] False positive in -Wmismatched-new-delete
  2021-05-08  9:32 [Bug c++/100485] New: False positive in -Wmismatched-new-delete fiesh at zefix dot tv
@ 2021-05-09 17:49 ` msebor at gcc dot gnu.org
  2021-05-09 18:05 ` fiesh at zefix dot tv
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-05-09 17:49 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org
             Blocks|                            |100406
         Resolution|---                         |WONTFIX
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
The call to the inline operator delete is inlined but the call to new is not so
the warning sees a mismatch.  This can be suppressed either by inlining both
calls or by preventing it by declaring the operators with attribute noinline.  

The article below gives more insight:

https://developers.redhat.com/blog/2021/04/30/detecting-memory-management-bugs-with-gcc-11-part-1-understanding-dynamic-allocation/
https://developers.redhat.com/blog/2021/05/05/detecting-memory-management-bugs-with-gcc-11-part-2-deallocation-functions/


Referenced Bugs:

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

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

* [Bug c++/100485] False positive in -Wmismatched-new-delete
  2021-05-08  9:32 [Bug c++/100485] New: False positive in -Wmismatched-new-delete fiesh at zefix dot tv
  2021-05-09 17:49 ` [Bug c++/100485] " msebor at gcc dot gnu.org
@ 2021-05-09 18:05 ` fiesh at zefix dot tv
  2021-05-09 18:06 ` fiesh at zefix dot tv
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: fiesh at zefix dot tv @ 2021-05-09 18:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from fiesh at zefix dot tv ---
But this isn't really a solution since I can't inline new without moving a lot
of code into the header, and marking `operator new` noinline isn't what I want
either.  I read both articles prior to making this bug report (and asked on
IRC).  I don't understand why supplying `__attribute__((malloc (free, 1)))` to
operator new doesn't silence this since it should then expect a call to free.

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

* [Bug c++/100485] False positive in -Wmismatched-new-delete
  2021-05-08  9:32 [Bug c++/100485] New: False positive in -Wmismatched-new-delete fiesh at zefix dot tv
  2021-05-09 17:49 ` [Bug c++/100485] " msebor at gcc dot gnu.org
  2021-05-09 18:05 ` fiesh at zefix dot tv
@ 2021-05-09 18:06 ` fiesh at zefix dot tv
  2021-05-09 22:04 ` msebor at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: fiesh at zefix dot tv @ 2021-05-09 18:06 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from fiesh at zefix dot tv ---
* marking operator delete noinline

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

* [Bug c++/100485] False positive in -Wmismatched-new-delete
  2021-05-08  9:32 [Bug c++/100485] New: False positive in -Wmismatched-new-delete fiesh at zefix dot tv
                   ` (2 preceding siblings ...)
  2021-05-09 18:06 ` fiesh at zefix dot tv
@ 2021-05-09 22:04 ` msebor at gcc dot gnu.org
  2021-05-10  6:53 ` fiesh at zefix dot tv
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-05-09 22:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
-Wmismatched-new-delete considers only "mismatches between calls to operator
new or operator delete and the corresponding call to the allocation or
deallocation function."  It doesn't also consider attribute malloc, by design. 
It would be possible, even easy, to change that and have the warning also look
for attribute malloc to handle this case but it doesn't seem like a good design
feature to me to provide support for associating a member operator new() with
an unrelated deallocation function, whether it's free() or another operator
delete().  To do that, I would instead suggest suppressing the warning by using
#pragma GCC diagnostic ignored "-Wmismatched-new-delete":

extern "C" void free (void *);

class Base
{
public:
        Base();

        void * operator new(unsigned long, const int &);
        void operator delete(void * ptr, const int &) {
          #pragma GCC diagnostic ignored "-Wmismatched-new-delete"
          free (ptr);
        }
};

void f()
{
        new (0) Base;
}

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

* [Bug c++/100485] False positive in -Wmismatched-new-delete
  2021-05-08  9:32 [Bug c++/100485] New: False positive in -Wmismatched-new-delete fiesh at zefix dot tv
                   ` (3 preceding siblings ...)
  2021-05-09 22:04 ` msebor at gcc dot gnu.org
@ 2021-05-10  6:53 ` fiesh at zefix dot tv
  2021-05-10 14:39 ` msebor at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: fiesh at zefix dot tv @ 2021-05-10  6:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from fiesh at zefix dot tv ---

> extern "C" void free (void *);
>   
> class Base
> {
> public:
>         Base();
> 
>         void * operator new(unsigned long, const int &);
>         void operator delete(void * ptr, const int &) {
>           #pragma GCC diagnostic ignored "-Wmismatched-new-delete"
>           free (ptr);
>         }
> };
> 
> void f()
> {
>         new (0) Base;
> }

Alas, keeping the diagnostic ignore local by means of `diagnostic push` /
`diagnostic pop` makes it not work correctly, presumably since the warning is
generated at the call site.  Making it global to everything including the
header file seems like a bad idea to me.

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

* [Bug c++/100485] False positive in -Wmismatched-new-delete
  2021-05-08  9:32 [Bug c++/100485] New: False positive in -Wmismatched-new-delete fiesh at zefix dot tv
                   ` (4 preceding siblings ...)
  2021-05-10  6:53 ` fiesh at zefix dot tv
@ 2021-05-10 14:39 ` msebor at gcc dot gnu.org
  2021-05-10 14:45 ` fiesh at zefix dot tv
  2021-06-16 22:55 ` msebor at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-05-10 14:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Martin Sebor <msebor at gcc dot gnu.org> ---
That does sound frustrating, sorry.  The #pragma has a limitation that can make
it difficult to use in inlining contexts.  I submitted a patch for it for GCC
11 but it got pushed to GCC 12 (see pr98465 comment 32).  Until it's fixed, the
only other solution I can think of is to prevent the member operator delete
from inlining using attribute noinline.

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

* [Bug c++/100485] False positive in -Wmismatched-new-delete
  2021-05-08  9:32 [Bug c++/100485] New: False positive in -Wmismatched-new-delete fiesh at zefix dot tv
                   ` (5 preceding siblings ...)
  2021-05-10 14:39 ` msebor at gcc dot gnu.org
@ 2021-05-10 14:45 ` fiesh at zefix dot tv
  2021-06-16 22:55 ` msebor at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: fiesh at zefix dot tv @ 2021-05-10 14:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from fiesh at zefix dot tv ---
Thanks for the outline!  We'll turn off -Wmismatched-new-delete with GCC 11 and
try to switch to the selective opt-out with pragmas in 12.  That's a good
workaround for now.

On a random related note, the man page says -Wmismatched-new-delete is enabled
by default, but playing around with it, it seems it's only turned on by -Wall:
https://godbolt.org/z/n1s6Y8G85

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

* [Bug c++/100485] False positive in -Wmismatched-new-delete
  2021-05-08  9:32 [Bug c++/100485] New: False positive in -Wmismatched-new-delete fiesh at zefix dot tv
                   ` (6 preceding siblings ...)
  2021-05-10 14:45 ` fiesh at zefix dot tv
@ 2021-06-16 22:55 ` msebor at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-06-16 22:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Martin Sebor <msebor at gcc dot gnu.org> ---
(In reply to fiesh from comment #7)
> On a random related note, the man page says -Wmismatched-new-delete is
> enabled by default, but playing around with it, it seems it's only turned on
> by -Wall: https://godbolt.org/z/n1s6Y8G85

Thanks, I just fixed it in r12-1544.

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

end of thread, other threads:[~2021-06-16 22:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-08  9:32 [Bug c++/100485] New: False positive in -Wmismatched-new-delete fiesh at zefix dot tv
2021-05-09 17:49 ` [Bug c++/100485] " msebor at gcc dot gnu.org
2021-05-09 18:05 ` fiesh at zefix dot tv
2021-05-09 18:06 ` fiesh at zefix dot tv
2021-05-09 22:04 ` msebor at gcc dot gnu.org
2021-05-10  6:53 ` fiesh at zefix dot tv
2021-05-10 14:39 ` msebor at gcc dot gnu.org
2021-05-10 14:45 ` fiesh at zefix dot tv
2021-06-16 22:55 ` msebor 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).