Skip to content

Commit 946c572

Browse files
committed
QL: Add codeql-ruby sources (236643f)
1 parent d8e1e3e commit 946c572

151 files changed

Lines changed: 42145 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.

repo-tests/codeql-ruby.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
236643fc43b8ae09e15dfa13e86bfdb61a106668
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import codeql.ruby.AST
2+
import codeql.ruby.ast.internal.Synthesis
3+
4+
query predicate missingParent(AstNode node, string cls) {
5+
not exists(node.getParent()) and
6+
node.getLocation().getFile().getExtension() != "erb" and
7+
not node instanceof Toplevel and
8+
cls = node.getPrimaryQlClasses()
9+
}
10+
11+
pragma[noinline]
12+
private AstNode parent(AstNode child, int desugarLevel) {
13+
result = child.getParent() and
14+
desugarLevel = desugarLevel(result)
15+
}
16+
17+
query predicate multipleParents(AstNode node, AstNode parent, string cls) {
18+
parent = node.getParent() and
19+
cls = parent.getPrimaryQlClasses() and
20+
exists(AstNode one, AstNode two, int desugarLevel |
21+
one = parent(node, desugarLevel) and
22+
two = parent(node, desugarLevel) and
23+
one != two
24+
)
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import codeql.ruby.controlflow.internal.ControlFlowGraphImplShared::Consistency
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import codeql.ruby.dataflow.internal.DataFlowImplConsistency::Consistency
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import ruby
2+
import codeql.ruby.dataflow.SSA
3+
import codeql.ruby.controlflow.ControlFlowGraph
4+
5+
query predicate nonUniqueDef(CfgNode read, Ssa::Definition def) {
6+
read = def.getARead() and
7+
exists(Ssa::Definition other | read = other.getARead() and other != def)
8+
}
9+
10+
query predicate readWithoutDef(LocalVariableReadAccess read) {
11+
exists(CfgNode node |
12+
node = read.getAControlFlowNode() and
13+
not node = any(Ssa::Definition def).getARead()
14+
)
15+
}
16+
17+
query predicate deadDef(Ssa::Definition def, LocalVariable v) {
18+
v = def.getSourceVariable() and
19+
not v.isCaptured() and
20+
not exists(def.getARead()) and
21+
not def = any(Ssa::PhiNode phi).getAnInput()
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import codeql.ruby.ast.Variable
2+
3+
query predicate ambiguousVariable(VariableAccess access, Variable variable) {
4+
access.getVariable() = variable and
5+
count(access.getVariable()) > 1
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: codeql/ruby-consistency-queries
2+
version: 0.0.1
3+
dependencies:
4+
codeql/ruby-all: 0.0.1
5+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: codeql/ruby-examples
2+
version: 0.0.2
3+
dependencies:
4+
codeql/ruby-all: ^0.0.2
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
* @id ruby/examples/emptythen
6+
* @tags if
7+
* then
8+
* empty
9+
* conditional
10+
* branch
11+
* statement
12+
*/
13+
14+
import ruby
15+
16+
from IfExpr i
17+
where not exists(i.getThen().getAChild())
18+
select i
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
private import codeql.files.FileSystem
2+
3+
/**
4+
* Returns an appropriately encoded version of a filename `name`
5+
* passed by the VS Code extension in order to coincide with the
6+
* output of `.getFile()` on locatable entities.
7+
*/
8+
cached
9+
File getFileBySourceArchiveName(string name) {
10+
// The name provided for a file in the source archive by the VS Code extension
11+
// has some differences from the absolute path in the database:
12+
// 1. colons are replaced by underscores
13+
// 2. there's a leading slash, even for Windows paths: "C:/foo/bar" ->
14+
// "/C_/foo/bar"
15+
// 3. double slashes in UNC prefixes are replaced with a single slash
16+
// We can handle 2 and 3 together by unconditionally adding a leading slash
17+
// before replacing double slashes.
18+
name = ("/" + result.getAbsolutePath().replaceAll(":", "_")).replaceAll("//", "/")
19+
}

0 commit comments

Comments
 (0)