Repository navigation
Cap CompactArcStore state count by StateId and validate header counts before narrowing. - #349
Merged
Merged
Conversation
copybara-service
Bot
force-pushed
the
copybara/995337607
branch
2 times, most recently
from
October 7, 2026 22:11
4e1966e to
b17752e
Compare
…unts before narrowing. In `CompactArcStore<Element, Unsigned>` (`lib/compact-fst.h`): 1. `CompactArcCompactor::NumStates()` returns `StateId` (`int32_t` for `StdArc`, max `2^31 - 1`), whereas `kMaxStates` is `1ULL << 52` (`CompactArcStore` is not templated on `Arc`, but its constructors and `Read()` are templated on `ArcCompactor`). 2. In `CompactArcStore::Read`, the 64-bit signed `hdr.NumStates()` and `hdr.NumArcs()` values were assigned to unsigned `size_t` (`data->nstates_` and `data->narcs_`) before checking bounds, making `data->nstates_ < 0` and `data->narcs_ < 0` dead unsigned comparisons. Fix by: - Making `kMaxStates` and `kMaxArcs` public on `CompactArcStore`. - Capping the maximum allowed state count in `CompactArcStore`'s constructors and `Read()` to `std::min<uint64_t>(kMaxStates, std::numeric_limits<StateId>::max())`. - Validating `hdr.NumStates()`, `hdr.Start()`, and `hdr.NumArcs()` as `int64_t` in `CompactArcStore::Read` before assigning them to `data->nstates_`, `data->start_`, and `data->narcs_`. - Expanding `CompactLimitsTest` in `fst_limits_test.cc` to test `std::numeric_limits<StdArc::StateId>::max() + 1` and negative counts. PiperOrigin-RevId: 995395588
copybara-service
Bot
force-pushed
the
copybara/995337607
branch
from
October 7, 2026 22:33
b17752e to
45c78f8
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.
Cap
CompactArcStorestate count byStateIdand validate header counts before narrowing.In
CompactArcStore<Element, Unsigned>(lib/compact-fst.h):CompactArcCompactor::NumStates()returnsStateId(int32_tforStdArc,max
2^31 - 1), whereaskMaxStatesis1ULL << 52(CompactArcStoreisnot templated on
Arc, but its constructors andRead()are templated onArcCompactor).CompactArcStore::Read, the 64-bit signedhdr.NumStates()andhdr.NumArcs()values were assigned to unsignedsize_t(data->nstates_and
data->narcs_) before checking bounds, makingdata->nstates_ < 0anddata->narcs_ < 0dead unsigned comparisons.Fix by:
kMaxStatesandkMaxArcspublic onCompactArcStore.CompactArcStore's constructorsand
Read()tostd::min<uint64_t>(kMaxStates, std::numeric_limits<StateId>::max()).hdr.NumStates(),hdr.Start(), andhdr.NumArcs()asint64_tin
CompactArcStore::Readbefore assigning them todata->nstates_,data->start_, anddata->narcs_.CompactLimitsTestinfst_limits_test.ccto teststd::numeric_limits<StdArc::StateId>::max() + 1and negative counts.