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

check for stack size before executing the opcode #182

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions src/engine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ pub trait EngineTrait {
fn has_flag(ref self: Engine, flag: ScriptFlags) -> bool;
// Return the script since last OP_CODESEPARATOR
fn sub_script(ref self: Engine) -> ByteArray;

// check the stack size before pushing data
fn check_stack_size(ref self: Engine) -> Result<bool, felt252>;
}

pub impl EngineImpl of EngineTrait {
fn check_stack_size(ref self: Engine) -> Result<bool, felt252> {
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
if self.dstack.len() >= 1000 {
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
return Result::Err(Error::SCRIPT_STACK_SIZE_EXCEEDED);
}
return Result::Ok(true);
}
fn new(
script_pubkey: @ByteArray, transaction: Transaction, tx_idx: u32, flags: u32, amount: i64
) -> Engine {
Expand Down Expand Up @@ -110,11 +119,16 @@ pub impl EngineImpl of EngineTrait {
Opcode::is_opcode_always_illegal(opcode, ref self)?;

if !self.cond_stack.branch_executing() && !flow::is_branching_opcode(opcode) {
// check stack size
self.check_stack_size()?;
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
Opcode::is_opcode_disabled(opcode, ref self)?;
self.opcode_idx += 1;
return Result::Ok(true);
}

// check stack size
self.check_stack_size()?;
b-j-roberts marked this conversation as resolved.
Show resolved Hide resolved

Opcode::execute(opcode, ref self)?;
self.opcode_idx += 1;
return Result::Ok(true);
Expand Down
1 change: 1 addition & 0 deletions src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod Error {
pub const FINALIZED_TX_CLTV: felt252 = 'Finalized tx in OP_CLTV';
pub const INVALID_TX_VERSION: felt252 = 'Invalid transaction version';
pub const SCRIPT_INVALID: felt252 = 'Invalid script data';
pub const SCRIPT_STACK_SIZE_EXCEEDED: felt252 = 'max script stack size exceeded';
}

pub fn byte_array_err(err: felt252) -> ByteArray {
Expand Down