Repository navigation
P4: Harden campaign save loading against malformed data - #7906
dustinspace217 wants to merge 15 commits into
Conversation
Recent missions, mission records, the last flown ship and red alert weapons read from a campaign save are now checked against the lists they index, and the legacy save converter stays within its score tables. Bad entries are skipped with a warning; valid saves load as before.
The previous mission and loop reentry mission read from a campaign save are now checked as soon as they are read, and cleared with a log message when they are outside the campaign's mission list; a cleared loop reentry also leaves the loop. A negative next mission gets the same invalid-data handling as one past the end, and the ready room checks the previous mission against the mission count before using it. Valid saves load as before.
A section size of 0 turned the per-section read limit off, so the loader read whatever followed as that section's data. Such sections are only written empty, so they are now skipped. The read limit is also capped at the end of the file and checked before cfread shortens a read at the end of the file, so a section claiming more bytes than the file has stops with the existing limit warning instead of reading zeros. Only the pilot file loaders set a read limit. Valid saves load as before.
… file A section size larger than the bytes left could make the loader seek back to the same header forever. Both section loops now stop at such a section, and the rank-only loop also skips zero-size sections. The read limit is no longer capped at the file size (the size check above makes that cap unnecessary). Adds a unit test for the read limit at the end of a file.
BinaryFileHandler::nextSection now treats a section larger than the bytes left in the file like a zero-size one (Section::Invalid), so the section end can no longer wrap around and seek back into the file. The read limit unit test also checks a limit past the end of the file (short read, no exception).
…mpaign-save-loading # Conflicts: # code/cfile/cfilearchive.cpp
Master now pins the other order (scp-fs2open#7863): cfread shortens a read at the end of the file before it checks the read limit, and its tests cover that. The merge keeps master's order, so this test no longer describes the code. Sections that run past the end of the file are still refused by the loaders.
Goober5000
left a comment
There was a problem hiding this comment.
Looks reasonable; one finding of substance:
Medium: the last-flown-ship Assertion is still there, so a debug build still stops on an oversized index.
code/pilotfile/csg.cpp:133(confirmed)
Assertion((idx < (int)ship_list.size()), "Campaign file contains an incorrect value for the last flown ship class...")sits just above the new(idx >= 0) && (idx < size)check.- A save with
idx >= ship_list.size()still hits the Assertion in debug/FastDebug. Only the negative case is actually fixed.- This is external data, so it should be an mprintf or Warning, matching the neighbouring checks. Since the PR claims "the last flown ship" is now checked and that edited saves load on debug builds, this case evidently wasn't among the tested edits.
- Fix: delete the Assertion, or turn it into the same
mprintf(("CSG => Parse Warning: ..."))the other new checks use.
Note: we should probably hold this PR until #7823 and #7824 merge, then rebase.
|
Thanks for looking! That Assertion is already removed in this PR (01b4f67); it's still on master, which may be what got read. An index past the end of the ship list now logs the same CSG parse warning as the other checks and clears the last flown ship. The test Happy to hold this until #7823 and #7824 merge, then rebase onto master. |
What?
This makes the campaign save (
.csg) and pilot file (.plr) loaders check the numbers they read before using them, as list positions or as counts.Why?
A damaged or hand-edited save could crash the game while loading, leave the previous pilot's data in place, or, if it ended right after a large count, grow memory until the game ran out or hang. Normal saves load exactly as before.
How?
Testing?
Anything Else?
The length of each name stored in the save is handled by #7863. I checked the legacy converter change by reading the code; I didn't have an old-format pilot to convert.
I used Claude Code to help find and test this.