|
5 | 5 | * @kind problem
|
6 | 6 | * @problem.severity error
|
7 | 7 | * @id rb/uninitialized-local-variable
|
8 |
| - * @tags reliability |
| 8 | + * @tags quality |
| 9 | + * reliability |
9 | 10 | * correctness
|
10 |
| - * @precision low |
| 11 | + * @precision high |
11 | 12 | */
|
12 | 13 |
|
13 | 14 | import codeql.ruby.AST
|
14 | 15 | import codeql.ruby.dataflow.SSA
|
| 16 | +private import codeql.ruby.dataflow.internal.DataFlowPublic |
| 17 | + |
| 18 | +predicate factor(Expr e, Expr factor) { |
| 19 | + factor = e |
| 20 | + or |
| 21 | + e.(BinaryOperation).getOperator() = ["||", "&&"] and |
| 22 | + factor = e.(BinaryOperation).getAnOperand() |
| 23 | + or |
| 24 | + e.(BinaryOperation).getOperator() = ["||", "&&"] and |
| 25 | + factor(e.(BinaryOperation).getAnOperand(), factor) |
| 26 | +} |
| 27 | + |
| 28 | +predicate previousConjunct(Expr e, Expr prev) { |
| 29 | + exists(BinaryOperation b | |
| 30 | + b.getOperator() = "&&" and |
| 31 | + b.getRightOperand() = e |
| 32 | + | |
| 33 | + // 'prev' && 'e' |
| 34 | + prev = b.getLeftOperand() |
| 35 | + or |
| 36 | + // (... && 'prev') && 'e' |
| 37 | + b.getLeftOperand().(BinaryOperation).getOperator() = "&&" and |
| 38 | + prev = b.getLeftOperand().(BinaryOperation).getRightOperand() |
| 39 | + or |
| 40 | + // (subtree['prev'] && _) && 'e' |
| 41 | + b.getLeftOperand().(BinaryOperation).getOperator() = "&&" and |
| 42 | + previousConjunct(b.getLeftOperand().(BinaryOperation).getRightOperand(), prev) |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +Expr evaluatingMention(LocalVariableReadAccess read) { |
| 47 | + result = read |
| 48 | + or |
| 49 | + result.(AssignExpr).getLeftOperand() = read |
| 50 | + or |
| 51 | + result.(NotExpr).getOperand() = read |
| 52 | +} |
15 | 53 |
|
16 | 54 | class RelevantLocalVariableReadAccess extends LocalVariableReadAccess {
|
17 | 55 | RelevantLocalVariableReadAccess() {
|
18 | 56 | not exists(MethodCall c |
|
19 | 57 | c.getReceiver() = this and
|
20 | 58 | c.getMethodName() = "nil?"
|
| 59 | + ) and |
| 60 | + not exists(MethodCall c | |
| 61 | + c.getReceiver() = this and |
| 62 | + c.isSafeNavigation() |
| 63 | + ) and |
| 64 | + // 'a' is fine to be uninitialised in 'a || ...' |
| 65 | + not exists(BinaryOperation b | |
| 66 | + b.getLeftOperand() = this and |
| 67 | + b.getOperator() = "||" |
| 68 | + ) and |
| 69 | + // The second 'a' cannot be uninitialised in 'a && (...a...)' |
| 70 | + not exists(Expr parent | |
| 71 | + parent.getAChild*() = this and |
| 72 | + previousConjunct(parent, this.getVariable().getAnAccess()) |
| 73 | + ) and |
| 74 | + // Various guards |
| 75 | + not exists(ConditionalExpr c | factor(c.getCondition(), evaluatingMention(this))) and |
| 76 | + not exists(ConditionalExpr c | factor(c.getCondition(), this.getVariable().getAnAccess()) | |
| 77 | + this = c.getBranch(true).getAChild*() |
| 78 | + ) and |
| 79 | + not exists(ConditionalLoop l | |
| 80 | + l.getCondition() = this.getVariable().getAnAccess() and |
| 81 | + l.entersLoopWhenConditionIs(false) and |
| 82 | + l.getAControlFlowNode().getBasicBlock().dominates(this.getAControlFlowNode().getBasicBlock()) |
21 | 83 | )
|
22 | 84 | }
|
23 | 85 | }
|
|
0 commit comments