On Sat, Dec 13, 2025 at 11:27:34AM +0100, Richard Biener wrote:
> IMO the fix is to simply drop the lang_GNU_CXX () check.
> DECL_IS_OPERATOR_NEW_P is good enough.
> 
> OK with that change.
> 
> Richard.

Thanks! Pushed the below patch.

-Lewis
The implementation of -Walloc-size-larger-than has logic to avoid issuing
the warning for ::operator new[] calls emitted by the C++ front end, which
otherwise produce known false positives.  The logic for suppressing the
warning only activates in the C++ front end, and so it does not prevent the
LTO front end from issuing the warning.  Fix by applying the logic in all
cases.

gcc/ChangeLog:

        PR tree-optimization/106409
        * gimple-ssa-warn-access.cc (maybe_warn_alloc_args_overflow): Adjust
        comment for clarity, and augment check to work in LTO as well.

gcc/testsuite/ChangeLog:

        PR tree-optimization/106409
        * g++.dg/lto/pr106409_0.C: New test.

diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc
index d12f797f36b..0625a36ca35 100644
--- a/gcc/gimple-ssa-warn-access.cc
+++ b/gcc/gimple-ssa-warn-access.cc
@@ -2334,14 +2334,14 @@ maybe_warn_alloc_args_overflow (gimple *stmt, const 
tree args[2],
            }
          else if (tree_int_cst_lt (maxobjsize, args[i]))
            {
-             /* G++ emits calls to ::operator new[](SIZE_MAX) in C++98
-                mode and with -fno-exceptions as a way to indicate array
-                size overflow.  There's no good way to detect C++98 here
-                so avoid diagnosing these calls for all C++ modes.  */
+             /* G++ emits calls to ::operator new[](SIZE_MAX) in C++98 mode or
+                with -fno-exceptions as a way to indicate array size overflow.
+                Avoid diagnosing these calls.  Additionally, see e.g. PR99934,
+                G++ also potentially generates such calls in C++11 and later as
+                well, so suppress the diagnostic in all C++ modes.  */
              if (i == 0
                  && fn
                  && !args[1]
-                 && lang_GNU_CXX ()
                  && DECL_IS_OPERATOR_NEW_P (fn)
                  && integer_all_onesp (args[i]))
                continue;
diff --git a/gcc/testsuite/g++.dg/lto/pr106409_0.C 
b/gcc/testsuite/g++.dg/lto/pr106409_0.C
new file mode 100644
index 00000000000..464d13a7947
--- /dev/null
+++ b/gcc/testsuite/g++.dg/lto/pr106409_0.C
@@ -0,0 +1,23 @@
+/* PR tree-optimization/106409 */
+/* { dg-lto-do link } */
+/* { dg-lto-options { { -flto -W -Wall -O2 -fno-exceptions } { -flto -W -Wall 
-O2 -std=c++98 } { -flto -W -Wall -O2 -std=gnu++20 } } } */
+struct bb
+{
+  int t;
+  int t1;
+  int t2;
+  int t3;
+};
+
+[[gnu::noipa]]
+void *f(unsigned long paramCount)
+{
+    if (paramCount == 0)
+      return 0;
+    return new bb[paramCount]();
+}
+
+int main(void)
+{
+  f(100);
+}

Reply via email to