From 5114b8f8b69eb4e46da0148e48df4a762ea5ed57 Mon Sep 17 00:00:00 2001 From: Kp Date: Sat, 8 Oct 2016 23:24:21 +0000 Subject: [PATCH] Fix object corruption loading save game Saved games dump robot ai_local state for all objects, regardless of whether the object is a robot. Loading a game restored all ai_local values, regardless of whether the object was a robot on restore. This caused memory corruption in objects that were not robots when the `ctype.`_T_ for that object was large enough to overlap `ctype.ai_info.ail`. Fix this by loading the ai_local into a junk buffer when the object is not a robot. It would be better to skip over the bogus ai_local entirely, but the structure of ai_local_read_swap makes that difficult. Use the easy fix for now. --- similar/main/ai.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/similar/main/ai.cpp b/similar/main/ai.cpp index 78d91c82d..e647b693d 100644 --- a/similar/main/ai.cpp +++ b/similar/main/ai.cpp @@ -4659,7 +4659,10 @@ int ai_restore_state(PHYSFS_File *fp, int version, int swap) PHYSFSX_readSXE32(fp, swap); Overall_agitation = PHYSFSX_readSXE32(fp, swap); range_for (object &obj, Objects) - ai_local_read_swap(&obj.ctype.ai_info.ail, swap, fp); + { + ai_local discard; + ai_local_read_swap(obj.type == OBJ_ROBOT ? &obj.ctype.ai_info.ail : &discard, swap, fp); + } PHYSFSX_serialize_read(fp, Point_segs); ai_cloak_info_read_n_swap(Ai_cloak_info.data(), Ai_cloak_info.size(), swap, fp); tmptime32 = PHYSFSX_readSXE32(fp, swap);