[PATCH] correct -Wmismatched-new-delete (PR 98160, 98166)

Martin Sebor msebor@gmail.com
Tue Dec 8 20:46:32 GMT 2020


PR 98160 reports an ICE in pretty printer code called from the newly
added -Wmismatched-new-delete.  The ICE is just a simple oversight,
but much more interesting is the warning issued for the test case.
It highlights an issue I didn't consider in the initial implementation:
that inlining one of a pair of allocation/deallocation functions but
not the other might lead to false positives when the inlined function
calls another allocator that the deallocator isn't associated with.

In addition, tests for the changes exposed the overly simplistic
nature of the detection of calls to mismatched forms of operator
new and delete which fails to consider member operators, also
resulting in false positives.

Finally, in a comment on the initial implementation Jonathan notes
that the -Wmismatched-new-delete warning should trigger not only
in user code but also in libstdc++ functions inlined into user code.
I thought I had done that but as it turns out, the "standard code
sequence" I put in place isn't sufficient to make this work.

The attached changes avoid the false positives a) by ignoring (with
a warning) the new form of the malloc attribute on inline functions,
and disabling the inlining of others by implicitly adding attribute
noinline to their declaration, and b) by making more robust
the detection of mismatched operators new and delete.  Furthermore,
the patch also arranges for the warning to trigger even for inlined
calls to functions defined in system headers.

To make review a little (marginally) easier the change are two files:
1) gcc-98166-1.diff: introduces valid_new_delete_pair_p and
tree_inlined_location.
2) gcc-98166-2.diff: adjusts the atrribute/warning implementation .

Tested on x86_64-linux.

Martin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gcc-98166-1.diff
Type: text/x-patch
Size: 8265 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20201208/5828f92b/attachment-0002.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gcc-98166-2.diff
Type: text/x-patch
Size: 52702 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20201208/5828f92b/attachment-0003.bin>


More information about the Gcc-patches mailing list