public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new
@ 2021-06-02 13:46 redbeard0531 at gmail dot com
  2021-06-02 15:19 ` [Bug c++/100876] " redi at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: redbeard0531 at gmail dot com @ 2021-06-02 13:46 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100876
           Summary: -Wmismatched-new-delete should either look through or
                    ignore placement new
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redbeard0531 at gmail dot com
  Target Milestone: ---

https://godbolt.org/z/KTTMrEGns

Example code:
    free(new (malloc(4)) int()); // Warns but shouldn't
    delete new (malloc(4)) int(); // Doesn't warn but should

output:

<source>:5:9: warning: 'void free(void*)' called on pointer returned from a
mismatched allocation function [-Wmismatched-new-delete]
    5 |     free(new (malloc(4)) int()); // Warns but shouldn't
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~
<source>:5:30: note: returned from 'void* operator new(std::size_t, void*)'
    5 |     free(new (malloc(4)) int()); // Warns but shouldn't
      |                              ^

While it would be nice to have a warning on the second line, not warning on the
first seems more important. And hopefully is a backportable fix.

Here is some Real World Code exhibiting this pattern that g++ currently warns
about when compiling:
https://github.com/facebook/hermes/blob/dfef1abd6d20b196e24c591e225a7003e6337a94/unittests/VMRuntime/StringPrimitiveTest.cpp#L221-L235.
There is also an example using calloc() lower in that file.

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

* [Bug c++/100876] -Wmismatched-new-delete should either look through or ignore placement new
  2021-06-02 13:46 [Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new redbeard0531 at gmail dot com
@ 2021-06-02 15:19 ` redi at gcc dot gnu.org
  2021-06-02 18:16 ` [Bug middle-end/100876] -Wmismatched-new-delete should understand placement new when it's not inlined msebor at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2021-06-02 15:19 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-06-02
     Ever confirmed|0                           |1
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |NEW

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

* [Bug middle-end/100876] -Wmismatched-new-delete should understand placement new when it's not inlined
  2021-06-02 13:46 [Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new redbeard0531 at gmail dot com
  2021-06-02 15:19 ` [Bug c++/100876] " redi at gcc dot gnu.org
@ 2021-06-02 18:16 ` msebor at gcc dot gnu.org
  2021-06-02 18:16 ` msebor at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-06-02 18:16 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |100406
      Known to fail|                            |11.1.0
            Summary|-Wmismatched-new-delete     |-Wmismatched-new-delete
                   |should either look through  |should understand placement
                   |or ignore placement new     |new when it's not inlined
                 CC|                            |msebor at gcc dot gnu.org
          Component|c++                         |middle-end

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
[Please include all the information we ask for in the bug report, including the
command line options and the full test cases: https://gcc.gnu.org/bugs/#need]

The test case behaves as expected when the placement new is inlined, either
with optimization or when the operator is declared with attribute
always_inline:

  inline __attribute__ ((__always_inline__)) void*
  operator new (__SIZE_TYPE__, void*);

The problem is a general one, not specific to -Wmismatched-new-delete: GCC
understands the semantics of built-in functions, including whether they return
one of their arguments, but not user-defined ones.  Even though it's special,
GCC treats placement new as an ordinary user-defined function.  Because nothing
indicates the operator returns its pointer argument, no logic in GCC can
determine that unless the operator is inlined.

The fix is to either hardcode into GCC the knowledge of placement new, or
provide an attribute for users (and the standard library) to indicate that a
function returns one of its argument.  The former might be suitable for GCC 11
as a bug fix for this warning, the latter is something I'd like to do in GCC 12
regardless.  Let me work on the former and plan on looking into the latter in
the future.


Referenced Bugs:

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

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

* [Bug middle-end/100876] -Wmismatched-new-delete should understand placement new when it's not inlined
  2021-06-02 13:46 [Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new redbeard0531 at gmail dot com
  2021-06-02 15:19 ` [Bug c++/100876] " redi at gcc dot gnu.org
  2021-06-02 18:16 ` [Bug middle-end/100876] -Wmismatched-new-delete should understand placement new when it's not inlined msebor at gcc dot gnu.org
@ 2021-06-02 18:16 ` msebor at gcc dot gnu.org
  2021-06-02 21:42 ` msebor at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-06-02 18:16 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |msebor at gcc dot gnu.org

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

* [Bug middle-end/100876] -Wmismatched-new-delete should understand placement new when it's not inlined
  2021-06-02 13:46 [Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new redbeard0531 at gmail dot com
                   ` (2 preceding siblings ...)
  2021-06-02 18:16 ` msebor at gcc dot gnu.org
@ 2021-06-02 21:42 ` msebor at gcc dot gnu.org
  2021-06-14 22:51 ` cvs-commit at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-06-02 21:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
   Target Milestone|---                         |11.2

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
Patch: https://gcc.gnu.org/pipermail/gcc-patches/2021-June/571777.html

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

* [Bug middle-end/100876] -Wmismatched-new-delete should understand placement new when it's not inlined
  2021-06-02 13:46 [Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new redbeard0531 at gmail dot com
                   ` (3 preceding siblings ...)
  2021-06-02 21:42 ` msebor at gcc dot gnu.org
@ 2021-06-14 22:51 ` cvs-commit at gcc dot gnu.org
  2021-06-14 22:55 ` [Bug middle-end/100876] [11 Regression] " msebor at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-06-14 22:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:d9f1466f88abef7c814d02ba39a6ea5ef420aaec

commit r12-1441-gd9f1466f88abef7c814d02ba39a6ea5ef420aaec
Author: Martin Sebor <msebor@redhat.com>
Date:   Mon Jun 14 16:34:48 2021 -0600

    Teach compute_objsize about placement new [PR100876].

    Resolves:
    PR c++/100876 - -Wmismatched-new-delete should understand placement new
when it's not inlined

    gcc/ChangeLog:

            PR c++/100876
            * builtins.c (gimple_call_return_array): Check for attribute fn
spec.
            Handle calls to placement new.
            (ndecl_dealloc_argno): Avoid placement delete.

    gcc/testsuite/ChangeLog:

            PR c++/100876
            * g++.dg/warn/Wmismatched-new-delete-4.C: New test.
            * g++.dg/warn/Wmismatched-new-delete-5.C: New test.
            * g++.dg/warn/Wstringop-overflow-7.C: New test.
            * g++.dg/warn/Wfree-nonheap-object-6.C: New test.
            * g++.dg/analyzer/placement-new.C: Prune out expected warning.

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

* [Bug middle-end/100876] [11 Regression] -Wmismatched-new-delete should understand placement new when it's not inlined
  2021-06-02 13:46 [Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new redbeard0531 at gmail dot com
                   ` (4 preceding siblings ...)
  2021-06-14 22:51 ` cvs-commit at gcc dot gnu.org
@ 2021-06-14 22:55 ` msebor at gcc dot gnu.org
  2021-06-15 18:47 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-06-14 22:55 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|-Wmismatched-new-delete     |[11 Regression]
                   |should understand placement |-Wmismatched-new-delete
                   |new when it's not inlined   |should understand placement
                   |                            |new when it's not inlined

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
Fixed in GCC 12.  Since this fixes a false positive I think it can be
backported even if it also fixes a false negative.  I'll plan to do it for GCC
11.2 unless there are objections.

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

* [Bug middle-end/100876] [11 Regression] -Wmismatched-new-delete should understand placement new when it's not inlined
  2021-06-02 13:46 [Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new redbeard0531 at gmail dot com
                   ` (5 preceding siblings ...)
  2021-06-14 22:55 ` [Bug middle-end/100876] [11 Regression] " msebor at gcc dot gnu.org
@ 2021-06-15 18:47 ` cvs-commit at gcc dot gnu.org
  2021-06-17 20:08 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-06-15 18:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:71790f398e119c7fed867b0cfce60a7500629dff

commit r12-1490-g71790f398e119c7fed867b0cfce60a7500629dff
Author: Martin Sebor <msebor@redhat.com>
Date:   Tue Jun 15 12:42:06 2021 -0600

    Consider size_t mangling as unsigned int and long [PR100876].

    gcc/ChangeLog:

            PR middle-end/100876
            * builtins.c: (gimple_call_return_array): Account for size_t
            mangling as either unsigned int or unsigned long

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

* [Bug middle-end/100876] [11 Regression] -Wmismatched-new-delete should understand placement new when it's not inlined
  2021-06-02 13:46 [Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new redbeard0531 at gmail dot com
                   ` (6 preceding siblings ...)
  2021-06-15 18:47 ` cvs-commit at gcc dot gnu.org
@ 2021-06-17 20:08 ` cvs-commit at gcc dot gnu.org
  2021-06-17 20:08 ` cvs-commit at gcc dot gnu.org
  2021-06-17 20:11 ` msebor at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-06-17 20:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Martin Sebor
<msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:fbafba7114939b279e860198d009d7641f3ce49c

commit r11-8606-gfbafba7114939b279e860198d009d7641f3ce49c
Author: Martin Sebor <msebor@redhat.com>
Date:   Thu Jun 17 12:18:53 2021 -0600

    Backported from trunk:

    Teach compute_objsize about placement new [PR100876].

    Resolves:
    PR c++/100876 - -Wmismatched-new-delete should understand placement new
when it's not inlined

    gcc/ChangeLog:

            PR c++/100876
            * builtins.c (gimple_call_return_array): Check for attribute fn
spec.
            Handle calls to placement new.
            (ndecl_dealloc_argno): Avoid placement delete.

    gcc/testsuite/ChangeLog:

            PR c++/100876
            * g++.dg/warn/Wmismatched-new-delete-4.C: New test.
            * g++.dg/warn/Wmismatched-new-delete-5.C: New test.
            * g++.dg/warn/Wstringop-overflow-7.C: New test.
            * g++.dg/warn/Wfree-nonheap-object-6.C: New test.
            * g++.dg/analyzer/placement-new.C: Prune out expected warning.

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

* [Bug middle-end/100876] [11 Regression] -Wmismatched-new-delete should understand placement new when it's not inlined
  2021-06-02 13:46 [Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new redbeard0531 at gmail dot com
                   ` (7 preceding siblings ...)
  2021-06-17 20:08 ` cvs-commit at gcc dot gnu.org
@ 2021-06-17 20:08 ` cvs-commit at gcc dot gnu.org
  2021-06-17 20:11 ` msebor at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-06-17 20:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Martin Sebor
<msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:00bf3e4d12944e659b3a3706ca720f1d6346e610

commit r11-8607-g00bf3e4d12944e659b3a3706ca720f1d6346e610
Author: Martin Sebor <msebor@redhat.com>
Date:   Thu Jun 17 12:22:28 2021 -0600

    Backported from trunk:

    Consider size_t mangling as unsigned int and long [PR100876].

    gcc/ChangeLog:

            PR middle-end/100876
            * builtins.c: (gimple_call_return_array): Account for size_t
            mangling as either unsigned int or unsigned long

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

* [Bug middle-end/100876] [11 Regression] -Wmismatched-new-delete should understand placement new when it's not inlined
  2021-06-02 13:46 [Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new redbeard0531 at gmail dot com
                   ` (8 preceding siblings ...)
  2021-06-17 20:08 ` cvs-commit at gcc dot gnu.org
@ 2021-06-17 20:11 ` msebor at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-06-17 20:11 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #8 from Martin Sebor <msebor at gcc dot gnu.org> ---
Backported to GCC 11.2.

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

end of thread, other threads:[~2021-06-17 20:11 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-02 13:46 [Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new redbeard0531 at gmail dot com
2021-06-02 15:19 ` [Bug c++/100876] " redi at gcc dot gnu.org
2021-06-02 18:16 ` [Bug middle-end/100876] -Wmismatched-new-delete should understand placement new when it's not inlined msebor at gcc dot gnu.org
2021-06-02 18:16 ` msebor at gcc dot gnu.org
2021-06-02 21:42 ` msebor at gcc dot gnu.org
2021-06-14 22:51 ` cvs-commit at gcc dot gnu.org
2021-06-14 22:55 ` [Bug middle-end/100876] [11 Regression] " msebor at gcc dot gnu.org
2021-06-15 18:47 ` cvs-commit at gcc dot gnu.org
2021-06-17 20:08 ` cvs-commit at gcc dot gnu.org
2021-06-17 20:08 ` cvs-commit at gcc dot gnu.org
2021-06-17 20:11 ` 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).