From 0a2c2850d254b419b125e970c18a5ba86d27e198 Mon Sep 17 00:00:00 2001 From: Stanimal Date: Tue, 16 Nov 2021 17:53:04 +0400 Subject: [PATCH] fix: ensure ExecutionStack cannot exceed MAX_STACK_SIZE --- src/script/stack.rs | 8 ++++---- src/script/tari_script.rs | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/script/stack.rs b/src/script/stack.rs index fd23b395..b7e44c0a 100644 --- a/src/script/stack.rs +++ b/src/script/stack.rs @@ -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 { @@ -250,18 +250,18 @@ impl ExecutionStack { } pub fn from_bytes(bytes: &[u8]) -> Result { - 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 diff --git a/src/script/tari_script.rs b/src/script/tari_script.rs index 388576ab..556a9e33 100644 --- a/src/script/tari_script.rs +++ b/src/script/tari_script.rs @@ -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 { use Opcode::*; match opcode {