Skip to content

Commit 20a3e3a

Browse files
committed
Update library
1 parent 65989ae commit 20a3e3a

File tree

8 files changed

+68
-67
lines changed

8 files changed

+68
-67
lines changed

ruby/ql/lib/codeql/ruby/ast/Method.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,24 @@ class Lambda extends Callable, BodyStmt, TLambda {
252252

253253
/** A block. */
254254
class Block extends Callable, StmtSequence, Scope, TBlock {
255+
/**
256+
* Get a local variable declared by this block.
257+
* For example `local` in `{ | param; local| puts param }`.
258+
*/
259+
LocalVariableWriteAccess getALocalVariable() { result = this.getLocalVariable(_) }
260+
261+
/**
262+
* Gets the `n`th local variable declared by this block.
263+
* For example `local` in `{ | param; local| puts param }`.
264+
*/
265+
LocalVariableWriteAccess getLocalVariable(int n) { none() }
266+
255267
override AstNode getAChild(string pred) {
256268
result = Callable.super.getAChild(pred)
257269
or
258270
result = StmtSequence.super.getAChild(pred)
271+
or
272+
pred = "getLocalVariable" and result = this.getLocalVariable(_)
259273
}
260274
}
261275

@@ -265,6 +279,10 @@ class DoBlock extends Block, BodyStmt, TDoBlock {
265279

266280
DoBlock() { this = TDoBlock(g) }
267281

282+
final override LocalVariableWriteAccess getLocalVariable(int n) {
283+
toGenerated(result) = g.getParameters().getLocals(n)
284+
}
285+
268286
final override Parameter getParameter(int n) {
269287
toGenerated(result) = g.getParameters().getChild(n)
270288
}

ruby/ql/lib/codeql/ruby/ast/internal/AST.qll

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ private module Cached {
138138
TFalseLiteral(Ruby::False g) or
139139
TFile(Ruby::File g) or
140140
TFindPattern(Ruby::FindPattern g) or
141-
TFloatLiteral(Ruby::Float g) { not any(Ruby::Rational r).getChild() = g } or
141+
TFloatLiteral(Ruby::Float g) {
142+
not any(Ruby::Complex r).getChild() = g and not any(Ruby::Rational r).getChild() = g
143+
} or
142144
TForExpr(Ruby::For g) or
143145
TForwardParameter(Ruby::ForwardParameter g) or
144146
TForwardArgument(Ruby::ForwardArgument g) or
@@ -169,7 +171,9 @@ private module Cached {
169171
TInstanceVariableAccessSynth(AST::AstNode parent, int i, AST::InstanceVariable v) {
170172
mkSynthChild(InstanceVariableAccessKind(v), parent, i)
171173
} or
172-
TIntegerLiteralReal(Ruby::Integer g) { not any(Ruby::Rational r).getChild() = g } or
174+
TIntegerLiteralReal(Ruby::Integer g) {
175+
not any(Ruby::Complex r).getChild() = g and not any(Ruby::Rational r).getChild() = g
176+
} or
173177
TIntegerLiteralSynth(AST::AstNode parent, int i, int value) {
174178
mkSynthChild(IntegerLiteralKind(value), parent, i)
175179
} or
@@ -223,7 +227,7 @@ private module Cached {
223227
TRangeLiteralSynth(AST::AstNode parent, int i, boolean inclusive) {
224228
mkSynthChild(RangeLiteralKind(inclusive), parent, i)
225229
} or
226-
TRationalLiteral(Ruby::Rational g) or
230+
TRationalLiteral(Ruby::Rational g) { not any(Ruby::Complex r).getChild() = g } or
227231
TRedoStmt(Ruby::Redo g) or
228232
TRegExpLiteral(Ruby::Regex g) or
229233
TRegExpMatchExpr(Ruby::Binary g) { g instanceof @ruby_binary_equaltilde } or
@@ -248,9 +252,6 @@ private module Cached {
248252
casePattern(g)
249253
)
250254
} or
251-
TScopeResolutionMethodCall(Ruby::ScopeResolution g, Ruby::Identifier i) {
252-
isScopeResolutionMethodCall(g, i)
253-
} or
254255
TSelfReal(Ruby::Self g) or
255256
TSelfSynth(AST::AstNode parent, int i, AST::SelfVariable v) {
256257
mkSynthChild(SelfKind(v), parent, i)
@@ -356,15 +357,15 @@ private module Cached {
356357
TRShiftExprReal or TRangeLiteralReal or TRationalLiteral or TRedoStmt or TRegExpLiteral or
357358
TRegExpMatchExpr or TRegularArrayLiteral or TRegularMethodCall or TRegularStringLiteral or
358359
TRegularSuperCall or TRescueClause or TRescueModifierExpr or TRetryStmt or TReturnStmt or
359-
TScopeResolutionConstantAccess or TScopeResolutionMethodCall or TSelfReal or
360-
TSimpleParameterReal or TSimpleSymbolLiteral or TSingletonClass or TSingletonMethod or
361-
TSpaceshipExpr or TSplatExprReal or TSplatParameter or TStringArrayLiteral or
362-
TStringConcatenation or TStringEscapeSequenceComponent or TStringInterpolationComponent or
363-
TStringTextComponent or TSubExprReal or TSubshellLiteral or TSymbolArrayLiteral or
364-
TTernaryIfExpr or TThen or TTokenConstantAccess or TTokenMethodName or TTokenSuperCall or
365-
TToplevel or TTrueLiteral or TUnaryMinusExpr or TUnaryPlusExpr or TUndefStmt or
366-
TUnlessExpr or TUnlessModifierExpr or TUntilExpr or TUntilModifierExpr or
367-
TReferencePattern or TWhenClause or TWhileExpr or TWhileModifierExpr or TYieldCall;
360+
TScopeResolutionConstantAccess or TSelfReal or TSimpleParameterReal or
361+
TSimpleSymbolLiteral or TSingletonClass or TSingletonMethod or TSpaceshipExpr or
362+
TSplatExprReal or TSplatParameter or TStringArrayLiteral or TStringConcatenation or
363+
TStringEscapeSequenceComponent or TStringInterpolationComponent or TStringTextComponent or
364+
TSubExprReal or TSubshellLiteral or TSymbolArrayLiteral or TTernaryIfExpr or TThen or
365+
TTokenConstantAccess or TTokenMethodName or TTokenSuperCall or TToplevel or TTrueLiteral or
366+
TUnaryMinusExpr or TUnaryPlusExpr or TUndefStmt or TUnlessExpr or TUnlessModifierExpr or
367+
TUntilExpr or TUntilModifierExpr or TReferencePattern or TWhenClause or TWhileExpr or
368+
TWhileModifierExpr or TYieldCall;
368369

369370
class TAstNodeSynth =
370371
TAddExprSynth or TAssignExprSynth or TBitwiseAndExprSynth or TBitwiseOrExprSynth or
@@ -498,7 +499,6 @@ private module Cached {
498499
n = TReturnStmt(result) or
499500
n = TRShiftExprReal(result) or
500501
n = TScopeResolutionConstantAccess(result, _) or
501-
n = TScopeResolutionMethodCall(result, _) or
502502
n = TSelfReal(result) or
503503
n = TSimpleParameterReal(result) or
504504
n = TSimpleSymbolLiteral(result) or
@@ -659,8 +659,8 @@ class TCall = TMethodCall or TYieldCall;
659659
class TCase = TCaseExpr or TCaseMatch;
660660

661661
class TMethodCall =
662-
TMethodCallSynth or TIdentifierMethodCall or TScopeResolutionMethodCall or TRegularMethodCall or
663-
TElementReference or TSuperCall or TUnaryOperation or TBinaryOperation;
662+
TMethodCallSynth or TIdentifierMethodCall or TRegularMethodCall or TElementReference or
663+
TSuperCall or TUnaryOperation or TBinaryOperation;
664664

665665
class TSuperCall = TTokenSuperCall or TRegularSuperCall;
666666

ruby/ql/lib/codeql/ruby/ast/internal/Call.qll

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ predicate isIdentifierMethodCall(Ruby::Identifier g) { vcall(g) and not access(g
77

88
predicate isRegularMethodCall(Ruby::Call g) { not g.getMethod() instanceof Ruby::Super }
99

10-
predicate isScopeResolutionMethodCall(Ruby::ScopeResolution g, Ruby::Identifier i) {
11-
i = g.getName() and
12-
not exists(Ruby::Call c | c.getMethod() = g)
13-
}
14-
1510
abstract class CallImpl extends Expr, TCall {
1611
abstract AstNode getArgumentImpl(int n);
1712

@@ -69,23 +64,6 @@ class IdentifierMethodCall extends MethodCallImpl, TIdentifierMethodCall {
6964
final override Block getBlockImpl() { none() }
7065
}
7166

72-
class ScopeResolutionMethodCall extends MethodCallImpl, TScopeResolutionMethodCall {
73-
private Ruby::ScopeResolution g;
74-
private Ruby::Identifier i;
75-
76-
ScopeResolutionMethodCall() { this = TScopeResolutionMethodCall(g, i) }
77-
78-
final override string getMethodNameImpl() { result = i.getValue() }
79-
80-
final override Expr getReceiverImpl() { toGenerated(result) = g.getScope() }
81-
82-
final override Expr getArgumentImpl(int n) { none() }
83-
84-
final override int getNumberOfArgumentsImpl() { result = 0 }
85-
86-
final override Block getBlockImpl() { none() }
87-
}
88-
8967
class RegularMethodCall extends MethodCallImpl, TRegularMethodCall {
9068
private Ruby::Call g;
9169

@@ -94,33 +72,21 @@ class RegularMethodCall extends MethodCallImpl, TRegularMethodCall {
9472
final override Expr getReceiverImpl() {
9573
toGenerated(result) = g.getReceiver()
9674
or
97-
not exists(g.getReceiver()) and
98-
toGenerated(result) = g.getMethod().(Ruby::ScopeResolution).getScope()
99-
or
10075
result = TSelfSynth(this, 0, _)
10176
}
10277

10378
final override string getMethodNameImpl() {
10479
isRegularMethodCall(g) and
10580
(
106-
result = "call" and g.getMethod() instanceof Ruby::ArgumentList
81+
result = "call" and not exists(g.getMethod())
10782
or
10883
result = g.getMethod().(Ruby::Token).getValue()
109-
or
110-
result = g.getMethod().(Ruby::ScopeResolution).getName().(Ruby::Token).getValue()
11184
)
11285
}
11386

114-
final override Expr getArgumentImpl(int n) {
115-
toGenerated(result) = g.getArguments().getChild(n)
116-
or
117-
toGenerated(result) = g.getMethod().(Ruby::ArgumentList).getChild(n)
118-
}
87+
final override Expr getArgumentImpl(int n) { toGenerated(result) = g.getArguments().getChild(n) }
11988

120-
final override int getNumberOfArgumentsImpl() {
121-
result =
122-
count(g.getArguments().getChild(_)) + count(g.getMethod().(Ruby::ArgumentList).getChild(_))
123-
}
89+
final override int getNumberOfArgumentsImpl() { result = count(g.getArguments().getChild(_)) }
12490

12591
final override Block getBlockImpl() { toGenerated(result) = g.getBlock() }
12692
}

ruby/ql/lib/codeql/ruby/ast/internal/Literal.qll

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ class RationalLiteralImpl extends Expr, TRationalLiteral {
8888
}
8989

9090
float getComplexValue(Ruby::Complex c) {
91+
exists(int n, int d | isRationalValue(c.getChild(), n, d) and result = n.(float) / d.(float))
92+
or
9193
exists(string s |
92-
s = c.getValue() and
93-
result = s.prefix(s.length() - 1).toFloat()
94+
s = c.getChild().(Ruby::Token).getValue() and
95+
result = s.prefix(s.length()).toFloat()
9496
)
9597
}
9698

@@ -103,7 +105,10 @@ class ComplexLiteralImpl extends Expr, TComplexLiteral {
103105
real = 0 and imaginary = getComplexValue(g)
104106
}
105107

106-
final override string toString() { result = g.getValue() }
108+
final override string toString() {
109+
result = g.getChild().(Ruby::Token).getValue() + "i" or
110+
result = g.getChild().(Ruby::Rational).getChild().(Ruby::Token).getValue() + "ri"
111+
}
107112
}
108113

109114
class NilLiteralImpl extends Expr, TNilLiteral {

ruby/ql/lib/codeql/ruby/ast/internal/Method.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class BraceBlockReal extends BraceBlock, TBraceBlockReal {
77

88
BraceBlockReal() { this = TBraceBlockReal(g) }
99

10+
final override LocalVariableWriteAccess getLocalVariable(int n) {
11+
toGenerated(result) = g.getParameters().getLocals(n)
12+
}
13+
1014
final override Parameter getParameter(int n) {
1115
toGenerated(result) = g.getParameters().getChild(n)
1216
}

ruby/ql/lib/codeql/ruby/ast/internal/Synthesis.qll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,8 @@ private module ImplicitSelfSynthesis {
197197
private predicate regularMethodCallSelfSynthesis(TRegularMethodCall mc, int i, Child child) {
198198
exists(Ruby::AstNode g |
199199
mc = TRegularMethodCall(g) and
200-
// If there's no explicit receiver (or scope resolution that acts like a
201-
// receiver), then the receiver is implicitly `self`. N.B. `::Foo()` is
202-
// not valid Ruby.
203-
not exists(g.(Ruby::Call).getReceiver()) and
204-
not exists(g.(Ruby::Call).getMethod().(Ruby::ScopeResolution).getScope())
200+
// If there's no explicit receiver, then the receiver is implicitly `self`.
201+
not exists(g.(Ruby::Call).getReceiver())
205202
) and
206203
child = SynthChild(SelfKind(TSelfVariable(scopeOf(toGenerated(mc)).getEnclosingSelfScope()))) and
207204
i = 0

ruby/ql/lib/codeql/ruby/ast/internal/Variable.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ predicate implicitAssignmentNode(Ruby::AstNode n) {
5050

5151
/** Holds if `n` is inside a parameter. */
5252
predicate implicitParameterAssignmentNode(Ruby::AstNode n, Callable::Range c) {
53-
n = c.getParameter(_)
54-
or
53+
n = c.getParameter(_) or
54+
n = c.(Ruby::Block).getParameters().getLocals(_) or
55+
n = c.(Ruby::DoBlock).getParameters().getLocals(_) or
5556
implicitParameterAssignmentNode(n.getParent().(Ruby::DestructuredParameter), c)
5657
}
5758

ruby/ql/lib/codeql/ruby/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,12 @@ module Trees {
347347
final override AstNode getBodyChild(int i, boolean rescuable) {
348348
result = this.getParameter(i) and rescuable = false
349349
or
350-
result = StmtSequenceTree.super.getBodyChild(i - this.getNumberOfParameters(), rescuable)
350+
result = this.getLocalVariable(i - this.getNumberOfParameters()) and rescuable = false
351+
or
352+
result =
353+
StmtSequenceTree.super
354+
.getBodyChild(i - this.getNumberOfParameters() - count(this.getALocalVariable()),
355+
rescuable)
351356
}
352357

353358
override predicate first(AstNode first) { first = this }
@@ -959,7 +964,12 @@ module Trees {
959964
final override AstNode getBodyChild(int i, boolean rescuable) {
960965
result = this.getParameter(i) and rescuable = false
961966
or
962-
result = BodyStmtTree.super.getBodyChild(i - this.getNumberOfParameters(), rescuable)
967+
result = this.getLocalVariable(i - this.getNumberOfParameters()) and rescuable = false
968+
or
969+
result =
970+
BodyStmtTree.super
971+
.getBodyChild(i - this.getNumberOfParameters() - count(this.getALocalVariable()),
972+
rescuable)
963973
}
964974

965975
override predicate propagatesAbnormal(AstNode child) { none() }

0 commit comments

Comments
 (0)