Skip to content

Commit

Permalink
fix: ensure ExecutionStack cannot exceed MAX_STACK_SIZE (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi authored Mar 7, 2022
1 parent e4ec3af commit 1b74d94
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/script/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use tari_utilities::{
hex::{from_hex, to_hex, Hex, HexError},
ByteArray,
};
pub const MAX_STACK_SIZE: usize = 256;
pub const MAX_STACK_SIZE: usize = 255;

#[macro_export]
macro_rules! inputs {
Expand Down Expand Up @@ -250,18 +250,18 @@ impl ExecutionStack {
}

pub fn from_bytes(bytes: &[u8]) -> Result<Self, ScriptError> {
let mut items = Vec::new();
let mut stack = ExecutionStack { items: Vec::new() };
let mut byte_str = bytes;
while !byte_str.is_empty() {
match StackItem::read_next(byte_str) {
Some((item, b)) => {
items.push(item);
stack.push(item)?;
byte_str = b;
},
None => return Err(ScriptError::InvalidInput),
}
}
Ok(ExecutionStack { items })
Ok(stack)
}

/// Pushes the item onto the top of the stack. This function will only error if the new stack size exceeds the
Expand Down
5 changes: 5 additions & 0 deletions src/script/tari_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ impl TariScript {
}
}

/// Returns the number of script op codes
pub fn size(&self) -> usize {
self.script.len()
}

fn should_execute(&self, opcode: &Opcode, state: &ExecutionState) -> Result<bool, ScriptError> {
use Opcode::*;
match opcode {
Expand Down

0 comments on commit 1b74d94

Please sign in to comment.