From c46f7689aa937982bfedde693e925898a4dfc819 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 | 10 +++++----- src/script/tari_script.rs | 5 +++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/script/stack.rs b/src/script/stack.rs index fd23b395..3e2277b1 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,24 +250,24 @@ 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 /// maximum allowed stack size, given by [MAX_STACK_SIZE] pub fn push(&mut self, item: StackItem) -> Result<(), ScriptError> { - if self.size() >= MAX_STACK_SIZE { + if self.size() > MAX_STACK_SIZE { return Err(ScriptError::StackOverflow); } self.items.push(item); 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 {