]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
middle-end: Fix spurious -Walloc-size-larger-than warning during LTO [PR106409]
authorLewis Hyatt <lhyatt@gmail.com>
Sun, 14 Dec 2025 14:51:12 +0000 (09:51 -0500)
committerLewis Hyatt <lhyatt@gcc.gnu.org>
Sun, 14 Dec 2025 19:28:14 +0000 (14:28 -0500)
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.

gcc/gimple-ssa-warn-access.cc
gcc/testsuite/g++.dg/lto/pr106409_0.C [new file with mode: 0644]

index d12f797f36b3eafb0c978fa16016438aeb288254..0625a36ca35a2f318a51356a056b4ff3bf653520 100644 (file)
@@ -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 (file)
index 0000000..464d13a
--- /dev/null
@@ -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);
+}