Skip to content

Commit

Permalink
fix: add error handling for underflow, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Jul 3, 2024
1 parent 629ea82 commit 037f8e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
11 changes: 10 additions & 1 deletion assembly/src/assembler/instruction/mem_ops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{push_felt, push_u32_value, validate_param, AssemblyContext, BasicBlockBuilder};
use crate::AssemblyError;
use crate::{diagnostics::Report, AssemblyError};
use vm_core::{Felt, Operation::*};

// INSTRUCTION PARSERS
Expand Down Expand Up @@ -112,6 +112,15 @@ pub fn local_to_absolute_addr(
index: u16,
num_proc_locals: u16,
) -> Result<(), AssemblyError> {
if num_proc_locals == 0 {
return Err(AssemblyError::Other(
Report::msg(format!(

Check failure on line 117 in assembly/src/assembler/instruction/mem_ops.rs

View workflow job for this annotation

GitHub Actions / Clippy

useless use of `format!`
"number of procedure locals was not set (or set to 0), but local values were used",
))
.into(),
));
}

let max = num_proc_locals - 1;
validate_param(index, 0..=max)?;

Expand Down
25 changes: 25 additions & 0 deletions assembly/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,31 @@ end";
Ok(())
}

#[test]
fn program_with_proc_locals_fail() -> TestResult {
let mut context = TestContext::default();
let source = source_file!(
"\
proc.foo \
loc_store.0 \
add \
loc_load.0 \
mul \
end \
begin \
push.4 push.3 push.2 \
exec.foo \
end"
);
assert_assembler_diagnostic!(
context,
source,
"number of procedure locals was not set (or set to 0), but local values were used"
);

Ok(())
}

#[test]
fn program_with_exported_procedure() -> TestResult {
let mut context = TestContext::default();
Expand Down

0 comments on commit 037f8e7

Please sign in to comment.