Skip to content

Commit 8cfed06

Browse files
committed
new tests
1 parent 5decd4c commit 8cfed06

File tree

6 files changed

+135
-0
lines changed

6 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.cpp:16:14:16:15 | call to mutex | Mutex used by thread potentially destroyed while in use. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.donotallowamutextogooutofscopewhilelocked.DoNotAllowAMutexToGoOutOfScopeWhileLocked
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include <mutex>
2+
#include <thread>
3+
4+
std::mutex *m4 = new std::mutex(); // COMPLIANT
5+
std::mutex m5; // COMPLIANT
6+
std::mutex *m6 = new std::mutex(); // COMPLIANT
7+
8+
void t1(int i, std::mutex *pm) {}
9+
void t2(int i, std::mutex **pm) {}
10+
void t3(int i, std::mutex *pm) { delete pm; }
11+
void t4(int i) { delete m4; }
12+
void t5(int i) { std::lock_guard<std::mutex> lk(m5); }
13+
14+
void f1() {
15+
std::thread threads[5];
16+
std::mutex m1; // NON_COMPLIANT
17+
18+
for (int i = 0; i < 5; ++i) {
19+
threads[i] = std::thread(t1, i, &m1);
20+
}
21+
}
22+
23+
void f2() {
24+
std::thread threads[5];
25+
std::mutex m1; // COMPLIANT
26+
27+
for (int i = 0; i < 5; ++i) {
28+
threads[i] = std::thread(t1, i, &m1);
29+
}
30+
31+
for (int i = 0; i < 5; ++i) {
32+
threads[i].join();
33+
}
34+
}
35+
36+
std::mutex m2; // COMPLIANT - m2 is not deleted and never goes out of scope.
37+
// There is no delete
38+
39+
void f3() {
40+
std::thread threads[5];
41+
42+
for (int i = 0; i < 5; ++i) {
43+
threads[i] = std::thread(t1, i, &m2);
44+
}
45+
}
46+
47+
std::mutex *m3;
48+
49+
void f4() {
50+
m3 = new std::mutex(); // COMPLIANT
51+
52+
std::thread threads[5];
53+
54+
for (int i = 0; i < 5; ++i) {
55+
threads[i] = std::thread(t1, i, m3);
56+
}
57+
58+
// since we wait here, and the local function created the
59+
// mutex, the following delete is allowed.
60+
for (int i = 0; i < 5; ++i) {
61+
threads[i].join();
62+
}
63+
64+
delete m3;
65+
}
66+
67+
void f5() {
68+
m3 = new std::mutex(); // COMPLIANT
69+
70+
std::thread threads[5];
71+
72+
for (int i = 0; i < 5; ++i) {
73+
threads[i] = std::thread(t2, i, &m3);
74+
}
75+
}
76+
77+
void f6() {
78+
m3 = new std::mutex(); // COMPLIANT
79+
80+
std::thread threads[5];
81+
82+
for (int i = 0; i < 5; ++i) {
83+
threads[i] = std::thread(t1, i, m3);
84+
}
85+
86+
for (int i = 0; i < 5; ++i) {
87+
threads[i].join();
88+
}
89+
90+
delete m3;
91+
}
92+
93+
void f7() {
94+
m3 = new std::mutex(); // COMPLIANT - Not related to scope
95+
96+
std::thread threads[5];
97+
98+
for (int i = 0; i < 5; ++i) {
99+
threads[i] = std::thread(t3, i, m3);
100+
}
101+
102+
for (int i = 0; i < 5; ++i) {
103+
threads[i].join();
104+
}
105+
}
106+
107+
void f8() {
108+
std::mutex *m = new std::mutex(); // COMPLIANT
109+
delete m;
110+
}
111+
112+
void f9() {
113+
std::mutex m; // COMPLIANT
114+
}
115+
116+
// f10 does not wait but it is OK since m5 is global and doesn't go out of
117+
// scope -- the destructor isn't called until the program exists.
118+
void f10() {
119+
std::thread threads[5];
120+
121+
for (int i = 0; i < 5; ++i) {
122+
threads[i] = std::thread(t5, i);
123+
}
124+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| test.cpp:4:18:4:33 | call to mutex | Mutex used by thread potentially $@ while in use. | test.cpp:11:18:11:26 | call to ~mutex | destroyed |
2+
| test.cpp:4:18:4:33 | call to mutex | Mutex used by thread potentially $@ while in use. | test.cpp:11:18:11:26 | delete | destroyed |
3+
| test.cpp:16:14:16:15 | call to mutex | Mutex used by thread potentially $@ while in use. | test.cpp:21:1:21:1 | call to ~mutex | destroyed |
4+
| test.cpp:16:14:16:15 | call to mutex | Mutex used by thread potentially $@ while in use. | test.cpp:21:1:21:1 | return ... | destroyed |
5+
| test.cpp:94:8:94:23 | call to mutex | Mutex used by thread potentially $@ while in use. | test.cpp:10:34:10:42 | call to ~mutex | destroyed |
6+
| test.cpp:94:8:94:23 | call to mutex | Mutex used by thread potentially $@ while in use. | test.cpp:10:34:10:42 | delete | destroyed |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.donotdestroyamutexwhileitislocked.DoNotDestroyAMutexWhileItIsLocked

0 commit comments

Comments
 (0)