[Bug c++/85795] bad_alloc thrown from allocation function when bad_array_length is expected

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Mar 28 09:21:26 GMT 2022


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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Erroneous expressions include negative ones, as in PR 105077:

#include <new>
#include <stdexcept>
int main()  
{
  try 
  {
    int negative = -1;
    new char[negative];
  } 
  catch(const std::bad_array_new_length &e) 
  {
  }
  return 0;
}

This throws bad_alloc instead of bad_array_new_length.

PR 99934 is another example where we should not call the allocation function,
but the FE should return a nullptr instead of throwing.


More information about the Gcc-bugs mailing list