Repository navigation
Cap VectorFstImpl::kMaxStates by StateId and validate start state before narrowing. - #350
Merged
Merged
Conversation
copybara-service
Bot
force-pushed
the
copybara/995386187
branch
2 times, most recently
from
October 7, 2026 22:48
f8ebf1b to
72e8c16
Compare
… before narrowing. In `VectorFstImpl<S>` (`lib/vector-fst.h`): 1. `StateId` (`typename S::StateId`) is `int32_t` for `StdArc` (max `2^31 - 1`), whereas `kMaxStates` was `1LL << 52`. If `hdr.NumStates()` (or the actual stream state count when `hdr.NumStates() == kNoStateId`) exceeded `std::numeric_limits<StateId>::max()`, the `StateId state` loop counter in `Read()` would overflow signed `StateId`. 2. In `VectorFstImpl::Read`, `hdr.Start()` (`int64_t`) was passed to `impl->BaseImpl::SetStart(hdr.Start())` (`StateId`) before the post-loop start-state range check, so a 64-bit value such as `1LL << 32` truncated to `0` in 32-bit `StateId` and passed validation on any non-empty FST. Fix by: - Capping `VectorFstImpl::kMaxStates` at `std::min<int64_t>(0x10000000000000LL, std::numeric_limits<StateId>::max())`. - Validating `hdr.Start()` as `int64_t` in `VectorFstImpl::Read` before calling `SetStart`, and guarding `state == kMaxStates` in the state-reading loop when `hdr.NumStates() == kNoStateId`. - Expanding `VectorLimitsTest` in `fst_limits_test.cc` to cover 64-bit state counts and start states that truncate into 32-bit `StateId`. PiperOrigin-RevId: 995416751
copybara-service
Bot
force-pushed
the
copybara/995386187
branch
from
October 7, 2026 23:07
72e8c16 to
3d6e086
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
VectorFstImpl::kMaxStatesbyStateIdand validate start state before narrowing.In
VectorFstImpl<S>(lib/vector-fst.h):StateId(typename S::StateId) isint32_tforStdArc(max2^31 - 1),whereas
kMaxStateswas1LL << 52. Ifhdr.NumStates()(or the actualstream state count when
hdr.NumStates() == kNoStateId) exceededstd::numeric_limits<StateId>::max(), theStateId stateloop counter inRead()would overflow signedStateId.VectorFstImpl::Read,hdr.Start()(int64_t) was passed toimpl->BaseImpl::SetStart(hdr.Start())(StateId) before the post-loopstart-state range check, so a 64-bit value such as
1LL << 32truncated to0in 32-bitStateIdand passed validation on any non-empty FST.Fix by:
VectorFstImpl::kMaxStatesatstd::min<int64_t>(0x10000000000000LL, std::numeric_limits<StateId>::max()).hdr.Start()asint64_tinVectorFstImpl::Readbefore callingSetStart, and guardingstate == kMaxStatesin the state-reading loop whenhdr.NumStates() == kNoStateId.VectorLimitsTestinfst_limits_test.ccto cover 64-bit statecounts and start states that truncate into 32-bit
StateId.