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

            Bug ID: 105077
           Summary: The std::bad_array_new_length is not thrown for some
                    new array scenarios.
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chumarshal at foxmail dot com
  Target Milestone: ---

The std::bad_array_new_length is not thrown for the following three scenarios.
These scenarios do not follow C++ spec:"std::bad_array_new_length is the type
of the object thrown as exceptions by the new-expressions to report invalid
array lengths if array length is negative."


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

terminate called after throwing an instance of 'std::bad_alloc'
what():  std::bad_alloc


case2   using g++ -O0
==========================================================================
#include <new>
#include <stdexcept>

struct s_char
{ 
  char ch;
};

int main()  
{
  try 
  {
        int negative = -1;
        new s_char[negative];
  } 
  catch(const std::bad_array_new_length &e) 
  {
  }
  return 0;
}

terminate called after throwing an instance of 'std::bad_alloc'
what():  std::bad_alloc


case3  using g++ -O0
==========================================================================
#include <new>
#include <stdexcept>

struct s_char_with_new 
{
  char ch;
  void *operator new[] (std::size_t sz)
  {
    abort();
  }
};

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

terminate called after throwing an instance of 'std::bad_alloc'
what():  std::bad_alloc

Reply via email to