Skip to content

reject undeclared integer ids in variant_id_to_index - #3131

Open
uwezkhan wants to merge 1 commit into
stephenberry:mainfrom
uwezkhan:variant-int-id-verify
Open

uwezkhan wants to merge 1 commit into
stephenberry:mainfrom
uwezkhan:variant-int-id-verify

Conversation

@uwezkhan

Copy link
Copy Markdown
Contributor

variant_id_to_index<T, true>::op in include/glaze/core/reflect.hpp turns 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 below ids_v<T>.size(). For sparse ids it picks the modular or modular_shifted hash, which maps every integer to some slot, and it returned that slot without comparing the id with the one declared there. With ids = 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 and get_enum_name already make after their hashes. The offset and small_range subtractions move to uint64_t, since id - min_value overflowed for an INT64_MIN id (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_type when 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.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant