18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00

fix(timetable): read the table from its start, bound the station index both ways

read_timetable() went on from the StationCount it was given and checked only the upper bound, so
TimeTable[StationCount] and TimeTable[StationCount - 1] could index out of the table (Sonar cpp:S3519).
It now resets the count and the table first, and checks both bounds before the next station.
This commit is contained in:
jerrrrycho
2026-11-17 08:54:50 +01:00
parent aa84e41f09
commit 66b8a67361
2 changed files with 24 additions and 3 deletions

View File

@@ -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);

View File

@@ -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()));