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

            Bug ID: 102514
           Summary: The allocation function shall not be called when
                    existing an erroneous expression in
                    noptr-new-declarator
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xmh970252187 at gmail dot com
  Target Milestone: ---

````
struct C{
    void* operator new[](std::size_t N) noexcept(false){  // #1
        std::cout<<"abc\n";
        auto p =  malloc(10);
        return p;
    }
};
int n = -1;
auto ptr = new C[n];
std::cout<< ptr;
delete [] ptr;
````

GCC prints that `#1` is called and the address is non-null. According to
[expr.new] p9, the function at `#1` shall not be called, and since the function
has potential throwing specification, the new-expression should terminate by
throwing an exception.  

Example 2: 
````cpp
struct C2{
    void* operator new[](std::size_t N) noexcept{  // #2
        std::cout<<"abc\n";
        auto p =  malloc(10);
        return p;
    }
};
int n = -1;
auto ptr = new C2[n];
std::cout<< ptr;
delete [] ptr;
````
GCC calls `#2`, and the address is non-null.

Reply via email to