Serialize non-native BEVE map/pair keys via a generic-array fallback - #2669
Open
stephenberry wants to merge 1 commit into
Open
Serialize non-native BEVE map/pair keys via a generic-array fallback#2669stephenberry wants to merge 1 commit into
stephenberry wants to merge 1 commit into
Conversation
…2666) A BEVE object (map) header encodes a single shared key type, so only numeric or string-like keys fit the compact map encoding. Keys that reduce to neither (e.g. a multi-field struct, as in std::unordered_map<user_defined_key, int>) previously failed deep inside the serializers with a cryptic template error. Rather than requiring such keys to be restructured by hand, the map/pair serializers (read/write/size) now fall back to a generic array of [key, value] entries, where each key is written as a full BEVE value. The container round-trips with no user changes. Each entry is encoded identically to std::tuple<Key, Value>, so a non-native std::pair and the matching std::tuple produce the same bytes. Native keys are unaffected and keep the compact shared-header encoding. Covers std::map, std::unordered_map, std::vector<std::pair<Key, Value>>, and a standalone std::pair<Key, Value>. Also bound the element count against the remaining buffer before allocating, on both the native vector-of-pairs reader and the new entry-array readers, mirroring the std::map reader (prevents unbounded emplace_back / OOM on a hostile count). Adds round-trip tests (incl. the issue's unordered_map case and a pair==tuple byte check) and a DoS-protection test for the vector-of-pairs reader. Closes #2666.
stephenberry
force-pushed
the
beve-non-native-key-diagnostics
branch
from
June 22, 2026 18:20
f8ad800 to
df93f7b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A BEVE object (map) header encodes a single shared key type, so only numeric or string-like keys fit the compact map encoding. Keys that reduce to neither (e.g. a multi-field struct, as in
std::unordered_map<user_defined_key, int>) previously failed deep inside the serializers with a cryptic template error.Rather than requiring users to restructure such keys by hand, the map/pair serializers now fall back to a generic array of
[key, value]entries when the key isn't native. Each key is written as a full BEVE value, so the container round-trips with no code changes. Each entry is encoded identically tostd::tuple<Key, Value>(a non-nativestd::pairand the matchingstd::tupleproduce the same bytes). Native (numeric/string) keys are unaffected and keep the compact shared-header encoding.Covers
std::map,std::unordered_map,std::vector<std::pair<Key, Value>>, and a standalonestd::pair<Key, Value>. Documented indocs/binary.md.Hardening
Bound the element count against the remaining buffer before allocating, on both the native vector-of-pairs reader and the new entry-array readers, mirroring the guard the
std::mapreader already has. Previously a hostile count drove unboundedemplace_back(OOM/DoS).Testing
beve_testbuilds clean under-Wall -Wextra -pedanticwith ASan + UBSan.std::unordered_map<CompositeKey, int>case,std::map,std::vector<std::pair>, a standalonestd::pair, an explicit entry struct,std::tuple, and apair == tuplebyte-identity check.Closes #2666.