Skip to main content

Budget

Struct Budget 

Source
pub struct Budget { /* private fields */ }
Expand description

A per-read traversal budget — an opt-in guard against amplification-DoS on untrusted messages (the wire spec §5.2). Veritate’s offsets are absolute and may alias, so a small hostile message can point many fields at the same large sub-object and make a naive full read do work super-linear in the message’s own size. Memory safety (bounds, depth, allocation) always holds; a Budget additionally caps total work.

Create one per message read and open the root with Message::root_bounded. Every byte a read touches through the buffer is charged against the budget; because aliased sub-objects are charged on every visit, total work is capped regardless of how offsets alias. When exhausted, reads fail with Error::TraversalBudgetExceeded instead of running away. A good starting limit is Message::suggested_budget.

let msg = Message::parse(buf)?;
let budget = Budget::new(msg.suggested_budget());
let root = msg.root_bounded(resolver, &budget)?;
// ... traverse `root`; any amplification trips TraversalBudgetExceeded ...

Implementations§

Source§

impl Budget

Source

pub fn new(limit: u64) -> Self

A budget that allows limit total bytes of buffer access before tripping.

Source

pub fn remaining(&self) -> u64

Bytes still allowed before the budget trips.

Source

pub fn charge(&self, bytes: u64) -> Result<()>

Charge bytes against the budget, returning Error::TraversalBudgetExceeded if it would go negative. Public so generated code (see crate::wire) can charge its own buffer accesses.

Trait Implementations§

Source§

impl Debug for Budget

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Budget

§

impl !RefUnwindSafe for Budget

§

impl !Sync for Budget

§

impl Send for Budget

§

impl Unpin for Budget

§

impl UnsafeUnpin for Budget

§

impl UnwindSafe for Budget

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.