From f0ac2edd5c70a15ca1827af4eb3f794571c58faa Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Thu, 26 Sep 2024 16:50:30 -0700 Subject: [PATCH] Fix while handling --- src/analyzer/stmt/while_analyzer.rs | 18 +++++++++--------- .../Loop/While/whileTrueWithBreak/input.hack | 10 ++++++++++ .../security/taintThroughResultGet/input.hack | 2 +- .../input.hack | 2 +- 4 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 tests/inference/Loop/While/whileTrueWithBreak/input.hack diff --git a/src/analyzer/stmt/while_analyzer.rs b/src/analyzer/stmt/while_analyzer.rs index fc1ce334..36d3cf52 100644 --- a/src/analyzer/stmt/while_analyzer.rs +++ b/src/analyzer/stmt/while_analyzer.rs @@ -30,6 +30,14 @@ pub(crate) fn analyze( let mut loop_scope = LoopScope::new(context.locals.clone()); + let always_enters_loop = if while_true { + true + } else if let Some(stmt_cond_type) = analysis_data.get_expr_type(stmt.0.pos()) { + stmt_cond_type.is_always_truthy() + } else { + false + }; + let inner_loop_context = loop_analyzer::analyze( statements_analyzer, &stmt.1 .0, @@ -40,17 +48,9 @@ pub(crate) fn analyze( context, analysis_data, false, - false, + always_enters_loop, )?; - let always_enters_loop = if while_true { - true - } else if let Some(stmt_cond_type) = analysis_data.get_expr_type(stmt.0.pos()) { - stmt_cond_type.is_always_truthy() - } else { - false - }; - let can_leave_loop = !while_true || loop_scope.final_actions.contains(&ControlAction::Break); if always_enters_loop { diff --git a/tests/inference/Loop/While/whileTrueWithBreak/input.hack b/tests/inference/Loop/While/whileTrueWithBreak/input.hack new file mode 100644 index 00000000..a195cb72 --- /dev/null +++ b/tests/inference/Loop/While/whileTrueWithBreak/input.hack @@ -0,0 +1,10 @@ +function foo(): void { + while (true) { + $a = 5; + if (rand(0, 1)) { + break; + } + } + + echo $a; +} \ No newline at end of file diff --git a/tests/security/taintThroughResultGet/input.hack b/tests/security/taintThroughResultGet/input.hack index 80bb21ba..ed56c556 100644 --- a/tests/security/taintThroughResultGet/input.hack +++ b/tests/security/taintThroughResultGet/input.hack @@ -32,6 +32,6 @@ function foo(shape('a' => string) $args): void { } } -function get_a_result(shape('a' => string) $args): Result { +function get_a_result(shape('a' => string) $args): Result { return new ResultSuccess($args['a']); } \ No newline at end of file diff --git a/tests/unused/UnusedExpression/switchVarConditionalAssignmentWithReference/input.hack b/tests/unused/UnusedExpression/switchVarConditionalAssignmentWithReference/input.hack index f61bd02e..7b30b8db 100644 --- a/tests/unused/UnusedExpression/switchVarConditionalAssignmentWithReference/input.hack +++ b/tests/unused/UnusedExpression/switchVarConditionalAssignmentWithReference/input.hack @@ -4,7 +4,7 @@ switch (rand(0, 4)) { $a = 0; break; } - + // FALLTHROUGH default: $a = 1; }