extension PureYAML.Parsing.EventStreamComposer { /// Charges `cost` new expanded nodes against the document's remaining /// ``EventStreamComposer/expandedNodeBudget``, throwing `expansionTooLarge` the moment it /// runs out. /// /// `enforceNestingLimit` (`Parser.Events.swift`) bounds PHYSICAL collection depth in the raw /// event stream; it does nothing against an anchor-expansion ("billion laughs") bomb, where a /// handful of shallow, small anchors are each aliased many times, each alias nested inside the /// next, so the LOGICAL tree an alias resolves to is exponential in the number of alias levels /// while the physical document stays kilobytes -- a 25-level, fan-out-9 document already /// expands past 9^25 logical nodes. /// /// Called once per scalar, alias resolution, or completed container -- never re-derived from /// a walk -- so a bomb is rejected within a few alias levels, long before its true size could /// ever be materialized: each charge is the MARGINAL cost this one node adds, with any nested /// alias expansion it contains already folded into a prior charge (cached in /// ``EventStreamComposer/anchorSizes``) and looked up in O(1), never re-counted here. /// ``EventStreamComposer/expandedNodeBudget``'s initial 5,000,000 is far past any real /// document's node count. mutating func chargeExpansion(_ cost: Int, mark: PureYAML.Parsing.Mark) throws { expandedNodeBudget -= cost guard expandedNodeBudget >= 0 else { throw PureYAML.Parsing.ParseError.expansionTooLarge(line: mark.line, column: mark.column) } } }