CERT.MEM.OVERRIDE.NEW

Honor replacement dynamic storage management requirements for 'new'.

Vulnerability and risk

Dynamic memory allocation and deallocation functions can be globally replaced by custom implementations. A replacement for any of the dynamic memory allocation or deallocation functions must meet the semantic requirements specified by the appropriate required behavior clause of the replaced function.

This checker looks for replacement functions for "new" and checks that they are defined with noexcept or that there is a throw std::bad_alloc in the function body.

Mitigation and prevention

Replacement "new" functions should throw std::bad_alloc on allocation failures or be defined with noexcept.

Example

Copy
  void *operator new(std::size_t size) {  
    extern void *alloc_mem(std::size_t); // Implemented elsewhere; may return nullptr
    return alloc_mem(size);
  }
  
  void *operator new[](std::size_t size) { 
    extern void *alloc_mem(std::size_t); // Implemented elsewhere; may return nullptr
    return alloc_mem(size);
  }

Violations will be reported on lines 1 and 6.