Repository navigation
Cap ConstFstImpl::kMaxStates by StateId and validate header counts before narrowing. - #348
Merged
Merged
Conversation
copybara-service
Bot
force-pushed
the
copybara/995324513
branch
2 times, most recently
from
October 7, 2026 20:59
33cc626 to
84b45c4
Compare
…s before narrowing. In `ConstFstImpl<Arc, Unsigned>` (`lib/const-fst.h`): 1. `nstates_` and `start_` have type `StateId` (`int32_t` for `StdArc`, max `2^31 - 1`), whereas `kMaxStates` was `1ULL << 52`. 2. In `ConstFstImpl::Read`, the 64-bit `hdr.NumStates()`, `hdr.Start()`, and `hdr.NumArcs()` values were assigned to `impl->nstates_` (`StateId`), `impl->start_` (`StateId`), and `impl->narcs_` (`size_t`) before checking bounds. As a result, a 64-bit `hdr.NumStates()` or `hdr.Start()` such as `(1LL << 32) + 1` or `1LL << 32` silently truncated to `1` or `0` in 32-bit `StateId`, bypassing the range checks (`impl->nstates_ > kMaxStates` was tautologically false for 32-bit `StateId`), and `impl->narcs_ < 0` was an unsigned comparison. Fix by: - Making `kMaxStates` and `kMaxArcs` public on `ConstFstImpl` and capping `kMaxStates` at `std::min<uint64_t>(0x10000000000000ull, std::numeric_limits<StateId>::max())`. - Counting states in `uint64_t` in `ConstFstImpl(const Fst<Arc>&)` and checking `nstates > kMaxStates` before narrowing to `nstates_`. - Validating `hdr.NumStates()`, `hdr.Start()`, and `hdr.NumArcs()` as `int64_t` in `ConstFstImpl::Read` before assigning them to `impl->nstates_`, `impl->start_`, and `impl->narcs_`. - Expanding `ConstLimitsTest` in `fst_limits_test.cc` to test 64-bit values that truncate into 32-bit `StateId` against non-empty state streams. PiperOrigin-RevId: 995363968
copybara-service
Bot
force-pushed
the
copybara/995324513
branch
from
October 7, 2026 21:40
84b45c4 to
3ac88d4
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
ConstFstImpl::kMaxStatesbyStateIdand validate header counts before narrowing.In
ConstFstImpl<Arc, Unsigned>(lib/const-fst.h):nstates_andstart_have typeStateId(int32_tforStdArc, max2^31 - 1), whereaskMaxStateswas1ULL << 52.ConstFstImpl::Read, the 64-bithdr.NumStates(),hdr.Start(), andhdr.NumArcs()values were assigned toimpl->nstates_(StateId),impl->start_(StateId), andimpl->narcs_(size_t) before checkingbounds. As a result, a 64-bit
hdr.NumStates()orhdr.Start()such as(1LL << 32) + 1or1LL << 32silently truncated to1or0in 32-bitStateId, bypassing the range checks (impl->nstates_ > kMaxStateswastautologically false for 32-bit
StateId), andimpl->narcs_ < 0was anunsigned comparison.
Fix by:
kMaxStatesandkMaxArcspublic onConstFstImpland cappingkMaxStatesatstd::min<uint64_t>(0x10000000000000ull, std::numeric_limits<StateId>::max()).uint64_tinConstFstImpl(const Fst<Arc>&)and checkingnstates > kMaxStatesbefore narrowing tonstates_.hdr.NumStates(),hdr.Start(), andhdr.NumArcs()asint64_tin
ConstFstImpl::Readbefore assigning them toimpl->nstates_,impl->start_, andimpl->narcs_.ConstLimitsTestinfst_limits_test.ccto test 64-bit valuesthat truncate into 32-bit
StateIdagainst non-empty state streams.