Skip to content

Commit 4d797d6

Browse files
authored
Merge pull request #7324 from github/hmac/empty-else-cfg
Ruby: Include empty StmtSequences in CFG
2 parents ee23799 + 6f42153 commit 4d797d6

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,13 @@ module Trees {
932932
class StmtSequenceTree extends PostOrderTree, StmtSequence {
933933
override predicate propagatesAbnormal(AstNode child) { child = this.getAStmt() }
934934

935-
override predicate first(AstNode first) { first(this.getStmt(0), first) }
935+
override predicate first(AstNode first) {
936+
// If this sequence contains any statements, go to the first one.
937+
first(this.getStmt(0), first)
938+
or
939+
// Otherwise, treat this node as a leaf node.
940+
not exists(this.getStmt(0)) and first = this
941+
}
936942

937943
/** Gets the `i`th child in the body of this body statement. */
938944
AstNode getBodyChild(int i, boolean rescuable) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,6 +3675,7 @@ ifs.rb:
36753675
#-----| -> self
36763676

36773677
# 47| b
3678+
#-----| false -> else ...
36783679
#-----| true -> self
36793680

36803681
# 47| then ...
@@ -3689,6 +3690,9 @@ ifs.rb:
36893690
# 48| "true"
36903691
#-----| -> call to puts
36913692

3693+
# 49| else ...
3694+
#-----| -> if ...
3695+
36923696
# 51| call to puts
36933697
#-----| -> exit empty_else (normal)
36943698

@@ -4006,6 +4010,7 @@ loops.rb:
40064010
#-----| -> exit m4 (normal)
40074011

40084012
# 31| ... < ...
4013+
#-----| true -> do ...
40094014
#-----| false -> while ...
40104015

40114016
# 31| x
@@ -4014,6 +4019,9 @@ loops.rb:
40144019
# 31| y
40154020
#-----| -> ... < ...
40164021

4022+
# 31| do ...
4023+
#-----| -> x
4024+
40174025
raise.rb:
40184026
# 1| enter raise.rb
40194027
#-----| -> ExceptionA
@@ -5116,9 +5124,20 @@ raise.rb:
51165124
# 147| [ensure: raise] 3
51175125
#-----| -> [ensure: raise] return
51185126

5127+
# 150| enter m13
5128+
#-----| -> ensure ...
5129+
51195130
# 150| m13
51205131
#-----| -> m14
51215132

5133+
# 150| exit m13
5134+
5135+
# 150| exit m13 (normal)
5136+
#-----| -> exit m13
5137+
5138+
# 151| ensure ...
5139+
#-----| -> exit m13 (normal)
5140+
51225141
# 154| enter m14
51235142
#-----| -> element
51245143

0 commit comments

Comments
 (0)