Repository navigation
Conversation
The integral variant id lookup returned the slot its hash picked without
checking that the id was the one declared there. For sparse ids the
modular and modular_shifted strategies map every integer to some slot,
so with ids {1000, 2000, 3000} a tag of 3001 read as the 3000
alternative and 4242 as the 2000 one, instead of the catch-all
alternative or no_matching_variant_type. Compare the id with ids_v<T>
before returning it, as the string id lookup and get_enum_name do, and
do the offset and small_range subtractions in uint64_t so an INT64_MIN
id no longer overflows.
This branch has not been deployed
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.
variant_id_to_index<T, true>::opininclude/glaze/core/reflect.hppturns the integer tag of a tagged variant into an alternative index, and the JSON, YAML, BEVE, CBOR, MessagePack and JSONB readers accept any index it returns belowids_v<T>.size(). For sparse ids it picks themodularormodular_shiftedhash, which maps every integer to some slot, and it returned that slot without comparing the id with the one declared there. Withids = std::array{1000, 2000, 3000}and a catch-all fourth alternative as in docs/variant-handling.md,{"type":3001}reads as the 3000 alternative and{"type":4242}as the 2000 one; across [-4096, 8192], 4,605 of the 12,286 undeclared ids land on a declared alternative. A router that dispatches on the tag, or a gateway that filters on it, sees a different message type than the one glaze builds.The fix accepts the candidate index only when
ids_v<T>[index] == id, the check the string id specialization directly above andget_enum_namealready make after their hashes. Theoffsetandsmall_rangesubtractions move touint64_t, sinceid - min_valueoverflowed for anINT64_MINid (UBSan at reflect.hpp:3314 and :3340). The check lives in the lookup because all six readers share it and none of them sees the declared ids.Before: an undeclared integer tag that hashes onto a declared id selects that alternative, with a success return.
After: only declared ids select an alternative. Other ids go to the catch-all alternative, or fail with
no_matching_variant_typewhen there is none, as non-colliding ids already did.Tradeoff: one integer compare per tagged variant read. Declared ids resolve the same in every lookup strategy.
Tests: five cases in
variant_default_tests(tests/json_test/variant_ambiguous_test.cpp); three fail on main (13 asserts) and the int64 case reports both overflows under UBSan. Full local ctest 169/169, clang Debug with ASan+UBSan (SSL tests off, no OpenSSL here); clang-format 20 clean.