#[non_exhaustive]pub struct Config {
pub max_alloc: usize,
}Expand description
Configuration for a decode session.
At construction time the codec validates the configuration; an invalid
config (currently: max_alloc == 0) is rejected before any bytes are read.
Validation happens once, in Decoder::with_config /
crate::IoDecoder::with_config, not on every operation.
Config is #[non_exhaustive] so the project can add knobs in a MINOR
release without breaking downstream code. Build instances with
Config::new / Config::with_max_alloc or via Default.
§Examples
use pack_io::{Config, Decoder};
// Refuse to allocate more than 16 KiB for any single length-prefixed
// value (a `String`, a `Vec<u8>`, a collection element count, …).
// Hostile producers that send multi-gigabyte length prefixes fail fast.
let cfg = Config::new().with_max_alloc(16 * 1024);
let dec = Decoder::with_config(&[], cfg).expect("non-zero cap");
drop(dec);Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.max_alloc: usizeMaximum number of bytes the decoder may allocate for any single
length-prefixed value (a String, a Vec<u8>, a collection element
count, …).
The default is 1 GiB, which is enough that well-formed inputs are never rejected on size, while still defending against the obvious hostile-length-prefix DoS. Tighten this in any context that accepts untrusted input from a low-budget producer.
Implementations§
Source§impl Config
impl Config
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Default configuration: max_alloc = 1 GiB.
1 GiB is large enough to be irrelevant for well-formed inputs and
small enough to refuse the obvious length = u64::MAX attack before
allocating a single byte.
§Examples
let cfg = pack_io::Config::new();
assert_eq!(cfg.max_alloc, 1 << 30);Sourcepub const fn with_max_alloc(self, max_alloc: usize) -> Self
pub const fn with_max_alloc(self, max_alloc: usize) -> Self
Replace max_alloc and return the updated config.
§Examples
let cfg = pack_io::Config::new().with_max_alloc(4096);
assert_eq!(cfg.max_alloc, 4096);