18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 12:25:27 +01:00

fix(simulation): every value of a snapshot file is checked before it's used

A damaged or edited snapshot could put an index past a table, a count
which takes forever or a number out of the range of its type into the
simulation, and a file cut short was put back half way.

- Reading a value: a whole number has to be whole and fit its type (an
  enum its underlying one), a float can't be past its largest value; what
  doesn't fit is refused. Every refusal names the entry, the key and the
  value, and goes to the log (errors.txt), up to 100 lines and a count.
- Ranges: coupler ends 0 or 1 and the flags of a coupling; couplings which
  would close a ring of vehicles; the count of the points of a driver's
  table and of its point limits (not past the keys of the entry), the
  indices into the table (tablelast, semnext, semnextstop); the order
  stack and its position, the timetable station, the driver's enums and
  directions; the path, events and distance of an axle, facing and first
  axle; the date and time; switch states, made track ends, crossing
  segments, event siblings and times, launcher timers, the weather; the
  load (amount, and a name which isn't a path); the whole numbers inside
  number lists (flags of a table point, the stop of a coupler pair, the
  track flags under a vehicle, the fuse count of a power source, the cab
  of the vigilance device).
- The positions of the controllers are brought within the ranges of the
  vehicle: master and secondary controller (set and actual), reverser,
  cabs, light, compressor and wiper switches, brake handle, hand brake,
  dimmer.
- A document with problems isn't loaded unless --load-snapshot-force says
  so; a test run ends with the error code then. The header counts the
  entries, so a file cut between two entries is found too. A file of a
  newer version is a warning, not a problem.
- A snapshot is written to <file>.tmp and renamed over the file only when
  the stream didn't fail.
- Unit tests of the reader on damaged input.
This commit is contained in:
jerrrrycho
2026-11-20 03:29:20 +01:00
parent f8d1d03f85
commit ac1ebd5426
10 changed files with 1068 additions and 142 deletions
+2 -2
View File
@@ -1302,7 +1302,7 @@ int eu07_application::init_settings(int Argc, char *Argv[])
// taken from the command line above, with its value
++i;
}
else if (token == "--load-snapshot" || token == "--snapshot-at")
else if (token == "--load-snapshot" || token == "--snapshot-at" || token == "--load-snapshot-force")
{
// taken from the command line above, with their values
i += simulation::snapshot::option_value_count(token);
@@ -1325,7 +1325,7 @@ int eu07_application::init_settings(int Argc, char *Argv[])
<< " [-seed number|word]"
<< " [-ini settingsfile]"
<< " [--test [--test-timeout seconds] [--test-wall-timeout seconds]]"
<< " [--load-snapshot file] [--snapshot-at seconds file]" << std::endl;
<< " [--load-snapshot file [--load-snapshot-force]] [--snapshot-at seconds file]" << std::endl;
simulation::test().conclude(simulation::test_result::error, std::format("unknown command line argument {}", token));
return -1;
}