Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions openfst/lib/vector-fst.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <ios>
#include <iosfwd>
#include <istream>
#include <limits>
#include <memory>
#include <optional>
#include <ostream>
Expand Down Expand Up @@ -483,6 +484,8 @@ VectorFstImpl<S>* VectorFstImpl<S>::Read(std::istream& strm,
impl->BaseImpl::SetStart(hdr.Start());
if (hdr.NumStates() != kNoStateId) impl->ReserveStates(hdr.NumStates());
StateId state = 0;
StateId max_next_state = std::numeric_limits<StateId>::lowest();
StateId min_next_state = std::numeric_limits<StateId>::max();
for (; hdr.NumStates() == kNoStateId || state < hdr.NumStates(); ++state) {
Weight weight;
if (!weight.Read(strm)) break;
Expand All @@ -506,13 +509,30 @@ VectorFstImpl<S>* VectorFstImpl<S>::Read(std::istream& strm,
LOG(ERROR) << "VectorFst::Read: Read failed: " << opts.source;
return nullptr;
}
if (arc.nextstate > max_next_state) max_next_state = arc.nextstate;
if (arc.nextstate < min_next_state) min_next_state = arc.nextstate;
impl->BaseImpl::AddArc(state, std::move(arc));
}
}
if (hdr.NumStates() != kNoStateId && state != hdr.NumStates()) {
LOG(ERROR) << "VectorFst::Read: Unexpected end of file: " << opts.source;
return nullptr;
}
// Sanity check for the start state.
if (impl->Start() != kNoStateId && impl->Start() >= state) {
LOG(ERROR) << "VectorFst::Read: initial state " << impl->Start()
<< " out of range [0, " << state << ")";
return nullptr;
}
// Sanity check on next states.
if (min_next_state < 0) {
LOG(ERROR) << "VectorFst::Read: Invalid arc.nextstate=" << min_next_state;
return nullptr;
}
if (max_next_state >= state) {
LOG(ERROR) << "VectorFst::Read: Invalid arc.nextstate=" << max_next_state;
return nullptr;
}
return impl.release();
}

Expand Down
Loading