Skip to content

Commit 19b05c5

Browse files
authored
Merge pull request #1204 from geoffw0/badlock
CPP: Add a test of common mistakes using locking classes.
2 parents 063dbee + 34fbc7b commit 19b05c5

File tree

7 files changed

+126
-0
lines changed

7 files changed

+126
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| UnintendedDeclaration.cpp:51:2:51:22 | declaration | Functions should be declared at file scope, not inside blocks. |
2+
| UnintendedDeclaration.cpp:72:2:72:27 | declaration | Functions should be declared at file scope, not inside blocks. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jsf/4.13 Functions/AV Rule 107.ql
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| UnintendedDeclaration.cpp:44:2:44:29 | declaration | myLock | Variable |
2+
| UnintendedDeclaration.cpp:51:2:51:22 | declaration | myLock | Function |
3+
| UnintendedDeclaration.cpp:58:2:58:20 | declaration | myLock | Variable |
4+
| UnintendedDeclaration.cpp:65:2:65:22 | declaration | myMutex | Variable |
5+
| UnintendedDeclaration.cpp:72:2:72:27 | declaration | myLock | Function |
6+
| UnintendedDeclaration.cpp:82:3:82:34 | declaration | myLock | Variable |
7+
| UnintendedDeclaration.cpp:89:3:89:27 | declaration | memberMutex | Variable |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import cpp
2+
3+
string describe(Declaration d)
4+
{
5+
(
6+
d instanceof Variable and
7+
result = "Variable"
8+
) or (
9+
d instanceof Function and
10+
result = "Function"
11+
)
12+
}
13+
14+
from DeclStmt ds, Declaration d
15+
where
16+
ds.getADeclaration() = d
17+
select
18+
ds, concat(d.getName(), ", "), concat(describe(d), ", ")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| UnintendedDeclaration.cpp:65:14:65:20 | definition of myMutex | Local variable myMutex hides $@ with the same name. | UnintendedDeclaration.cpp:40:7:40:13 | myMutex | a global variable |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Best Practices/Hiding/LocalVariableHidesGlobalVariable.ql
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
class Mutex
3+
{
4+
public:
5+
Mutex();
6+
~Mutex();
7+
8+
void lock();
9+
void unlock();
10+
11+
private:
12+
// ...
13+
};
14+
15+
template<class T>
16+
class Lock
17+
{
18+
public:
19+
Lock() : m(0)
20+
{
21+
}
22+
23+
Lock(T &_m) : m(&_m)
24+
{
25+
m->lock();
26+
}
27+
28+
~Lock()
29+
{
30+
if (m)
31+
{
32+
m->unlock();
33+
}
34+
}
35+
36+
private:
37+
T *m;
38+
};
39+
40+
Mutex myMutex;
41+
42+
void test1()
43+
{
44+
Lock<Mutex> myLock(myMutex); // GOOD (creates `myLock` on `myMutex`)
45+
46+
// ...
47+
}
48+
49+
void test2()
50+
{
51+
Lock<Mutex> myLock(); // BAD (interpreted as a function declaration, this does nothing)
52+
53+
// ...
54+
}
55+
56+
void test3()
57+
{
58+
Lock<Mutex> myLock; // GOOD (creates an uninitialized variable called `myLock`, probably intended)
59+
60+
// ...
61+
}
62+
63+
void test4()
64+
{
65+
Lock<Mutex>(myMutex); // BAD (creates an uninitialized variable called `myMutex`, probably not intended)
66+
67+
// ...
68+
}
69+
70+
void test5()
71+
{
72+
Lock<Mutex> myLock(Mutex); // BAD (interpreted as a function declaration, this does nothing)
73+
74+
// ...
75+
}
76+
77+
class MyTestClass
78+
{
79+
public:
80+
void test6()
81+
{
82+
Lock<Mutex> myLock(memberMutex); // GOOD (creates `myLock` on `memberMutex`)
83+
84+
// ...
85+
}
86+
87+
void test7()
88+
{
89+
Lock<Mutex>(memberMutex); // BAD (creates an uninitialized variable called `memberMutex`, probably not intended) [NOT DETECTED]
90+
91+
// ...
92+
}
93+
94+
private:
95+
Mutex memberMutex;
96+
};

0 commit comments

Comments
 (0)