Skip to content

Commit 7ba0682

Browse files
committed
Ruby: Split basic blocks around constant conditionals
1 parent c86f597 commit 7ba0682

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

ruby/ql/lib/codeql/ruby/controlflow/BasicBlocks.qll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,30 @@ private module Cached {
252252
cfn.isJoin()
253253
or
254254
cfn.getAPredecessor().isBranch()
255+
or
256+
/*
257+
* In cases such as
258+
*
259+
* ```rb
260+
* if x or y
261+
* foo
262+
* else
263+
* bar
264+
* ```
265+
*
266+
* we have a CFG that looks like
267+
*
268+
* x --false--> [false] x or y --false--> bar
269+
* \ |
270+
* --true--> y --false--
271+
* \
272+
* --true--> [true] x or y --true--> foo
273+
*
274+
* and we want to ensure that both `foo` and `bar` start a new basic block,
275+
* in order to get a `ConditionalBlock` out of the disjunction.
276+
*/
277+
278+
exists(cfn.getAPredecessor(any(SuccessorTypes::ConditionalSuccessor s)))
255279
}
256280

257281
/**

ruby/ql/test/library-tests/dataflow/barrier-guards/barrier-guards.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,8 @@ controls
4949
| barrier-guards.rb:100:4:100:21 | call to include? | barrier-guards.rb:103:5:103:7 | foo | false |
5050
| barrier-guards.rb:106:4:106:4 | call to x | barrier-guards.rb:106:4:106:9 | [false] ... or ... | false |
5151
| barrier-guards.rb:106:4:106:4 | call to x | barrier-guards.rb:106:9:106:9 | self | false |
52+
| barrier-guards.rb:106:4:106:4 | call to x | barrier-guards.rb:109:5:109:7 | foo | false |
53+
| barrier-guards.rb:106:4:106:9 | [false] ... or ... | barrier-guards.rb:109:5:109:7 | foo | false |
54+
| barrier-guards.rb:106:4:106:9 | [true] ... or ... | barrier-guards.rb:107:5:107:7 | foo | true |
5255
| barrier-guards.rb:106:9:106:9 | call to y | barrier-guards.rb:106:4:106:9 | [false] ... or ... | false |
56+
| barrier-guards.rb:106:9:106:9 | call to y | barrier-guards.rb:109:5:109:7 | foo | false |

0 commit comments

Comments
 (0)