Skip to main content

einvoice_xpath_eval/
limit.rs

1//! Limits that keep the processing of a document within a bounded call stack.
2//!
3//! Walking a tree recursively needs stack in proportion to its depth: without a bound, a
4//! small document nested deeply enough would overflow the stack of its thread, which
5//! aborts the whole process. A parsed document is therefore refused when an element is
6//! nested deeper than a maximum depth ([`DEFAULT_MAX_DEPTH`] unless the caller sets
7//! another one), with a [`LimitExceeded::Depth`] error.
8
9use std::fmt;
10
11/// The maximum element depth of a parsed document, unless the caller sets another one:
12/// the document element is at depth 1, its child elements at depth 2, and so on.
13///
14/// 256, the value of libxml2's `xmlParserMaxDepth` (without `XML_PARSE_HUGE`). libxml2
15/// compares it with the number of open ancestors of an element, so `xmllint` accepts 257
16/// nested elements and refuses 258 ("Excessive depth in document: 256").
17pub const DEFAULT_MAX_DEPTH: u32 = 256;
18
19/// A limit that a document or a transformation exceeded: the input is refused rather
20/// than processed with an unbounded call stack.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22#[non_exhaustive]
23pub enum LimitExceeded {
24    /// An element is nested deeper than `max` elements (the document element is at
25    /// depth 1); `line` and `column` (1-based) are the start of its start tag.
26    Depth {
27        /// The maximum depth.
28        max: u32,
29        /// The line of the element's start tag.
30        line: u32,
31        /// The column of the element's start tag, in characters.
32        column: u32,
33    },
34    /// Template rules, built-in template rules, named templates and stylesheet functions
35    /// are invoked one inside another more than `max` deep.
36    Nesting {
37        /// The maximum nesting.
38        max: u32,
39    },
40    /// The invocations in progress use more than `max` bytes of the call stack, measured
41    /// from the start of the transformation: a stylesheet whose templates or functions
42    /// need more stack per level than the nesting allows for.
43    Stack {
44        /// The maximum, in bytes.
45        max: u32,
46    },
47}
48
49impl LimitExceeded {
50    /// The limit's value (bytes for [`LimitExceeded::Stack`]).
51    #[must_use]
52    pub const fn max(self) -> u32 {
53        match self {
54            Self::Depth { max, .. } | Self::Nesting { max } | Self::Stack { max } => max,
55        }
56    }
57}
58
59impl fmt::Display for LimitExceeded {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match self {
62            Self::Depth { max, line, column } => write!(
63                f,
64                "{line}:{column}: element nested deeper than the maximum depth of {max}"
65            ),
66            Self::Nesting { max } => write!(
67                f,
68                "templates and functions invoked one inside another more than {max} deep"
69            ),
70            Self::Stack { max } => write!(
71                f,
72                "templates and functions invoked one inside another use more than {max} bytes of stack"
73            ),
74        }
75    }
76}