Skip to content

Commit

Permalink
Merge pull request github#220 from max/example-queries
Browse files Browse the repository at this point in the history
Add example queries
  • Loading branch information
Sauyon Lee authored and GitHub Enterprise committed Jan 24, 2020
2 parents 9507a22 + c30b1d9 commit 6e4880b
Show file tree
Hide file tree
Showing 67 changed files with 462 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ ql/src/go.dbscheme.stats: ql/src/go.dbscheme
odasa collectStats --dbscheme $^ --db build/stats-project/revision/working/db-go --outputFile $@

test: all extractor build/testdb/check-upgrade-path
codeql test run ql/test --search-path . --additional-packs .
codeql test run ql/test --search-path . --additional-packs ql
cd extractor; go test -mod=vendor ./... | grep -vF "[no test files]"

.PHONY: build/testdb/check-upgrade-path
Expand Down
12 changes: 12 additions & 0 deletions ql/examples/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>go-examples</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
<nature>com.semmle.plugin.qdt.core.qlnature</nature>
</natures>
</projectDescription>
10 changes: 10 additions & 0 deletions ql/examples/.qlpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:qlpath xmlns:ns2="https://semmle.com/schemas/qlpath">
<librarypath>
<path kind="WORKSPACE">/go-queries</path>
</librarypath>
<dbscheme kind="WORKSPACE">/go-queries/go.dbscheme</dbscheme>
<defaultImports>
<defaultImport>go</defaultImport>
</defaultImports>
</ns2:qlpath>
3 changes: 3 additions & 0 deletions ql/examples/qlpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: codeql-go-examples
version: 0.0.0
libraryPathDependencies: codeql-go
1 change: 1 addition & 0 deletions ql/examples/queries.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<queries language="go"/>
15 changes: 15 additions & 0 deletions ql/examples/snippets/calltobuiltin.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @name Call to built-in function
* @description Finds calls to the built-in `len` function.
* @id go/examples/calltolen
* @tags call
* function
* len
* built-in
*/

import go

from DataFlow::CallNode call
where call = Builtin::len().getACall()
select call
16 changes: 16 additions & 0 deletions ql/examples/snippets/calltofunction.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @name Call to library function
* @description Finds calls to "fmt.Println".
* @id go/examples/calltoprintln
* @tags call
* function
* println
*/

import go

from Function println, DataFlow::CallNode call
where
println.hasQualifiedName("fmt", "Println") and
call = println.getACall()
select call
18 changes: 18 additions & 0 deletions ql/examples/snippets/calltomethod.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @name Call to method
* @description Finds calls to the `Get` method of type `Header` from the `net/http` package.
* @id go/examples/calltoheaderget
* @tags call
* function
* net/http
* Header
* strings
*/

import go

from Method get, DataFlow::CallNode call
where
get.hasQualifiedName("net/http", "Header", "Get") and
call = get.getACall()
select call
14 changes: 14 additions & 0 deletions ql/examples/snippets/constant.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @name Compile-time constant
* @description Finds compile-time constants with value zero.
* @id go/examples/zeroconstant
* @tags expression
* numeric value
* constant
*/

import go

from DataFlow::Node zero
where zero.getNumericValue() = 0
select zero
18 changes: 18 additions & 0 deletions ql/examples/snippets/emptythen.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @name If statements with empty then branch
* @description Finds 'if' statements where the 'then' branch is
* an empty block statement
* @id go/examples/emptythen
* @tags if
* then
* empty
* conditional
* branch
* statement
*/

import go

from IfStmt i
where i.getThen().getNumStmt() = 0
select i
15 changes: 15 additions & 0 deletions ql/examples/snippets/fieldread.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @name Field read
* @description Finds code that reads `Request.Method`.
* @id go/examples/readofrequestmethod
* @tags field
* read
*/

import go

from Field reqm, Read read
where
reqm.hasQualifiedName("net/http", "Request", "Method") and
read = reqm.getARead()
select read
15 changes: 15 additions & 0 deletions ql/examples/snippets/fieldwrite.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @name Field write
* @description Finds assignments to field `Status` of type `Response` from package `net/http`.
* @id go/examples/responsestatus
* @tags net/http
* field write
*/

import go

from Field status, Write write
where
status.hasQualifiedName("net/http", "Response", "Status") and
write = status.getAWrite()
select write, write.getRhs()
13 changes: 13 additions & 0 deletions ql/examples/snippets/function.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @name Function
* @description Finds functions called "main".
* @id go/examples/mainfunction
* @tags function
* main
*/

import go

from Function main
where main.getName() = "main"
select main
15 changes: 15 additions & 0 deletions ql/examples/snippets/nilcheck.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @name Comparison with nil
* @description Finds comparisons with nil.
* @id go/examples/nilcheck
* @tags comparison
* nil
*/

import go

from DataFlow::EqualityTestNode eq, DataFlow::Node nd, DataFlow::Node nil
where
nil = Builtin::nil().getARead() and
eq.eq(_, nd, nil)
select eq
12 changes: 12 additions & 0 deletions ql/examples/snippets/param.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @name Parameter
* @description Finds parameters of type "ResponseWriter" from package "net/http".
* @id go/examples/responseparam
* @tags parameter
*/

import go

from Parameter req
where req.getType().hasQualifiedName("net/http", "ResponseWriter")
select req
15 changes: 15 additions & 0 deletions ql/examples/snippets/pointertype.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @name Type
* @description Finds pointer type `*Request` from package `net/http`.
* @id go/examples/requestptrtype
* @tags net/http
* type
*/

import go

from Type reqtp, PointerType reqptrtp
where
reqtp.hasQualifiedName("net/http", "Request") and
reqptrtp.getBaseType() = reqtp
select reqptrtp
12 changes: 12 additions & 0 deletions ql/examples/snippets/receiver.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @name Receiver variable
* @description Finds receiver variables of pointer type.
* @id go/examples/pointerreceiver
* @tags receiver variable
*/

import go

from ReceiverVariable recv
where recv.getType() instanceof PointerType
select recv
12 changes: 12 additions & 0 deletions ql/examples/snippets/result.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @name Result variable
* @description Finds result variables of type "error".
* @id go/examples/errresult
* @tags result variable
*/

import go

from ResultVariable err
where err.getType() = Builtin::error().getType()
select err
13 changes: 13 additions & 0 deletions ql/examples/snippets/type.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @name Type
* @description Finds type `Request` from package `net/http`.
* @id go/examples/requesttype
* @tags net/http
* type
*/

import go

from Type request
where request.hasQualifiedName("net/http", "Request")
select request
16 changes: 16 additions & 0 deletions ql/examples/snippets/typeinfo.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @name Type information
* @description Finds code elements of type `*Request` from package `net/http`.
* @id go/examples/requests
* @tags net/http
* types
*/

import go

from Type reqtp, PointerType reqptrtp, DataFlow::Node req
where
reqtp.hasQualifiedName("net/http", "Request") and
reqptrtp.getBaseType() = reqtp and
req.getType() = reqptrtp
select req
13 changes: 13 additions & 0 deletions ql/examples/snippets/updateinloop.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @name Increment statements in loops
* @description Finds increment statements that are nested in a loop
* @id go/examples/updateinloop
* @tags nesting
* increment
*/

import go

from IncStmt s, LoopStmt l
where s.getParent+() = l
select s, l
13 changes: 13 additions & 0 deletions ql/examples/snippets/variable.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @name Variable
* @description Finds variables called "err".
* @id go/examples/errvariable
* @tags variable
* err
*/

import go

from Variable err
where err.getName() = "err"
select err
14 changes: 14 additions & 0 deletions ql/examples/snippets/varread.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @name Variable read
* @description Finds code that reads a variable called `err`.
* @id go/examples/readoferr
* @tags variable read
*/

import go

from Variable err, Read read
where
err.getName() = "err" and
read = err.getARead()
select read
14 changes: 14 additions & 0 deletions ql/examples/snippets/varwrite.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @name Variable write
* @description Finds assignments to variables named "err".
* @id go/examples/errwrite
* @tags variable write
*/

import go

from Variable err, Write write
where
err.getName() = "err" and
write = err.getAWrite()
select write, write.getRhs()
16 changes: 16 additions & 0 deletions ql/examples/snippets/zerocheck.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @name Comparison with zero
* @description Finds comparisons between an unsigned value and zero.
* @id go/examples/unsignedgez
* @tags comparison
* unsigned
*/

import go

from DataFlow::RelationalComparisonNode cmp, DataFlow::Node unsigned, DataFlow::Node zero
where
zero.getNumericValue() = 0 and
unsigned.getType().getUnderlyingType() instanceof UnsignedIntegerType and
cmp.leq(_, zero, unsigned, 0)
select cmp, unsigned
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/calltobuiltin.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| main.go:15:41:15:52 | call to len |
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/calltobuiltin.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snippets/calltobuiltin.ql
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/calltofunction.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| main.go:14:2:14:29 | call to Println |
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/calltofunction.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snippets/calltofunction.ql
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/calltomethod.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| main.go:19:2:19:22 | call to Get |
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/calltomethod.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snippets/calltomethod.ql
4 changes: 4 additions & 0 deletions ql/test/example-tests/snippets/constant.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| main.go:11:18:11:26 | ...-... |
| main.go:15:56:15:59 | zero |
| main.go:35:9:35:9 | 0 |
| main.go:46:11:46:11 | 0 |
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/constant.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snippets/constant.ql
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/emptythen.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| main.go:30:2:31:2 | if statement |
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/emptythen.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snippets/emptythen.ql
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/fieldread.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| main.go:20:5:20:14 | selection of Method |
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/fieldread.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snippets/fieldread.ql
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/fieldwrite.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| main.go:23:3:23:13 | assignment to field Status | main.go:23:17:23:21 | "200" |
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/fieldwrite.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snippets/fieldwrite.ql
2 changes: 2 additions & 0 deletions ql/test/example-tests/snippets/function.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| file://:0:0:0:0 | main |
| main.go:13:6:13:9 | main |
1 change: 1 addition & 0 deletions ql/test/example-tests/snippets/function.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snippets/function.ql
Loading

0 comments on commit 6e4880b

Please sign in to comment.