Skip to content

Commit 30860da

Browse files
committed
Add cookbook queries
1 parent a1b4d09 commit 30860da

156 files changed

Lines changed: 2342 additions & 0 deletions

File tree

Some content is hidden

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

cpp/ql/examples/addressof.ql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Address of reference variable
3+
* @description Finds address-of expressions (`&`) that take the address
4+
* of a reference variable
5+
* @tags addressof
6+
* reference
7+
*/
8+
9+
import cpp
10+
11+
from AddressOfExpr addr, VariableAccess access
12+
where
13+
access = addr.getOperand() and
14+
access.getTarget().getType() instanceof ReferenceType
15+
select addr

cpp/ql/examples/arrayaccess.ql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @name Array access
3+
* @description Finds array access expressions with an index expression
4+
* consisting of a postfix increment (`++`) expression.
5+
* @tags array
6+
* access
7+
* index
8+
* postfix
9+
* increment
10+
*/
11+
12+
import cpp
13+
14+
from ArrayExpr a
15+
where a.getArrayOffset() instanceof PostfixIncrExpr
16+
select a

cpp/ql/examples/castexpr.ql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Cast expressions
3+
* @description Finds casts from a floating point type to an integer type
4+
* @tags cast
5+
* integer
6+
* float
7+
* type
8+
*/
9+
10+
import cpp
11+
12+
from Cast c
13+
where c.getExpr().getType() instanceof FloatingPointType
14+
and c.getType() instanceof IntegralType
15+
select c

cpp/ql/examples/catch_exception.ql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @name Catch exception
3+
* @description Finds places where we catch exceptions of type `parse_error`
4+
* @tags catch
5+
* try
6+
* exception
7+
*/
8+
9+
import cpp
10+
11+
from CatchBlock catch
12+
// `stripType` converts `const parse_error &` to `parse_error`.
13+
where catch.getParameter().getType().stripType().hasName("parse_error")
14+
select catch
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Call to constructor
3+
* @description Finds places where we call `new MyClass(...)`
4+
* @tags call
5+
* constructor
6+
* new
7+
*/
8+
9+
import cpp
10+
11+
from NewExpr new, Constructor c
12+
where
13+
c = new.getInitializer().(ConstructorCall).getTarget() and
14+
c.getName() = "MyClass"
15+
select new
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @name Class derives from
3+
* @description Finds classes that derive from `std::exception`
4+
* @tags base
5+
* class
6+
* derive
7+
* inherit
8+
* override
9+
* subtype
10+
* supertype
11+
*/
12+
13+
import cpp
14+
15+
from Class type
16+
where
17+
type.getABaseClass+().hasName("exception") and
18+
type.getNamespace().getName() = "std"
19+
select type

cpp/ql/examples/emptyblock.ql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Empty blocks
3+
* @description Finds empty block statements
4+
* @tags empty
5+
* block
6+
* statement
7+
*/
8+
9+
import cpp
10+
11+
from Block blk
12+
where blk.getNumStmt() = 0
13+
select blk

cpp/ql/examples/emptythen.ql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @name If statements with empty then branch
3+
* @description Finds `if` statements where the `then` branch is
4+
* an empty block statement
5+
* @tags if
6+
* then
7+
* empty
8+
* conditional
9+
* branch
10+
*/
11+
12+
import cpp
13+
14+
from IfStmt i
15+
where i.getThen().(Block).getNumStmt() = 0
16+
select i

cpp/ql/examples/eq_true.ql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @name Equality test on boolean
3+
* @description Finds tests like `==true`, `!=true`
4+
* @tags equal
5+
* comparison
6+
* test
7+
* boolean
8+
*/
9+
10+
import cpp
11+
12+
from EqualityOperation eq, Expr trueExpr
13+
where
14+
trueExpr = eq.getAnOperand() and
15+
trueExpr.getType() instanceof BoolType and
16+
trueExpr.getValue().toInt() = 1
17+
select eq

cpp/ql/examples/field_access.ql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Access of field
3+
* @description Finds reads of `aDate` (defined on class `Order`)
4+
* @tags access
5+
* field
6+
* read
7+
*/
8+
9+
import cpp
10+
11+
from Field f, FieldAccess access
12+
where f.hasName("aDate")
13+
and f.getDeclaringType().hasName("Order")
14+
and f = access.getTarget()
15+
select access

0 commit comments

Comments
 (0)