-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert-MEM50.cpp
109 lines (94 loc) · 3.03 KB
/
cert-MEM50.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <cstring>
#include <iostream>
#include <memory>
#include <new>
#include <string>
namespace {
std::string str_func() { return std::string("test text"); }
void display_string(const char* s) { std::cout << s << std::endl; }
void undefined()
{
const char* str = str_func().c_str(); // diagnostic required
display_string(str); /* Undefined behavior */
}
struct S
{
int i{};
int f() { return ++i; }
};
void foo() noexcept(false)
{
S* s = new S;
// ...
delete s;
// ...
auto i = s->f(); // diagnostic required
std::cout << i << std::endl;
}
void bar()
{
int* array = new int[10]{};
// ...
delete[] array;
auto i = (*array)++; // diagnostic required
std::cout << i << std::endl;
}
void tests()
{
foo();
bar();
undefined();
}
void bad() noexcept(false)
{
char* ptr = static_cast<char*>(::operator new(0));
*ptr = 0;
// ...
::operator delete(ptr); /* Undefined behavior */
display_string(ptr); /* Undefined behavior */
}
} // namespace
int main(int argc, const char* argv[])
{
bad();
tests();
const char* s = "";
if (argc > 1) {
enum
{
BufferSize = 32
};
try {
std::unique_ptr<char[]> buff(new char[BufferSize]);
std::memset(buff.get(), 0, BufferSize);
// ...
s = std::strncpy(buff.get(), argv[1], BufferSize - 1);
} catch (std::bad_alloc&) {
// Handle error
}
}
std::cout << s << std::endl; // diagnostic required
}
/*
* https://wiki.sei.cmu.edu/confluence/display/cplusplus/MEM50-CPP.+Do+not+access+freed+memory
*
* cert-MEM50.cpp:15:23: warning: object backing the pointer will be destroyed at the end of the
full-expression [-Wdangling-gsl] const char* str = str_func().c_str();
^~~~~~~~~~
1 warning generated.
builddriver executing: 'run-clang-tidy cert-MEM50.cpp'
Compilation SUCCEED in 3.240367 seconds
Number of warnings: 6
WarningErrorEntry(path='cert-MEM50.cpp', lineno='16', severity='warning', message='Inner pointer of
container used after re/deallocation [clang-analyzer-cplusplus.InnerPointer]', column='5')
WarningErrorEntry(path='cert-MEM50.cpp', lineno='32', severity='warning', message='Use of memory after
it is freed [clang-analyzer-cplusplus.NewDelete]', column='14')
WarningErrorEntry(path='cert-MEM50.cpp', lineno='43', severity='warning', message='Use of memory after
it is freed [clang-analyzer-cplusplus.NewDelete]', column='14')
WarningErrorEntry(path='cert-MEM50.cpp', lineno='57', severity='warning', message='do not declare
variables of type va_list; use variadic templates instead [cppcoreguidelines-pro-type-vararg]',
column='5') WarningErrorEntry(path='cert-MEM50.cpp', lineno='58', severity='warning', message='Use of
zero-allocated memory [clang-analyzer-cplusplus.NewDelete]', column='10')
WarningErrorEntry(path='cert-MEM50.cpp', lineno='80', severity='warning', message='do not use pointer
arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]', column='42')
*/