Skip to content

Commit 64c1c9a

Browse files
committed
tests
1 parent fec7e5d commit 64c1c9a

File tree

6 files changed

+91
-40
lines changed

6 files changed

+91
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:43:3:43:10 | call to mtx_init | Mutex used by thread potentially destroyed while in use. |
2+
| test.c:58:3:58:10 | call to mtx_init | 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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <stddef.h>
2+
#include <threads.h>
3+
4+
int t3(void *data) {
5+
mtx_t *mxl = (mtx_t *)data;
6+
mtx_lock(mxl);
7+
mtx_unlock(mxl);
8+
return 0;
9+
}
10+
11+
int t4(void *data) {
12+
mtx_t *mxl = (mtx_t *)data;
13+
mtx_lock(mxl);
14+
mtx_unlock(mxl);
15+
return 0;
16+
}
17+
18+
// case 3 - correctly waiting for a well-behaved thread, with a local mutex.
19+
int f3() {
20+
thrd_t threads[5];
21+
22+
mtx_t mxl;
23+
24+
mtx_init(&mxl, mtx_plain); // COMPLIANT
25+
26+
for (size_t i = 0; i < 1; i++) {
27+
thrd_create(&threads[i], t3, &mxl);
28+
}
29+
30+
for (size_t i = 0; i < 1; i++) {
31+
thrd_join(threads[i], 0);
32+
}
33+
34+
mtx_destroy(&mxl);
35+
return 0;
36+
}
37+
38+
// case 4 - incorrectly waiting for a well behaved thread, with a local mutex.
39+
int f4() {
40+
thrd_t threads[5];
41+
mtx_t mxl;
42+
43+
mtx_init(&mxl, mtx_plain); // NON_COMPLIANT
44+
45+
for (size_t i = 0; i < 1; i++) {
46+
thrd_create(&threads[i], t3, &mxl);
47+
}
48+
49+
mtx_destroy(&mxl);
50+
return 0;
51+
}
52+
53+
// case 5 - incorrectly waiting with a stack local variable.
54+
int f5() {
55+
thrd_t threads[5];
56+
mtx_t mxl;
57+
58+
mtx_init(&mxl, mtx_plain); // NON_COMPLIANT
59+
60+
for (size_t i = 0; i < 1; i++) {
61+
thrd_create(&threads[i], t4, &mxl);
62+
}
63+
64+
return 0;
65+
}
66+
67+
// case 6 - correctly waiting with a stack local variable.
68+
int f6() {
69+
thrd_t threads[5];
70+
mtx_t mxl;
71+
72+
mtx_init(&mxl, mtx_plain); // COMPLIANT
73+
74+
for (size_t i = 0; i < 1; i++) {
75+
thrd_create(&threads[i], t4, &mxl);
76+
}
77+
78+
for (size_t i = 0; i < 1; i++) {
79+
thrd_join(threads[i], 0);
80+
}
81+
82+
return 0;
83+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:49:3:49:10 | call to mtx_init | Mutex used by thread potentially $@ while in use. | test.c:55:3:55:13 | call to mtx_destroy | destroyed |
2+
| test.c:84:3:84:10 | call to mtx_init | Mutex used by thread potentially $@ while in use. | test.c:90:3:90:13 | call to mtx_destroy | 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

cpp/cert/test/rules/CON50-CPP/test.c renamed to c/common/test/rules/donotdestroyamutexwhileitislocked/test.c

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <stdatomic.h>
21
#include <stddef.h>
32
#include <threads.h>
43

@@ -26,13 +25,6 @@ int t3(void *data) {
2625
return 0;
2726
}
2827

29-
int t4(void *data) {
30-
mtx_t *mxl = (mtx_t *)data;
31-
mtx_lock(mxl);
32-
mtx_unlock(mxl);
33-
return 0;
34-
}
35-
3628
// case 1 - correctly waiting for a well-behaved thread
3729
int f1() {
3830
thrd_t threads[5];
@@ -98,35 +90,3 @@ int f4() {
9890
mtx_destroy(&mxl);
9991
return 0;
10092
}
101-
102-
// case 5 - incorrectly waiting with a stack local variable.
103-
int f5() {
104-
thrd_t threads[5];
105-
mtx_t mxl;
106-
107-
mtx_init(&mxl, mtx_plain); // NON_COMPLIANT
108-
109-
for (size_t i = 0; i < 1; i++) {
110-
thrd_create(&threads[i], t4, &mxl);
111-
}
112-
113-
return 0;
114-
}
115-
116-
// case 6 - correctly waiting with a stack local variable.
117-
int f6() {
118-
thrd_t threads[5];
119-
mtx_t mxl;
120-
121-
mtx_init(&mxl, mtx_plain); // COMPLIANT
122-
123-
for (size_t i = 0; i < 1; i++) {
124-
thrd_create(&threads[i], t4, &mxl);
125-
}
126-
127-
for (size_t i = 0; i < 1; i++) {
128-
thrd_join(threads[i], 0);
129-
}
130-
131-
return 0;
132-
}

0 commit comments

Comments
 (0)