Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Failure to overload the corresponding dynamic storage function is likely to violate rules like VOID MEM39MEM31-CPP. Resources allocated by memory allocation functions must be released using the corresponding memory deallocation functionProperly deallocate dynamically allocated resources. For instance, if an overloaded allocation function uses a private heap to perform its allocations, passing a pointer returned by it to the default deallocation function will likely cause undefined behavior. Even in situations where the allocation function ultimately calls through to the default allocator to obtain a pointer to memory, failing to overload a corresponding deallocation function may leave the program in an unexpected state by not updating internal allocator state.

...

In this noncompliant code example, an allocation function is overloaded at global scope, however, the corresponding deallocation function is not declared. Were an object to be allocated with the overloaded allocation function, any attempt to delete the object would result in undefined behavior in violation of VOID MEM39MEM31-CPP. Resources allocated by memory allocation functions must be released using the corresponding memory deallocation functionProperly deallocate dynamically allocated resources.

Code Block
bgColor#FFcccc
langcpp
#include <Windows.h>
#include <new>
 
void *operator new(std::size_t size) noexcept(false) {
  static HANDLE H = ::HeapCreate(0, 0, 0); // Private, expandable heap
  if (H) {
    return ::HeapAlloc(H, 0, size);
  }
  throw std::bad_alloc();
}

...

Tool

Version

Checker

Description

PRQA QA-C++
Include Page
PRQA QA-C++_vV
PRQA QA-C++_vV
2160,2161 

Related Vulnerabilities

...

CERT C++ Secure Coding StandardVOID MEM39MEM31-CPP. Resources allocated by memory allocation functions must be released using the corresponding memory deallocation functionProperly deallocate dynamically allocated resources

Bibliography

[ISO/IEC 14882-2014]

3.7.4, "Dynamic Storage Duration"
5.3.4, "New"
5.3.5, "Delete" 

...