Skip to content

Commit 4501216

Browse files
authored
Merge pull request #206 from sanket1729/context_none_bugs
Return Err on None in context checks
2 parents adde3ea + bab678b commit 4501216

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

src/miniscript/context.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ pub trait ScriptContext:
195195
ms: &Miniscript<Pk, Ctx>,
196196
) -> Result<(), ScriptContextError> {
197197
Self::check_global_consensus_validity(ms)?;
198-
Self::check_global_consensus_validity(ms)?;
199-
Self::check_local_policy_validity(ms)?;
198+
Self::check_global_policy_validity(ms)?;
199+
Self::check_local_consensus_validity(ms)?;
200200
Self::check_local_policy_validity(ms)?;
201201
Ok(())
202202
}
@@ -277,26 +277,28 @@ impl ScriptContext for Legacy {
277277
fn check_local_consensus_validity<Pk: MiniscriptKey, Ctx: ScriptContext>(
278278
ms: &Miniscript<Pk, Ctx>,
279279
) -> Result<(), ScriptContextError> {
280-
if let Some(op_count) = ms.ext.ops_count_sat {
281-
if op_count > MAX_OPS_PER_SCRIPT {
282-
return Err(ScriptContextError::MaxOpCountExceeded);
280+
match ms.ext.ops_count_sat {
281+
None => Err(ScriptContextError::MaxOpCountExceeded),
282+
Some(op_count) if op_count > MAX_OPS_PER_SCRIPT => {
283+
Err(ScriptContextError::MaxOpCountExceeded)
283284
}
285+
_ => Ok(()),
284286
}
285-
Ok(())
286287
}
287288

288289
fn check_local_policy_validity<Pk: MiniscriptKey, Ctx: ScriptContext>(
289290
ms: &Miniscript<Pk, Ctx>,
290291
) -> Result<(), ScriptContextError> {
291-
if let Some(size) = ms.max_satisfaction_size() {
292-
if size > MAX_SCRIPTSIG_SIZE {
293-
return Err(ScriptContextError::MaxScriptSigSizeExceeded);
294-
}
295-
}
296292
// Legacy scripts permit upto 1000 stack elements, 520 bytes consensus limits
297293
// on P2SH size, it is not possible to reach the 1000 elements limit and hence
298294
// we do not check it.
299-
Ok(())
295+
match ms.max_satisfaction_size() {
296+
None => Err(ScriptContextError::MaxScriptSigSizeExceeded),
297+
Some(size) if size > MAX_SCRIPTSIG_SIZE => {
298+
Err(ScriptContextError::MaxScriptSigSizeExceeded)
299+
}
300+
_ => Ok(()),
301+
}
300302
}
301303

302304
fn max_satisfaction_size<Pk: MiniscriptKey, Ctx: ScriptContext>(
@@ -348,12 +350,13 @@ impl ScriptContext for Segwitv0 {
348350
fn check_local_consensus_validity<Pk: MiniscriptKey, Ctx: ScriptContext>(
349351
ms: &Miniscript<Pk, Ctx>,
350352
) -> Result<(), ScriptContextError> {
351-
if let Some(op_count) = ms.ext.ops_count_sat {
352-
if op_count > MAX_OPS_PER_SCRIPT {
353-
return Err(ScriptContextError::MaxOpCountExceeded);
353+
match ms.ext.ops_count_sat {
354+
None => Err(ScriptContextError::MaxOpCountExceeded),
355+
Some(op_count) if op_count > MAX_OPS_PER_SCRIPT => {
356+
Err(ScriptContextError::MaxOpCountExceeded)
354357
}
358+
_ => Ok(()),
355359
}
356-
Ok(())
357360
}
358361

359362
fn check_global_policy_validity<Pk: MiniscriptKey, Ctx: ScriptContext>(
@@ -371,12 +374,13 @@ impl ScriptContext for Segwitv0 {
371374
// We don't need to know if this is actually a p2wsh as the standard satisfaction for
372375
// other Segwitv0 defined programs all require (much) less than 100 elements.
373376
// The witness script item is accounted for in max_satisfaction_witness_elements().
374-
if let Some(max_witness_items) = ms.max_satisfaction_witness_elements() {
375-
if max_witness_items > MAX_STANDARD_P2WSH_STACK_ITEMS {
376-
return Err(ScriptContextError::MaxWitnessItemssExceeded);
377+
match ms.max_satisfaction_witness_elements() {
378+
None => Err(ScriptContextError::MaxWitnessItemssExceeded),
379+
Some(max_witness_items) if max_witness_items > MAX_STANDARD_P2WSH_STACK_ITEMS => {
380+
Err(ScriptContextError::MaxWitnessItemssExceeded)
377381
}
382+
_ => Ok(()),
378383
}
379-
Ok(())
380384
}
381385

382386
fn max_satisfaction_size<Pk: MiniscriptKey, Ctx: ScriptContext>(
@@ -417,12 +421,13 @@ impl ScriptContext for Bare {
417421
fn check_local_consensus_validity<Pk: MiniscriptKey, Ctx: ScriptContext>(
418422
ms: &Miniscript<Pk, Ctx>,
419423
) -> Result<(), ScriptContextError> {
420-
if let Some(op_count) = ms.ext.ops_count_sat {
421-
if op_count > MAX_OPS_PER_SCRIPT {
422-
return Err(ScriptContextError::MaxOpCountExceeded);
424+
match ms.ext.ops_count_sat {
425+
None => Err(ScriptContextError::MaxOpCountExceeded),
426+
Some(op_count) if op_count > MAX_OPS_PER_SCRIPT => {
427+
Err(ScriptContextError::MaxOpCountExceeded)
423428
}
429+
_ => Ok(()),
424430
}
425-
Ok(())
426431
}
427432

428433
fn other_top_level_checks<Pk: MiniscriptKey, Ctx: ScriptContext>(

src/miniscript/types/extra_props.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -675,21 +675,18 @@ impl Property for ExtData {
675675
.and_then(|z| a.ops_count_nsat.map(|x| x + b.ops_count_static + z + 3)),
676676
stack_elem_count_sat: cmp::max(
677677
a.stack_elem_count_sat
678-
.and_then(|a| c.stack_elem_count_sat.map(|c| c + a)),
678+
.and_then(|a| b.stack_elem_count_sat.map(|b| b + a)),
679679
a.stack_elem_count_dissat
680-
.and_then(|a_dis| b.stack_elem_count_sat.map(|b| b + a_dis)),
681-
),
682-
stack_elem_count_dissat: cmp::max(
683-
a.stack_elem_count_sat
684-
.and_then(|a| c.stack_elem_count_dissat.map(|c| c + a)),
685-
a.stack_elem_count_dissat
686-
.and_then(|a_dis| b.stack_elem_count_dissat.map(|b| b + a_dis)),
680+
.and_then(|a_dis| c.stack_elem_count_sat.map(|c| c + a_dis)),
687681
),
682+
stack_elem_count_dissat: a
683+
.stack_elem_count_dissat
684+
.and_then(|a_dis| c.stack_elem_count_dissat.map(|c| c + a_dis)),
688685
max_sat_size: cmp::max(
689686
a.max_sat_size
690-
.and_then(|(wa, sa)| c.max_sat_size.map(|(wc, sc)| (wa + wc, sa + sc))),
691-
a.max_dissat_size
692687
.and_then(|(wa, sa)| b.max_sat_size.map(|(wb, sb)| (wa + wb, sa + sb))),
688+
a.max_dissat_size
689+
.and_then(|(wa, sa)| c.max_sat_size.map(|(wc, sc)| (wa + wc, sa + sc))),
693690
),
694691
max_dissat_size: a
695692
.max_dissat_size

0 commit comments

Comments
 (0)