-
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: add loop context validation for break/continue statements #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -119,6 +119,7 @@ pub struct Interpreter { | |
| environment: Rc<RefCell<Environment>>, | ||
| functions: HashMap<String, FunctionDef>, | ||
| control_flow: ControlFlow, | ||
| in_loop_context: bool, // Track whether we're inside a loop | ||
| capabilities: crate::security::CapabilityRegistry, | ||
| pragmas: PragmaSettings, | ||
| worker_defs: HashMap<String, Vec<Statement>>, | ||
|
|
@@ -131,6 +132,7 @@ impl Interpreter { | |
| environment: Rc::new(RefCell::new(Environment::new())), | ||
| functions: HashMap::new(), | ||
| control_flow: ControlFlow::None, | ||
| in_loop_context: false, | ||
| capabilities: crate::security::CapabilityRegistry::new(), | ||
| pragmas: PragmaSettings::default(), | ||
| worker_defs: HashMap::new(), | ||
|
|
@@ -315,6 +317,7 @@ impl Interpreter { | |
| }; | ||
|
|
||
| let mut result = Value::Unit; | ||
| self.in_loop_context = true; | ||
| for _ in 0..iterations { | ||
| for stmt in &loop_stmt.body { | ||
| result = self.execute_statement(stmt)?; | ||
|
|
@@ -332,11 +335,13 @@ impl Interpreter { | |
| } | ||
| } | ||
| } | ||
| self.in_loop_context = false; | ||
| Ok(result) | ||
| } | ||
|
|
||
| Statement::While(while_loop) => { | ||
| let mut result = Value::Unit; | ||
| self.in_loop_context = true; | ||
| loop { | ||
| let condition = self.eval_expr(&while_loop.condition)?; | ||
| if !condition.is_truthy() { | ||
|
|
@@ -359,15 +364,26 @@ impl Interpreter { | |
| } | ||
| } | ||
| } | ||
| self.in_loop_context = false; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new loop-context tracking uses a single boolean and unconditionally writes Useful? React with 👍 / 👎. |
||
| Ok(result) | ||
| } | ||
|
|
||
| Statement::Break(_) => { | ||
| if !self.in_loop_context { | ||
| return Err(RuntimeError::new( | ||
| "break statement used outside of loop context".to_string() | ||
| )); | ||
| } | ||
| self.control_flow = ControlFlow::Break; | ||
| Ok(Value::Unit) | ||
| } | ||
|
|
||
| Statement::Continue(_) => { | ||
| if !self.in_loop_context { | ||
| return Err(RuntimeError::new( | ||
| "continue statement used outside of loop context".to_string() | ||
| )); | ||
| } | ||
| self.control_flow = ControlFlow::Continue; | ||
| Ok(Value::Unit) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in_loop_contextis set totruebefore entering the loop, but it is only cleared on the normal fallthrough path; early exits (break,return, or?-propagated errors) return before cleanup runs. That leaves the interpreter stuck in loop context, so laterbreak/continuestatements outside any loop can incorrectly pass validation. This needs guaranteed restoration across all exit paths (e.g., scoped guard or explicit save/restore).Useful? React with 👍 / 👎.