public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/90629] Support for -Wmismatched-new-delete
       [not found] <bug-90629-4@http.gcc.gnu.org/bugzilla/>
@ 2020-04-13 20:15 ` msebor at gcc dot gnu.org
  2020-08-27  7:51 ` dcb314 at hotmail dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-04-13 20:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org
     Ever confirmed|0                           |1
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=94527
   Last reconfirmed|                            |2020-04-13
             Status|UNCONFIRMED                 |ASSIGNED

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
Confirmed.  I'm working on general support for this sort of detection (see also
pr94527).

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

* [Bug c++/90629] Support for -Wmismatched-new-delete
       [not found] <bug-90629-4@http.gcc.gnu.org/bugzilla/>
  2020-04-13 20:15 ` [Bug c++/90629] Support for -Wmismatched-new-delete msebor at gcc dot gnu.org
@ 2020-08-27  7:51 ` dcb314 at hotmail dot com
  2020-10-20 15:30 ` msebor at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 6+ messages in thread
From: dcb314 at hotmail dot com @ 2020-08-27  7:51 UTC (permalink / raw)
  To: gcc-bugs

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

David Binderman <dcb314 at hotmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dcb314 at hotmail dot com

--- Comment #2 from David Binderman <dcb314 at hotmail dot com> ---

Here is a little test case:

// bad alloc & dealloc

# include <stdlib.h>

void
f()
{
        char * p = new char [ 10];
        delete p;

        char * p2 = new char;
        delete [] p2;

        char * q = new char [ 10];
        free( q);

        char * q2 = (char *) malloc( 10);
        delete [] q2;
}

Latest gcc finds no problems, latest clang finds two, for p and p2.
q and q2 not found.

Interestingly, I've just started compiling all of fedora distribution
with clang. I've only compiled [a-d]*.spec files so far, but I have 34 cases
found already. This implies about 220 cases in fedora.

So only duplicating what does clang for new and delete would find hundreds of
bugs in fedora. I'd be glad to help with testing any prototype implementation.

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

* [Bug c++/90629] Support for -Wmismatched-new-delete
       [not found] <bug-90629-4@http.gcc.gnu.org/bugzilla/>
  2020-04-13 20:15 ` [Bug c++/90629] Support for -Wmismatched-new-delete msebor at gcc dot gnu.org
  2020-08-27  7:51 ` dcb314 at hotmail dot com
@ 2020-10-20 15:30 ` msebor at gcc dot gnu.org
  2020-11-03 23:58 ` msebor at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-10-20 15:30 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
With the patch I'm testing GCC reports the following for the test case in
comment #2:

90629-c2.C: In function ‘void f()’:
90629-c2.C:8:16: warning: ‘void operator delete(void*, long unsigned int)’
called on pointer returned from a mismatched allocation function
[-Wmismatched-new-delete]
    8 |         delete p;
      |                ^
90629-c2.C:7:33: note: returned from a call to ‘void* operator new [](long
unsigned int)’
    7 |         char * p = new char [ 10];
      |                                 ^
90629-c2.C:11:19: warning: ‘void operator delete [](void*)’ called on pointer
returned from a mismatched allocation function [-Wmismatched-new-delete]
   11 |         delete [] p2;
      |                   ^~
90629-c2.C:10:25: note: returned from a call to ‘void* operator new(long
unsigned int)’
   10 |         char * p2 = new char;
      |                         ^~~~
90629-c2.C:17:19: warning: ‘void operator delete [](void*)’ called on pointer
returned from a mismatched allocation function [-Wmismatched-new-delete]
   17 |         delete [] q2;
      |                   ^~
90629-c2.C:16:36: note: returned from a call to ‘void* malloc(size_t)’
   16 |         char * q2 = (char *) malloc( 10);
      |                              ~~~~~~^~~~~


The call to free(q) isn't diagnosed in the simple test case because it's
eliminated by the cddce pass along with the new expression.

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

* [Bug c++/90629] Support for -Wmismatched-new-delete
       [not found] <bug-90629-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2020-10-20 15:30 ` msebor at gcc dot gnu.org
@ 2020-11-03 23:58 ` msebor at gcc dot gnu.org
  2020-12-03 22:43 ` cvs-commit at gcc dot gnu.org
  2020-12-03 22:44 ` msebor at gcc dot gnu.org
  5 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-11-03 23:58 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
Patch: https://gcc.gnu.org/pipermail/gcc-patches/2020-November/557987.html

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

* [Bug c++/90629] Support for -Wmismatched-new-delete
       [not found] <bug-90629-4@http.gcc.gnu.org/bugzilla/>
                   ` (3 preceding siblings ...)
  2020-11-03 23:58 ` msebor at gcc dot gnu.org
@ 2020-12-03 22:43 ` cvs-commit at gcc dot gnu.org
  2020-12-03 22:44 ` msebor at gcc dot gnu.org
  5 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-12-03 22:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- 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:dce6c58db87ebf7f4477bd3126228e73e4eeee97

commit r11-5732-gdce6c58db87ebf7f4477bd3126228e73e4eeee97
Author: Martin Sebor <msebor@redhat.com>
Date:   Thu Dec 3 15:41:25 2020 -0700

    Add support for detecting mismatched allocation/deallocation calls.

    PR c++/90629 - Support for -Wmismatched-new-delete
    PR middle-end/94527 - Add an __attribute__ that marks a function as freeing
an object

    gcc/ChangeLog:

            PR c++/90629
            PR middle-end/94527
            * builtins.c (access_ref::access_ref): Initialize new member.
            (compute_objsize): Use access_ref::deref.  Handle simple pointer
            assignment.
            (expand_builtin): Remove handling of the free built-in.
            (call_dealloc_argno): Same.
            (find_assignment_location): New function.
            (fndecl_alloc_p): Same.
            (gimple_call_alloc_p): Same.
            (call_dealloc_p): Same.
            (matching_alloc_calls_p): Same.
            (warn_dealloc_offset): Same.
            (maybe_emit_free_warning): Same.
            * builtins.h (struct access_ref): Declare new member.
            (maybe_emit_free_warning): Make extern.  Make use of access_ref.
            Handle -Wmismatched-new-delete.
            * calls.c (initialize_argument_information): Call
            maybe_emit_free_warning.
            * doc/extend.texi (attribute malloc): Update.
            * doc/invoke.texi (-Wfree-nonheap-object): Expand documentation.
            (-Wmismatched-new-delete): Document new option.
            (-Wmismatched-dealloc): Document new option.

    gcc/c-family/ChangeLog:

            PR c++/90629
            PR middle-end/94527
            * c-attribs.c (handle_dealloc_attribute): New function.
            (handle_malloc_attribute): Handle argument forms of attribute.
            * c.opt (-Wmismatched-dealloc): New option.
            (-Wmismatched-new-delete): New option.

    gcc/testsuite/ChangeLog:

            PR c++/90629
            PR middle-end/94527
            * g++.dg/asan/asan_test.cc: Fix a bug.
            * g++.dg/warn/delete-array-1.C: Add expected warning.
            * g++.old-deja/g++.other/delete2.C: Add expected warning.
            * g++.dg/warn/Wfree-nonheap-object-2.C: New test.
            * g++.dg/warn/Wfree-nonheap-object.C: New test.
            * g++.dg/warn/Wmismatched-new-delete.C: New test.
            * g++.dg/warn/Wmismatched-dealloc-2.C: New test.
            * g++.dg/warn/Wmismatched-dealloc.C: New test.
            * gcc.dg/Wmismatched-dealloc.c: New test.
            * gcc.dg/analyzer/malloc-1.c: Prune out expected warning.
            * gcc.dg/attr-malloc.c: New test.
            * gcc.dg/free-1.c: Adjust text of expected warning.
            * gcc.dg/free-2.c: Same.
            * gcc.dg/torture/pr71816.c: Prune out expected warning.
            * gcc.dg/tree-ssa/pr19831-2.c: Add an expected warning.
            * gcc.dg/Wfree-nonheap-object-2.c: New test.
            * gcc.dg/Wfree-nonheap-object-3.c: New test.
            * gcc.dg/Wfree-nonheap-object.c: New test.

    libstdc++-v3/ChangeLog:

            * testsuite/ext/vstring/modifiers/clear/56166.cc: Suppress a false
            positive warning.

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

* [Bug c++/90629] Support for -Wmismatched-new-delete
       [not found] <bug-90629-4@http.gcc.gnu.org/bugzilla/>
                   ` (4 preceding siblings ...)
  2020-12-03 22:43 ` cvs-commit at gcc dot gnu.org
@ 2020-12-03 22:44 ` msebor at gcc dot gnu.org
  5 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-12-03 22:44 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED
   Target Milestone|---                         |11.0

--- Comment #6 from Martin Sebor <msebor at gcc dot gnu.org> ---
Done for GCC 11.

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

end of thread, other threads:[~2020-12-03 22:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-90629-4@http.gcc.gnu.org/bugzilla/>
2020-04-13 20:15 ` [Bug c++/90629] Support for -Wmismatched-new-delete msebor at gcc dot gnu.org
2020-08-27  7:51 ` dcb314 at hotmail dot com
2020-10-20 15:30 ` msebor at gcc dot gnu.org
2020-11-03 23:58 ` msebor at gcc dot gnu.org
2020-12-03 22:43 ` cvs-commit at gcc dot gnu.org
2020-12-03 22:44 ` 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).