Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cpp/ql/test/examples/BadLocking/AV Rule 107.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| UnintendedDeclaration.cpp:51:2:51:22 | declaration | Functions should be declared at file scope, not inside blocks. |
| UnintendedDeclaration.cpp:72:2:72:27 | declaration | Functions should be declared at file scope, not inside blocks. |
1 change: 1 addition & 0 deletions cpp/ql/test/examples/BadLocking/AV Rule 107.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jsf/4.13 Functions/AV Rule 107.ql
7 changes: 7 additions & 0 deletions cpp/ql/test/examples/BadLocking/DeclStmts.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
| UnintendedDeclaration.cpp:44:2:44:29 | declaration | myLock | Variable |
| UnintendedDeclaration.cpp:51:2:51:22 | declaration | myLock | Function |
| UnintendedDeclaration.cpp:58:2:58:20 | declaration | myLock | Variable |
| UnintendedDeclaration.cpp:65:2:65:22 | declaration | myMutex | Variable |
| UnintendedDeclaration.cpp:72:2:72:27 | declaration | myLock | Function |
| UnintendedDeclaration.cpp:82:3:82:34 | declaration | myLock | Variable |
| UnintendedDeclaration.cpp:89:3:89:27 | declaration | memberMutex | Variable |
18 changes: 18 additions & 0 deletions cpp/ql/test/examples/BadLocking/DeclStmts.ql
Original file line number Diff line number Diff line change
@@ -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), ", ")
Original file line number Diff line number Diff line change
@@ -0,0 +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 |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Best Practices/Hiding/LocalVariableHidesGlobalVariable.ql
96 changes: 96 additions & 0 deletions cpp/ql/test/examples/BadLocking/UnintendedDeclaration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

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()
{
if (m)
{
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;
};