Skip to content

Commit

Permalink
rework bound check ot input/output index
Browse files Browse the repository at this point in the history
  • Loading branch information
biryukovmaxim committed Nov 9, 2024
1 parent 8422331 commit 046308f
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions crypto/txscript/src/opcodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,10 +927,7 @@ opcode_list! {
match vm.script_source {
ScriptSource::TxInput{tx, ..} => {
let [idx]: [i32; 1] = vm.dstack.pop_items()?;
if !(0..=u8::MAX as i32).contains(&idx) {
return Err(TxScriptError::InvalidIndex(idx as usize, tx.inputs().len()))
}
let idx = idx as usize;
let idx = usize::try_from(idx).map_err(|_| TxScriptError::InvalidIndex(idx as usize, tx.inputs().len()))?;
let utxo = tx.utxo(idx).ok_or_else(|| TxScriptError::InvalidIndex(idx, tx.inputs().len()))?;
push_number(utxo.amount as i64, vm)
},
Expand All @@ -945,10 +942,7 @@ opcode_list! {
match vm.script_source {
ScriptSource::TxInput{tx, ..} => {
let [idx]: [i32; 1] = vm.dstack.pop_items()?;
if !(0..=u8::MAX as i32).contains(&idx) {
return Err(TxScriptError::InvalidIndex(idx as usize, tx.inputs().len()))
}
let idx = idx as usize;
let idx = usize::try_from(idx).map_err(|_| TxScriptError::InvalidIndex(idx as usize, tx.inputs().len()))?;
let utxo = tx.utxo(idx).ok_or_else(|| TxScriptError::InvalidIndex(idx, tx.inputs().len()))?;
vm.dstack.push(utxo.script_public_key.to_bytes());
Ok(())
Expand All @@ -967,10 +961,7 @@ opcode_list! {
match vm.script_source {
ScriptSource::TxInput{tx, ..} => {
let [idx]: [i32; 1] = vm.dstack.pop_items()?;
if !(0..=u8::MAX as i32).contains(&idx) {
return Err(TxScriptError::InvalidIndex(idx as usize, tx.inputs().len()))
}
let idx = idx as usize;
let idx = usize::try_from(idx).map_err(|_| TxScriptError::InvalidIndex(idx as usize, tx.inputs().len()))?;
let output = tx.outputs().get(idx).ok_or_else(|| TxScriptError::InvalidOutputIndex(idx, tx.outputs().len()))?;
push_number(output.value as i64, vm)
},
Expand All @@ -985,10 +976,7 @@ opcode_list! {
match vm.script_source {
ScriptSource::TxInput{tx, ..} => {
let [idx]: [i32; 1] = vm.dstack.pop_items()?;
if !(0..=u8::MAX as i32).contains(&idx) {
return Err(TxScriptError::InvalidIndex(idx as usize, tx.inputs().len()))
}
let idx = idx as usize;
let idx = usize::try_from(idx).map_err(|_| TxScriptError::InvalidIndex(idx as usize, tx.inputs().len()))?;
let output = tx.outputs().get(idx).ok_or_else(|| TxScriptError::InvalidOutputIndex(idx, tx.outputs().len()))?;
vm.dstack.push(output.script_public_key.to_bytes());
Ok(())
Expand Down

0 comments on commit 046308f

Please sign in to comment.