Skip to content

v4.7.5: Add an opt-in nesting limit for self referencing types

Choose a tag to compare

@eyalz800 eyalz800 released this 09 Aug 21:13
· 2 commits to main since this release
A self referencing type nests as deeply as the message says it does, and
each level costs a stack frame, so a message can ask for more nesting than
the stack has room for. Add `zpp::bits::nesting_limit<N>{}`, which fails
with `std::errc::value_too_large` past `N` levels of nesting.

The option is opt-in and costs nothing when it is not used. The check sits
inside the `concepts::self_referencing` branch that `serialize_one` already
has, so it is never instantiated for a type that cannot nest without bound,
and the depth counter is an empty `[[no_unique_address]]` member unless the
option is present, so `sizeof` of an archive is unchanged. A test pins that
layout.

Both directions are covered, since serializing a deep structure recurses the
same way deserializing one does. Protobuf messages nest through a fresh
archive per level rather than by recursing on a single archive, so the depth
travels with the nested archive and the level itself is counted by
`serialize_one` for the message.

Tests cover a self referencing type at and beyond the limit in both
directions, protobuf nesting at and beyond the limit, an input that nests
far deeper than the stack allows, that nesting is unlimited without the
option, and that plainly nested containers are unaffected.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dn1bSVJQe33FQ7uBGZpewf