Skip to content

Commit

Permalink
Fix while handling
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Sep 26, 2024
1 parent aefcbda commit f0ac2ed
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/analyzer/stmt/while_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions tests/inference/Loop/While/whileTrueWithBreak/input.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function foo(): void {
while (true) {
$a = 5;
if (rand(0, 1)) {
break;
}
}

echo $a;
}
2 changes: 1 addition & 1 deletion tests/security/taintThroughResultGet/input.hack
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ function foo(shape('a' => string) $args): void {
}
}

function get_a_result(shape('a' => string) $args): Result<string> {
function get_a_result(shape('a' => string) $args): Result<string, nothing> {
return new ResultSuccess($args['a']);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ switch (rand(0, 4)) {
$a = 0;
break;
}

// FALLTHROUGH
default:
$a = 1;
}
Expand Down

0 comments on commit f0ac2ed

Please sign in to comment.