|
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 | +import codeql.ruby.controlflow.internal.Guards as Guards |
| 17 | +import codeql.ruby.controlflow.CfgNodes |
| 18 | +import codeql.ruby.ast.internal.Variable |
15 | 19 |
|
16 |
| -class RelevantLocalVariableReadAccess extends LocalVariableReadAccess { |
17 |
| - RelevantLocalVariableReadAccess() { |
18 |
| - not exists(MethodCall c | |
19 |
| - c.getReceiver() = this and |
| 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() |
| 31 | + or |
| 32 | + n = any(LogicalAndExpr op).getAnOperand() |
| 33 | + or |
| 34 | + n = any(LogicalOrExpr op).getAnOperand() |
| 35 | + or |
| 36 | + n = any(NotExpr op).getOperand() |
| 37 | + or |
| 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 | + ) |
| 48 | +} |
| 49 | + |
| 50 | +private predicate isGuarded(LocalVariableReadAccess read) { |
| 51 | + exists(AstCfgNode guard, boolean branch | |
| 52 | + Guards::guardControlsBlock(guard, read.getAControlFlowNode().getBasicBlock(), branch) |
| 53 | + | |
| 54 | + // guard is `var` |
| 55 | + guard.getAstNode() = read.getVariable().getAnAccess() and |
| 56 | + branch = true |
| 57 | + or |
| 58 | + // guard is `var.nil?` |
| 59 | + exists(MethodCall c | guard.getAstNode() = c | |
| 60 | + c.getReceiver() = read.getVariable().getAnAccess() and |
20 | 61 | c.getMethodName() = "nil?"
|
21 |
| - ) |
| 62 | + ) and |
| 63 | + branch = false |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +private predicate isNilChecked(LocalVariableReadAccess read) { |
| 68 | + exists(MethodCall c | c.getReceiver() = read | |
| 69 | + c.getMethodName() = "nil?" |
| 70 | + or |
| 71 | + c.isSafeNavigation() |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Holds if `name` is the name of a method defined on `nil`. |
| 77 | + * See https://ruby-doc.org/core-2.5.8/NilClass.html |
| 78 | + */ |
| 79 | +private predicate isNilMethodName(string name) { |
| 80 | + name in [ |
| 81 | + "inspect", "instance_of?", "is_a?", "kind_of?", "method", "nil?", "rationalize", "to_a", |
| 82 | + "to_c", "to_f", "to_h", "to_i", "to_r", "to_s" |
| 83 | + ] |
| 84 | +} |
| 85 | + |
| 86 | +class RelevantLocalVariableReadAccess extends LocalVariableReadAccess instanceof TVariableAccessReal |
| 87 | +{ |
| 88 | + RelevantLocalVariableReadAccess() { |
| 89 | + not isInBooleanContext(this) and |
| 90 | + not isNilChecked(this) and |
| 91 | + not isGuarded(this) and |
| 92 | + this = any(MethodCall m | not isNilMethodName(m.getMethodName())).getReceiver() |
22 | 93 | }
|
23 | 94 | }
|
24 | 95 |
|
|
0 commit comments