Add support for detecting mismatched allocation/deallocation calls.
authorMartin Sebor <msebor@redhat.com>
Thu, 3 Dec 2020 22:41:25 +0000 (15:41 -0700)
committerMartin Sebor <msebor@redhat.com>
Thu, 3 Dec 2020 22:43:32 +0000 (15:43 -0700)
commitdce6c58db87ebf7f4477bd3126228e73e4eeee97
treef89f18c53c2f16c2d39a1951c21f99cdd4773c99
parenta3f7a6957a674caf95c4aefa618be51092022e87
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.
27 files changed:
gcc/builtins.c
gcc/builtins.h
gcc/c-family/c-attribs.c
gcc/c-family/c.opt
gcc/calls.c
gcc/doc/extend.texi
gcc/doc/invoke.texi
gcc/testsuite/g++.dg/asan/asan_test.cc
gcc/testsuite/g++.dg/warn/Wfree-nonheap-object-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wfree-nonheap-object.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wfree-nonheap-object.s [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wmismatched-dealloc-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wmismatched-dealloc.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wmismatched-new-delete.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/delete-array-1.C
gcc/testsuite/g++.old-deja/g++.other/delete2.C
gcc/testsuite/gcc.dg/Wfree-nonheap-object-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wfree-nonheap-object-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wfree-nonheap-object.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wmismatched-dealloc.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/analyzer/malloc-1.c
gcc/testsuite/gcc.dg/attr-malloc.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/free-1.c
gcc/testsuite/gcc.dg/free-2.c
gcc/testsuite/gcc.dg/torture/pr71816.c
gcc/testsuite/gcc.dg/tree-ssa/pr19831-2.c
libstdc++-v3/testsuite/ext/vstring/modifiers/clear/56166.cc