Skip to content

Commit 4167e96

Browse files
yoffhvitved
andcommitted
ruby: more complete impleemntation of isInBooleanContext
Co-authored-by: Tom Hvitved <[email protected]>
1 parent f675a14 commit 4167e96

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

ruby/ql/src/queries/variables/UninitializedLocal.ql

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,37 @@ private import codeql.ruby.dataflow.internal.DataFlowPublic
1717
import codeql.ruby.controlflow.internal.Guards as Guards
1818
import codeql.ruby.controlflow.CfgNodes
1919

20-
predicate isInBooleanContext(Expr e) {
21-
e = any(ConditionalExpr c).getCondition()
20+
private predicate isInBooleanContext(AstNode n) {
21+
exists(ConditionalExpr i |
22+
n = i.getCondition()
23+
or
24+
isInBooleanContext(i) and
25+
n = i.getBranch(_)
26+
)
27+
or
28+
n = any(ConditionalLoop parent).getCondition()
29+
or
30+
n = any(InClause parent).getCondition()
2231
or
23-
e = any(ConditionalLoop l).getCondition()
32+
n = any(LogicalAndExpr op).getAnOperand()
2433
or
25-
e = any(LogicalAndExpr n).getAnOperand()
34+
n = any(LogicalOrExpr op).getAnOperand()
2635
or
27-
e = any(LogicalOrExpr n).getAnOperand()
36+
n = any(NotExpr op).getOperand()
2837
or
29-
e = any(NotExpr n).getOperand()
38+
n = any(StmtSequence parent | isInBooleanContext(parent)).getLastStmt()
39+
or
40+
exists(CaseExpr c, WhenClause w |
41+
not exists(c.getValue()) and
42+
c.getABranch() = w
43+
|
44+
w.getPattern(_) = n
45+
or
46+
w = n
47+
)
3048
}
3149

32-
predicate isGuarded(LocalVariableReadAccess read) {
50+
private predicate isGuarded(LocalVariableReadAccess read) {
3351
exists(AstCfgNode guard, boolean branch |
3452
Guards::guardControlsBlock(guard, read.getAControlFlowNode().getBasicBlock(), branch)
3553
|
@@ -46,7 +64,7 @@ predicate isGuarded(LocalVariableReadAccess read) {
4664
)
4765
}
4866

49-
predicate isNilChecked(LocalVariableReadAccess read) {
67+
private predicate isNilChecked(LocalVariableReadAccess read) {
5068
exists(MethodCall c | c.getReceiver() = read |
5169
c.getMethodName() = "nil?"
5270
or

0 commit comments

Comments
 (0)