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 46aca51
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
13 changes: 12 additions & 1 deletion assembly/src/assembler/instruction/mem_ops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{push_felt, push_u32_value, validate_param, AssemblyContext, BasicBlockBuilder};
use crate::AssemblyError;
use crate::{diagnostics::Report, AssemblyError};
use alloc::string::ToString;
use vm_core::{Felt, Operation::*};

// INSTRUCTION PARSERS
Expand Down Expand Up @@ -112,6 +113,16 @@ 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(
"number of procedure locals was not set (or set to 0), but local values were used"
.to_string(),
)
.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 46aca51

Please sign in to comment.