Skip to content

Commit a080f49

Browse files
committed
Ruby: fix CFG and toString for anonymous '*' and '**'
1 parent 4ff85d5 commit a080f49

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ class HashSplatParameter extends NamedParameter, THashSplatParameter {
183183

184184
final override LocalVariable getVariable() { result = TLocalVariableReal(_, _, g.getName()) }
185185

186-
final override string toString() { result = "**" + this.getName() }
186+
final override string toString() {
187+
result = "**" + this.getName()
188+
or
189+
not exists(g.getName()) and result = "**"
190+
}
187191

188192
final override string getName() { result = g.getName().getValue() }
189193
}
@@ -305,7 +309,11 @@ class SplatParameter extends NamedParameter, TSplatParameter {
305309

306310
final override LocalVariable getVariable() { result = TLocalVariableReal(_, _, g.getName()) }
307311

308-
final override string toString() { result = "*" + this.getName() }
312+
final override string toString() {
313+
result = "*" + this.getName()
314+
or
315+
not exists(g.getName()) and result = "*"
316+
}
309317

310318
final override string getName() { result = g.getName().getValue() }
311319
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,16 @@ module Trees {
139139
abstract private class NonDefaultValueParameterTree extends ControlFlowTree, NamedParameter {
140140
final override predicate first(AstNode first) {
141141
this.getDefiningAccess().(ControlFlowTree).first(first)
142+
or
143+
not exists(this.getDefiningAccess()) and first = this
142144
}
143145

144146
final override predicate last(AstNode last, Completion c) {
145147
this.getDefiningAccess().(ControlFlowTree).last(last, c)
148+
or
149+
not exists(this.getDefiningAccess()) and
150+
last = this and
151+
c.isValidFor(this)
146152
}
147153

148154
override predicate propagatesAbnormal(AstNode child) {

ruby/ql/test/library-tests/controlflow/graph/Cfg.expected

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3719,9 +3719,12 @@ cfg.rb:
37193719
#-----| -> exit do ... end
37203720

37213721
# 208| elem
3722-
#-----| -> rest
3722+
#-----| -> *
37233723

3724-
# 208| rest
3724+
# 208| *
3725+
#-----| -> **
3726+
3727+
# 208| **
37253728
#-----| -> elem
37263729

37273730
# 209| elem

ruby/ql/test/library-tests/controlflow/graph/cfg.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def forward_param(a, b, ...)
205205
foo&.bar(1,2) { |x| x }
206206

207207
def filter_nil list
208-
list.reject do |elem, *rest|
208+
list.reject do |elem, *, **|
209209
elem.nil?
210210
end
211211
end

0 commit comments

Comments
 (0)