Skip to content

Commit e5dece2

Browse files
committed
Require nested budget guard use
1 parent 0ed1034 commit e5dece2

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

‎crates/cardano-valkyoth-cbor/src/budget.rs‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ impl DecodeBudgetTracker {
221221
}
222222

223223
/// Active nested decode-budget scope.
224-
#[derive(Debug, Eq, PartialEq)]
224+
#[must_use = "the nesting guard must be held for the entire nested decode"]
225+
#[derive(Debug)]
225226
pub struct NestedBudget<'a> {
226227
tracker: &'a mut DecodeBudgetTracker,
227228
}
@@ -288,8 +289,6 @@ fn error_for(field: BudgetField, max: usize, actual: usize) -> DecodeBudgetError
288289
pub enum DecodeBudgetErrorCategory {
289290
/// The operation exceeded an explicit resource limit.
290291
ResourceExhaustion,
291-
/// The tracker was used in an invalid state.
292-
InvalidState,
293292
}
294293

295294
/// Decode-budget accounting failure.
@@ -338,16 +337,13 @@ pub enum DecodeBudgetError {
338337
/// Actual attempted total.
339338
actual: usize,
340339
},
341-
/// A caller attempted to leave nesting depth zero.
342-
NestingUnderflow,
343340
}
344341

345342
impl DecodeBudgetError {
346343
/// Returns the stable high-level error category.
347344
#[must_use]
348345
pub const fn category(self) -> DecodeBudgetErrorCategory {
349346
match self {
350-
Self::NestingUnderflow => DecodeBudgetErrorCategory::InvalidState,
351347
Self::InputTooLarge { .. }
352348
| Self::NestingTooDeep { .. }
353349
| Self::TooManyItems { .. }
@@ -367,7 +363,6 @@ impl DecodeBudgetError {
367363
Self::TooManyMapEntries { .. } => "cardano.cbor.budget.too_many_map_entries",
368364
Self::AllocationTooLarge { .. } => "cardano.cbor.budget.allocation_too_large",
369365
Self::TooManyValues { .. } => "cardano.cbor.budget.too_many_values",
370-
Self::NestingUnderflow => "cardano.cbor.budget.nesting_underflow",
371366
}
372367
}
373368

@@ -381,7 +376,6 @@ impl DecodeBudgetError {
381376
Self::TooManyMapEntries { .. } => "Cardano CBOR map-entry count exceeds decode budget",
382377
Self::AllocationTooLarge { .. } => "Cardano CBOR allocation bytes exceed decode budget",
383378
Self::TooManyValues { .. } => "Cardano CBOR value count exceeds decode budget",
384-
Self::NestingUnderflow => "Cardano CBOR nesting tracker underflow",
385379
}
386380
}
387381
}
@@ -398,7 +392,6 @@ impl fmt::Display for DecodeBudgetError {
398392
| Self::TooManyValues { max, actual } => {
399393
write!(f, " (max={max}, actual={actual})")
400394
}
401-
Self::NestingUnderflow => Ok(()),
402395
}
403396
}
404397
}

‎crates/cardano-valkyoth-cbor/src/tests.rs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,37 @@ fn nested_guard_tracks_depth_for_scope_lifetime() -> Result<(), DecodeBudgetErro
7070
Ok(())
7171
}
7272

73+
#[test]
74+
fn nested_guard_rejects_excess_depth() -> Result<(), DecodeBudgetError> {
75+
let mut tracker = DecodeBudgetTracker::for_input(BUDGET, &[])?;
76+
let mut first = tracker.nested()?;
77+
let mut second = first.nested()?;
78+
79+
assert!(matches!(
80+
second.nested(),
81+
Err(DecodeBudgetError::NestingTooDeep { max: 2, actual: 3 })
82+
));
83+
assert_eq!(second.nesting_depth(), 2);
84+
Ok(())
85+
}
86+
87+
#[test]
88+
fn nested_guard_restores_depth_after_early_error() -> Result<(), DecodeBudgetError> {
89+
fn fail_while_nested(tracker: &mut DecodeBudgetTracker) -> Result<(), DecodeBudgetError> {
90+
let _nested = tracker.nested()?;
91+
Err(DecodeBudgetError::TooManyValues { max: 0, actual: 1 })
92+
}
93+
94+
let mut tracker = DecodeBudgetTracker::for_input(BUDGET, &[])?;
95+
96+
assert_eq!(
97+
fail_while_nested(&mut tracker),
98+
Err(DecodeBudgetError::TooManyValues { max: 0, actual: 1 })
99+
);
100+
assert_eq!(tracker.nesting_depth(), 0);
101+
Ok(())
102+
}
103+
73104
#[test]
74105
fn tracker_fails_closed_on_items() -> Result<(), DecodeBudgetError> {
75106
let mut tracker = DecodeBudgetTracker::for_input(BUDGET, &[])?;

0 commit comments

Comments
 (0)