diff --git a/tests/unit/test_timetable.cpp b/tests/unit/test_timetable.cpp index f4387e7f8..a41aafcce 100644 --- a/tests/unit/test_timetable.cpp +++ b/tests/unit/test_timetable.cpp @@ -229,6 +229,24 @@ TEST_CASE("timetable: a timetable longer than the table is an error") { CHECK(data.StationCount == Mtable::MaxTTableSize); } +TEST_CASE("timetable: the reader starts from an empty table, whatever the data held before") { + auto const text{timetable_with("", "R2,H", "po", "R3")}; + auto const [error, fresh]{read(text)}; + REQUIRE(error == 0); + // a count past the table, a negative one, a stale one: the reader used to go on from it and index out of the table + for (auto const count : {Mtable::MaxTTableSize, Mtable::MaxTTableSize + 7, -3, 2}) { + CAPTURE(count); + std::istringstream input{text}; + Mtable::timetable_data data; + data.TrainName = "file"; + data.StationCount = count; + data.TimeTable[fresh.StationCount + 1].StationName = "stale"; + CHECK(Mtable::read_timetable(input, data) == 0); + CHECK(contents(data) == contents(fresh)); + CHECK(data.TimeTable[fresh.StationCount + 1].StationName == "nowhere"); + } +} + TEST_CASE("timetable: the entry whose flags hold at the stop and on the way") { // before the timetable is loaded, and with none CHECK(timetable::stop_flags_entry(0, 0) == 0); diff --git a/world/timetablereader.cpp b/world/timetablereader.cpp index 3d340e8bd..087c0205c 100644 --- a/world/timetablereader.cpp +++ b/world/timetablereader.cpp @@ -58,6 +58,9 @@ int read_timetable(std::istream &fin, timetable_data &Data) { int conversionerror{0}; auto &TimeTable{Data.TimeTable}; auto &StationCount{Data.StationCount}; + // the table is read from its start, whatever Data held before + StationCount = 0; + TimeTable.fill(TMTableEntry{}); // zapisywanie prędkości maksymalnej do wcześniejszych odcinków auto const UpdateVelocity{[&TimeTable, &StationCount](double const Velocity) { int i = StationCount; @@ -164,12 +167,12 @@ int read_timetable(std::istream &fin, timetable_data &Data) { TimeTable[0].flags = Data.Flags; auto activeradiochannel{-1}; while (!fin.fail() && !EndTable) { - ++StationCount; - if (StationCount > MaxTTableSize) { - --StationCount; + // both bounds, so TimeTable[StationCount] and TimeTable[StationCount - 1] below stay in the table + if (StationCount < 0 || StationCount >= MaxTTableSize) { conversionerror = timetable_too_long; break; } + ++StationCount; do { fin >> s; } while (!(s == "[" || fin.fail()));