Skip to content
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

fix: ensure ExecutionStack cannot exceed MAX_STACK_SIZE #65

Merged
merged 1 commit into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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