Skip to content

Commit 1d80697

Browse files
authored
Merge pull request #1634 from aibaars/cookbook
Approved by aschackmull, dave-bartolomeo, hvitved, markshannon, xiemaisi, yh-semmle
2 parents 7123067 + b3c403a commit 1d80697

File tree

156 files changed

+2493
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

156 files changed

+2493
-0
lines changed

cpp/ql/examples/queries.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<queries language="cpp"/>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @id cpp/examples/addressof
3+
* @name Address of reference variable
4+
* @description Finds address-of expressions (`&`) that take the address
5+
* of a reference variable
6+
* @tags addressof
7+
* reference
8+
*/
9+
10+
import cpp
11+
12+
from AddressOfExpr addr, VariableAccess access
13+
where
14+
access = addr.getOperand() and
15+
access.getTarget().getType() instanceof ReferenceType
16+
select addr
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @id cpp/examples/arrayaccess
3+
* @name Array access
4+
* @description Finds array access expressions with an index expression
5+
* consisting of a postfix increment (`++`) expression.
6+
* @tags array
7+
* access
8+
* index
9+
* postfix
10+
* increment
11+
*/
12+
13+
import cpp
14+
15+
from ArrayExpr a
16+
where a.getArrayOffset() instanceof PostfixIncrExpr
17+
select a
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @id cpp/examples/castexpr
3+
* @name Cast expressions
4+
* @description Finds casts from a floating point type to an integer type
5+
* @tags cast
6+
* integer
7+
* float
8+
* type
9+
*/
10+
11+
import cpp
12+
13+
from Cast c
14+
where c.getExpr().getType() instanceof FloatingPointType
15+
and c.getType() instanceof IntegralType
16+
select c
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @id cpp/examples/catch-exception
3+
* @name Catch exception
4+
* @description Finds places where we catch exceptions of type `parse_error`
5+
* @tags catch
6+
* try
7+
* exception
8+
*/
9+
10+
import cpp
11+
12+
from CatchBlock catch
13+
// `stripType` converts `const parse_error &` to `parse_error`.
14+
where catch.getParameter().getType().stripType().hasName("parse_error")
15+
select catch
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @id cpp/examples/constructor-call
3+
* @name Call to constructor
4+
* @description Finds places where we call `new MyClass(...)`
5+
* @tags call
6+
* constructor
7+
* new
8+
*/
9+
10+
import cpp
11+
12+
from NewExpr new, Constructor c
13+
where
14+
c = new.getInitializer().(ConstructorCall).getTarget() and
15+
c.getName() = "MyClass"
16+
select new
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @id cpp/examples/derives-from-class
3+
* @name Class derives from
4+
* @description Finds classes that derive from `std::exception`
5+
* @tags base
6+
* class
7+
* derive
8+
* inherit
9+
* override
10+
* subtype
11+
* supertype
12+
*/
13+
14+
import cpp
15+
16+
from Class type
17+
where
18+
type.getABaseClass+().hasName("exception") and
19+
type.getNamespace().getName() = "std"
20+
select type
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @id cpp/examples/emptyblock
3+
* @name Empty blocks
4+
* @description Finds empty block statements
5+
* @tags empty
6+
* block
7+
* statement
8+
*/
9+
10+
import cpp
11+
12+
from Block blk
13+
where blk.getNumStmt() = 0
14+
select blk
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @id cpp/examples/emptythen
3+
* @name If statements with empty then branch
4+
* @description Finds `if` statements where the `then` branch is
5+
* an empty block statement
6+
* @tags if
7+
* then
8+
* empty
9+
* conditional
10+
* branch
11+
*/
12+
13+
import cpp
14+
15+
from IfStmt i
16+
where i.getThen().(Block).getNumStmt() = 0
17+
select i
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @id cpp/examples/eq-true
3+
* @name Equality test on boolean
4+
* @description Finds tests like `==true`, `!=true`
5+
* @tags equal
6+
* comparison
7+
* test
8+
* boolean
9+
*/
10+
11+
import cpp
12+
13+
from EqualityOperation eq, Expr trueExpr
14+
where
15+
trueExpr = eq.getAnOperand() and
16+
trueExpr.getType() instanceof BoolType and
17+
trueExpr.getValue().toInt() = 1
18+
select eq

0 commit comments

Comments
 (0)