...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <cstddef> #include <new> struct S { S (); ~S (); }; void f() { const unsigned N = 32; alignas(S) unsigned char buffer[sizeof(S) * N]; S *sp = ::new (buffer) S[N]; // ... // Destroy elements of the array. for (size_t i = 0; i != nN; ++i) { sp[i].~S(); } } |
Compliant Solution (Clang/GCC)
...
Passing improperly aligned pointers or pointers to insufficient storage to placement new expressions can result in undefined behavior, including buffer overflow and abnormal termination.
Rule | Severity | Likelihood | Detectable | Remediation CostRepairable | Priority | Level |
|---|---|---|---|---|---|---|
MEM54-CPP | High | Likely | No | MediumNo | P18P9 | L1 L2 |
Automated Detection
Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| Axivion Bauhaus Suite |
| CertC++-MEM54 | |||||||
| CodeSonar |
| LANG.MEM.BO | Buffer Overrun | ||||||
| Helix QAC |
| C++3119, C++3128, DF3520, DF3521, DF3522, DF3523 | |||||||
| Klocwork |
| CERT.MEM.PLACEMENTNEW.MISALIGNED | |||||||
| LDRA tool suite |
| 597 S | Enhanced Enforcement | ||||||
| Parasoft C/C++test |
| CERT_CPP-MEM54-a | Do not pass a pointer that has insufficient storage capacity or that is not suitably aligned for the object being constructed to placement 'new' | ||||||
| Polyspace Bug Finder |
| CERT C++: MEM54-CPP | Checks for placement new used with insufficient storage or misaligned pointers (rule fully covered) | ||||||
| PVS-Studio |
| V752 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...