From 0b76d798acb30c188102e3ce4d68178b5c5d6239 Mon Sep 17 00:00:00 2001 From: Kelsey Gilbert Date: Wed, 26 Feb 2025 12:18:20 +0000 Subject: [PATCH] Bug 1946733 - Ignore -Walloc-size-larger-than= in `operator new[](nothrow)`. r=firefox-build-system-reviewers,sergesanspaille,sylvestre Unfortunately, GCC-14 appears to be generating this warning due to its own frontend/codegen [interactions?]. This is https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85783, which was WONTFIX'd. Comment #3 admits: > The excessive constant argument is introduced by the C++ front-end, in > cp/build_operator_new_call(), which emits a call to operator new[](size_t). > The code was added in r190546 as a solution to prevent unsigned wrapping > (when array new expression must compute the amount of space to allocate as a > product of the number of elements and element size). When the replacement > operator new[] is inlined the excessive argument is propagated to malloc() > and ultimately triggers the warning. Differential Revision: https://phabricator.services.mozilla.com/D239424 --- memory/mozalloc/cxxalloc.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/memory/mozalloc/cxxalloc.h b/memory/mozalloc/cxxalloc.h index c6fb4bb1dcf0..9b65c2d1f3f8 100644 --- a/memory/mozalloc/cxxalloc.h +++ b/memory/mozalloc/cxxalloc.h @@ -42,9 +42,22 @@ MOZALLOC_EXPORT_NEW void* operator new[](size_t size) noexcept(false) { return moz_xmalloc(size); } +// Inlining `new` like this is technically against C++ spec, but we crave perf. MOZALLOC_EXPORT_NEW void* operator new[](size_t size, const std::nothrow_t&) noexcept(true) { +#ifdef __GNUC__ +// GCC-14 codegen at -O2 causes false positive due to converting +// `new A[n]` to `malloc(-1)` when `n > PTRDIFF_MAX/sizeof(A)`. +// (See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85783, WONTFIX'd) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Walloc-size-larger-than=" +#endif + return malloc_impl(size); + +#ifdef __GNUC__ +# pragma GCC diagnostic pop +#endif } MOZALLOC_EXPORT_NEW void operator delete(void* ptr) noexcept(true) {