public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105077] New: The std::bad_array_new_length is not thrown for some new array scenarios.
@ 2022-03-28  9:12 chumarshal at foxmail dot com
  2022-03-28  9:17 ` [Bug c++/105077] " redi at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: chumarshal at foxmail dot com @ 2022-03-28  9:12 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-03-30  8:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-28  9:12 [Bug c++/105077] New: The std::bad_array_new_length is not thrown for some new array scenarios chumarshal at foxmail dot com
2022-03-28  9:17 ` [Bug c++/105077] " redi at gcc dot gnu.org
2022-03-28  9:21 ` redi at gcc dot gnu.org
2022-03-28  9:47 ` chumarshal at foxmail dot com
2022-03-28 10:34 ` redi at gcc dot gnu.org
2022-03-30  1:52 ` chumarshal at foxmail dot com
2022-03-30  8:59 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).