-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert-MEM57.cpp
46 lines (38 loc) · 973 Bytes
/
cert-MEM57.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// MEM57-CPP. Avoid using default operator new for over-aligned types
//
#include <cstdlib>
#include <cstring>
#include <new>
#undef USE_COMPIENT_SOLUTION
namespace {
// In particular, it is unsafe to use the storage for an object of a type with a stricter alignment
// requirement—an over-aligned type.
struct alignas(32) Vector
{
char elems[32];
#ifdef USE_COMPIENT_SOLUTION
static void* operator new(size_t nbytes)
{
if (void* p = std::aligned_alloc(alignof(Vector), nbytes)) {
return p;
}
throw std::bad_alloc();
}
static void operator delete(void* p) { free(p); }
// warning: do not manage memory manually; use RAII [cppcoreguidelines-no-malloc,hicpp-no-malloc]
#endif
};
Vector* createVector()
{
auto* pv = new Vector; // undefined behavior
memset(pv->elems, 0, sizeof(pv->elems));
return pv;
}
} // namespace
int main()
{
auto* pv = createVector();
// ...
delete pv;
}