-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA15-0-7.cpp
More file actions
62 lines (44 loc) · 1.35 KB
/
A15-0-7.cpp
File metadata and controls
62 lines (44 loc) · 1.35 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Rule: A15-0-7
// Source line: 23104
// Original file: A15-0-7.cpp
//% $Id: A15-0-7.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdlib>
#include <cstring>
struct CxaException
{
// Exception’s structure implementation
};
extern "C" void FatalError(const char* msg)
{
// Reports an error and terminates the program
}
extern "C" void* CxaAllocateExceptionDynamically(size_t thrownSize)
{
size_t size = thrownSize + sizeof(CxaException);
CxaException* buffer = static_cast<CxaException*>(
malloc(size)); // Non-compliant - dynamic memory allocation used
if (!buffer)
{
FatalError("Not enough memory to allocate exception!");
}
memset(buffer, 0, sizeof(CxaException));
return buffer + 1;
}
extern "C" void* StaticMalloc(size_t size)
{
void* mem = NULL;
// Allocates memory using static memory pool
return mem;
}
extern "C" void* CxaAllocateExceptionStatically(size_t thrownSize)
{
size_t size = thrownSize + sizeof(CxaException);
CxaException* buffer = static_cast<CxaException*>(StaticMalloc(
size)); // Compliant - memory allocation on static memory pool used
if (!buffer)
{
FatalError("Not enough memory to allocate exception!");
}
memset(buffer, 0, sizeof(CxaException));
return buffer + 1;
}