
...
Do not call a deallocation function on anything other than nullptr
, or a pointer returned by the corresponding allocation function described by the following.
Allocator | Deallocator |
---|---|
global operator new()/new | global operator delete ()/delete |
global operator new[]()/new[] | global operator delete[]()/delete[] |
class-specific operator new()/new | class-specific operator delete ()/delete |
class-specific operator new[]()/new[] | class-specific operator delete[]()/delete[] |
placement operator new () | N/A |
allocator<T>::allocate() |
|
std::malloc() , std::calloc() , std::realloc() | std::free() |
std::get_temporary_buffer() | std::return_temporary_buffer() |
Page properties | ||
---|---|---|
| ||
While the wording for |
...
Passing a pointer value to a deallocation function that was not previously obtained by the matching allocation function results in undefined behavior, which can lead to exploitable vulnerabilities.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
MEM51-CPP | High | Likely | Medium | P18 | L1 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Clang |
| clang-analyzer-cplusplus.NewDeleteLeaks -Wmismatched-new-delete | Checked by clang-tidy , but does not catch all violations of this rule | ||||||
CodeSonar |
| ALLOC.FNH | Free non-heap variable Type mismatch | ||||||
LDRA tool suite |
| 232 S, 236 S, 239 S, 407 S, 469 S, 470 S, 483 S, 484 S, 485 S, 64 D, 112 D | Partially implemented | ||||||
Parasoft C/C++test |
|
|
| MEM-06, MEM-12, MEM-28, MEM-29 |
Parasoft Insure++ |
Runtime detection | |||
PRQA QA-C++ | 4.1 | 2110, 2111, 2112, 2113, 2118, 4262, 4263, 4264, 3337, 3339 |
SonarQube C/C++ Plugin |
| S1232 |
PVS-Studio | 6.22 | V515, V554, V611, V701, V748, V773 | General analysis rule set |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
SEI CERT C++ Coding Standard | MEM53-CPP. Explicitly construct and destruct objects when manually managing object lifetime |
SEI CERT C Coding Standard | MEM31-C. Free dynamically allocated memory when no longer needed |
MITRE CWE | CWE 590, Free of Memory Not on the Heap |
Bibliography
[Dowd 2007] | "Attacking delete and delete [] in C++" | ||
[Henricson 1997] | Rule 8.1, "delete should only be used with new" Rule 8.2, " delete [] should only be used with new []" | ||
[ISO/IEC 14882-2014] | Subclause 5.3.5, "Delete" | ||
[Meyers 2005] | Item 16, "Use the Same Form in Corresponding Uses of new and delete " | ||
[Seacord 2013] | Chapter 4, "Dynamic Memory Management" | ||
[Viega 2005] | "Doubly Freeing Memory" |
...
...