CleverRaven/Cataclysm-DDA — pull request #2381 by kevingranade (2013-08-11 17:12:34+00:00)
Title: Prevent corrupting last loaded submap. Thanks to wito for finding it.

May fix #426 fix #437 and fix #1341.
To reproduce previous issue:
Clear maps, make a new character, immediately save, exit program, restart, and load.
The submap approximately 48 squares SE of the shelter will be completely uniform and featureless.
Also if you backup the save/maps.txt file after saving, then load and save. Comparing the backup and new file will show that the last listed submap is a uniform terrain type, and has no features, even if the previous version of it did.

Big props to wito, he pointed out that there was something funny happening with the last submap in maps.txt, and that the value used was the previous value of tmpter, both of which were instrumental in figuring out what was going wrong.

Post-mortem:
What was happening was that after processing the last submap from maps.txt, the program would check for EOF and not find it, because there was a single newline left. It would then try to read a submap, but every single read would fail. Since none of the variables in the loop were initialized (boo), they would retain their values from the previous iteration, so the x,y,z, turns, etc would match the last submap, the terrain would be the last terrain* read (SE corner of submap), and the radiation level would be the same as the last one read. The submap would be featureless, because the last string_identifier read was "---", which would cause it to exit from feature processing, at which point it would re-write the most recently written submap with a garbage version of itself. The error would then be persisted the next time the map was written to disk.

The fix is simple, check for EOF after trying to read past the end of the current submap and exit the loop if it's encountered.

*On debug builds, it uses the last terrain read, on release builds it gets a 0, which I suspect is the value of radtmp. Other contents of the submap may have caused the LOS-blocking, crash-causing version that has also been reported.