From 3f31775252c9b73f73912cfe23a4ccd124f9eee0 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Mon, 18 Jan 2021 15:01:41 +0100 Subject: [PATCH 1/2] CFG: Add test for constant condition --- .../controlflow/graph/Cfg.expected | 35 ++++++++++++++++++- .../library-tests/controlflow/graph/ifs.rb | 6 ++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/ql/test/library-tests/controlflow/graph/Cfg.expected b/ql/test/library-tests/controlflow/graph/Cfg.expected index 8c8ea06a75c..ad514265c5e 100644 --- a/ql/test/library-tests/controlflow/graph/Cfg.expected +++ b/ql/test/library-tests/controlflow/graph/Cfg.expected @@ -103,6 +103,9 @@ ifs.rb: # 36| enter conditional_method_def #-----| -> puts +# 40| enter constant_condition +#-----| -> true + loops.rb: # 1| enter top-level #-----| -> m1 @@ -2440,7 +2443,7 @@ ifs.rb: #-----| -> If # 36| UnlessModifier -#-----| -> exit top-level (normal) +#-----| -> constant_condition # 36| Method #-----| -> UnlessModifier @@ -2467,6 +2470,31 @@ ifs.rb: # 38| 2 #-----| -> Binary +# 40| Method +#-----| -> exit top-level (normal) + +# 40| constant_condition +#-----| -> Method + +# 41| If +#-----| -> exit constant_condition (normal) + +# 41| Unary +#-----| false -> If +#-----| true -> puts + +# 41| true +#-----| false -> Unary + +# 42| Call +#-----| -> If + +# 42| puts +#-----| -> String + +# 42| String +#-----| -> Call + loops.rb: # 1| Method #-----| -> m2 @@ -3771,6 +3799,8 @@ ifs.rb: # 36| exit conditional_method_def +# 40| exit constant_condition + loops.rb: # 1| exit top-level @@ -3926,6 +3956,9 @@ ifs.rb: # 36| exit conditional_method_def (normal) #-----| -> exit conditional_method_def +# 40| exit constant_condition (normal) +#-----| -> exit constant_condition + loops.rb: # 1| exit top-level (normal) #-----| -> exit top-level diff --git a/ql/test/library-tests/controlflow/graph/ifs.rb b/ql/test/library-tests/controlflow/graph/ifs.rb index a774f1e8ca6..aa8ac8a342b 100644 --- a/ql/test/library-tests/controlflow/graph/ifs.rb +++ b/ql/test/library-tests/controlflow/graph/ifs.rb @@ -36,3 +36,9 @@ end def conditional_method_def() puts "bla" end unless 1 == 2 + +def constant_condition() + if !true + puts "Impossible" + end +end From 34fe416a8504b7715d53def6497ef59e60947af2 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Mon, 18 Jan 2021 15:03:58 +0100 Subject: [PATCH 2/2] CFG: Fix bug in `LogicalNotTree` --- .../controlflow/internal/ControlFlowGraphImpl.qll | 8 ++------ .../library-tests/controlflow/graph/Cfg.expected | 14 ++------------ 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll b/ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll index 759c710f2d0..39a62fcee08 100644 --- a/ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll +++ b/ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll @@ -782,12 +782,8 @@ module Trees { final override predicate succ(AstNode pred, AstNode succ, Completion c) { succ = this and - ( - last(operand, pred, c.(BooleanCompletion).getDual()) - or - last(operand, pred, c) and - c instanceof SimpleCompletion - ) + last(operand, pred, c) and + c instanceof NormalCompletion } } diff --git a/ql/test/library-tests/controlflow/graph/Cfg.expected b/ql/test/library-tests/controlflow/graph/Cfg.expected index ad514265c5e..43788edcec7 100644 --- a/ql/test/library-tests/controlflow/graph/Cfg.expected +++ b/ql/test/library-tests/controlflow/graph/Cfg.expected @@ -2479,21 +2479,11 @@ ifs.rb: # 41| If #-----| -> exit constant_condition (normal) -# 41| Unary +# 41| [false] Unary #-----| false -> If -#-----| true -> puts # 41| true -#-----| false -> Unary - -# 42| Call -#-----| -> If - -# 42| puts -#-----| -> String - -# 42| String -#-----| -> Call +#-----| true -> [false] Unary loops.rb: # 1| Method