[Bug c++/85783] alloc-size-larger-than fires incorrectly with new[] and can't be disabled

psmith at gnu dot org gcc-bugzilla@gcc.gnu.org
Tue May 15 12:50:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85783

--- Comment #5 from Paul Smith <psmith at gnu dot org> ---
I simplified my example too much; I think this should be re-opened.

In my real code, operator new[] does not invoke exit(); it invokes my own
function (which is defined as noreturn, but that's not required).  There's no
way for the compiler to know whether this function will throw or not, so
"replacing a non-throwing operator new[]" isn't why my case is not working. 
Also, you mention an inline implementation of operator new[], but there's
nothing in your code example that I can see that forces my replacement to be
inline.  Is there some magic about global operator new replacement that I'm
forgetting?

Here's an example which still fails with the warning and which I think is valid
C++ (interestingly this version requires -O2 to show the problem):

void allocFail(__SIZE_TYPE__ _s);

void* operator new[](__SIZE_TYPE__ n)
{
  void* p = __builtin_malloc (n);
  if (!p)  allocFail (n);
  return p;
}

struct A
{
  A ();
  ~A ();
};


void* f (__SIZE_TYPE__ n)
{
  if (!n)
    return 0;

  return new A[n];
}

In function 'void* operator new [](long unsigned int)',
    inlined from 'void* f(long unsigned int)' at p1.cpp:22:17:
p1.cpp:5:30: warning: argument 1 value '18446744073709551615' exceeds maximum
object size 9223372036854775807 [-Walloc-size-larger-than=]
   void* p = __builtin_malloc (n);
             ~~~~~~~~~~~~~~~~~^~~
p1.cpp: In function 'void* f(long unsigned int)':
p1.cpp:5:30: note: in a call to built-in allocation function 'void*
__builtin_malloc(long unsigned int)'


More information about the Gcc-bugs mailing list