On 4/28/2021 6:51 PM, Martin Sebor via Gcc-patches wrote:
When the compute_objsize_r() function sees a pointer whose target
it can't determine it sets the size of the pointed to object to
the maximum but it doesn't clear the base0 flag to indicate that
the offset need not be zero-based.  This is done when the source
is in SSA form but not before.  Since the function is now also
called from the C++ front end to detect out-of-bounds placement
new (-Wplacement-new) this causes false positives there, as
reported in the PR.  The same problem also affects the "early"
-Wformat-overflow (when it runs without optimization).

Clearing the base0 flag as done in the attached patch avoids
the false positives in both warnings.

Besides avoiding false positives the change also makes some valid
(though incidental) warnings disappear.  Running -Wplacement-new
in the front end, and -Wformat-overflow before small IPA passes,
is too early; the warnings need to run before placement new is
inlined but after the program has been converted to SSA.  Since
it will introduce additional warnings I will submit separate
patches for that just for GCC 12.

I plan to commit the attached patch into GCC 12 and 11.1.

Martin

gcc-100307.diff

PR middle-end/100307 - spurious -Wplacement-new with negative pointer offset

gcc/ChangeLog:

        PR middle-end/100307
        * builtins.c (compute_objsize_r): Clear base0 for pointers.

gcc/testsuite/ChangeLog:

        PR middle-end/100307
        * g++.dg/warn/Wplacement-new-size-9.C: New test.
        * gcc.dg/tree-ssa/builtin-sprintf-warn-26.c: New test.

OK

jeff

Reply via email to