mirror of
https://github.com/github/codeql.git
synced 2025-12-20 02:44:30 +01:00
CPP: Add a test of common mistakes using locking and similar classes.
This commit is contained in:
2
cpp/ql/test/examples/BadLocking/AV Rule 107.expected
Normal file
2
cpp/ql/test/examples/BadLocking/AV Rule 107.expected
Normal file
@@ -0,0 +1,2 @@
|
||||
| UnintendedDeclaration.cpp:48:2:48:22 | declaration | Functions should be declared at file scope, not inside blocks. |
|
||||
| UnintendedDeclaration.cpp:69:2:69:27 | declaration | Functions should be declared at file scope, not inside blocks. |
|
||||
1
cpp/ql/test/examples/BadLocking/AV Rule 107.qlref
Normal file
1
cpp/ql/test/examples/BadLocking/AV Rule 107.qlref
Normal file
@@ -0,0 +1 @@
|
||||
jsf/4.13 Functions/AV Rule 107.ql
|
||||
7
cpp/ql/test/examples/BadLocking/DeclStmts.expected
Normal file
7
cpp/ql/test/examples/BadLocking/DeclStmts.expected
Normal file
@@ -0,0 +1,7 @@
|
||||
| UnintendedDeclaration.cpp:41:2:41:29 | declaration | myLock | Variable |
|
||||
| UnintendedDeclaration.cpp:48:2:48:22 | declaration | myLock | Function |
|
||||
| UnintendedDeclaration.cpp:55:2:55:20 | declaration | myLock | Variable |
|
||||
| UnintendedDeclaration.cpp:62:2:62:22 | declaration | myMutex | Variable |
|
||||
| UnintendedDeclaration.cpp:69:2:69:27 | declaration | myLock | Function |
|
||||
| UnintendedDeclaration.cpp:79:3:79:34 | declaration | myLock | Variable |
|
||||
| UnintendedDeclaration.cpp:86:3:86:27 | declaration | memberMutex | Variable |
|
||||
18
cpp/ql/test/examples/BadLocking/DeclStmts.ql
Normal file
18
cpp/ql/test/examples/BadLocking/DeclStmts.ql
Normal file
@@ -0,0 +1,18 @@
|
||||
import cpp
|
||||
|
||||
string describe(Declaration d)
|
||||
{
|
||||
(
|
||||
d instanceof Variable and
|
||||
result = "Variable"
|
||||
) or (
|
||||
d instanceof Function and
|
||||
result = "Function"
|
||||
)
|
||||
}
|
||||
|
||||
from DeclStmt ds, Declaration d
|
||||
where
|
||||
ds.getADeclaration() = d
|
||||
select
|
||||
ds, concat(d.getName(), ", "), concat(describe(d), ", ")
|
||||
@@ -0,0 +1 @@
|
||||
| UnintendedDeclaration.cpp:62:14:62:20 | definition of myMutex | Local variable myMutex hides $@ with the same name. | UnintendedDeclaration.cpp:37:7:37:13 | myMutex | a global variable |
|
||||
@@ -0,0 +1 @@
|
||||
Best Practices/Hiding/LocalVariableHidesGlobalVariable.ql
|
||||
93
cpp/ql/test/examples/BadLocking/UnintendedDeclaration.cpp
Normal file
93
cpp/ql/test/examples/BadLocking/UnintendedDeclaration.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
|
||||
class Mutex
|
||||
{
|
||||
public:
|
||||
Mutex();
|
||||
~Mutex();
|
||||
|
||||
void lock();
|
||||
void unlock();
|
||||
|
||||
private:
|
||||
// ...
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class Lock
|
||||
{
|
||||
public:
|
||||
Lock() : m(0)
|
||||
{
|
||||
}
|
||||
|
||||
Lock(T &_m) : m(&_m)
|
||||
{
|
||||
m->lock();
|
||||
}
|
||||
|
||||
~Lock()
|
||||
{
|
||||
m->unlock();
|
||||
}
|
||||
|
||||
private:
|
||||
T *m;
|
||||
};
|
||||
|
||||
Mutex myMutex;
|
||||
|
||||
void test1()
|
||||
{
|
||||
Lock<Mutex> myLock(myMutex); // GOOD (creates `myLock` on `myMutex`)
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
void test2()
|
||||
{
|
||||
Lock<Mutex> myLock(); // BAD (interpreted as a function declaration, this does nothing)
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
void test3()
|
||||
{
|
||||
Lock<Mutex> myLock; // GOOD (creates an uninitialized variable called `myLock`, probably intended)
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
void test4()
|
||||
{
|
||||
Lock<Mutex>(myMutex); // BAD (creates an uninitialized variable called `myMutex`, probably not intended)
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
void test5()
|
||||
{
|
||||
Lock<Mutex> myLock(Mutex); // BAD (interpreted as a function declaration, this does nothing)
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
class MyTestClass
|
||||
{
|
||||
public:
|
||||
void test6()
|
||||
{
|
||||
Lock<Mutex> myLock(memberMutex); // GOOD (creates `myLock` on `memberMutex`)
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
void test7()
|
||||
{
|
||||
Lock<Mutex>(memberMutex); // BAD (creates an uninitialized variable called `memberMutex`, probably not intended) [NOT DETECTED]
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
private:
|
||||
Mutex memberMutex;
|
||||
};
|
||||
Reference in New Issue
Block a user