Skip to main content

btrfs_core/
error.rs

1//! Error types for the btrfs reader.
2
3use thiserror::Error;
4
5/// Errors surfaced while parsing btrfs on-disk structures.
6///
7/// Every variant names the offending value so an "unknown/invalid" report hands
8/// the investigator the evidence (raw bytes / offset), never a bare "invalid".
9#[derive(Debug, Error, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum BtrfsError {
12    /// The buffer was too small to hold the structure being parsed.
13    #[error("buffer too small for {structure}: need {need} bytes, have {have}")]
14    Truncated {
15        /// Name of the structure that could not be read.
16        structure: &'static str,
17        /// Minimum byte length required.
18        need: usize,
19        /// Byte length actually available.
20        have: usize,
21    },
22
23    /// The superblock magic did not match `_BHRfS_M` at offset 0x40.
24    ///
25    /// Carries the eight bytes actually found so the caller can identify what the
26    /// image really is (fail-loud with the offending value).
27    #[error("bad btrfs magic: found bytes {bytes:02x?} at superblock offset 0x40, expected 5f42485266535f4d (\"_BHRfS_M\")")]
28    BadMagic {
29        /// The eight raw bytes read at superblock offset 0x40.
30        bytes: [u8; 8],
31    },
32
33    /// A length/size field from the untrusted image demanded an allocation far
34    /// larger than the image itself could justify — a classic allocation-bomb.
35    ///
36    /// Carries the offending value and the bound it exceeded so the report hands
37    /// the investigator the evidence, never a bare "too big" (fail-loud).
38    #[error("allocation bomb: {field} claims {claimed} bytes, exceeding the {bound}-byte bound")]
39    AllocationBomb {
40        /// The name of the length field that overflowed the bound.
41        field: &'static str,
42        /// The absurd byte count the image field claimed.
43        claimed: u64,
44        /// The largest value that could be legitimate (e.g. the image length).
45        bound: u64,
46    },
47}