diff options
author | Simon Howard | 2008-04-19 13:43:17 +0000 |
---|---|---|
committer | Simon Howard | 2008-04-19 13:43:17 +0000 |
commit | 974228a52920aeb89b3f1f19184aa9ffcc037ef5 (patch) | |
tree | 5d86d98fc7f3d41f48216583f9da9a74aeb93650 /src/g_game.c | |
parent | 33616f49d8a394b4db5fc237d59a898973270c53 (diff) | |
download | chocolate-doom-974228a52920aeb89b3f1f19184aa9ffcc037ef5.tar.gz chocolate-doom-974228a52920aeb89b3f1f19184aa9ffcc037ef5.tar.bz2 chocolate-doom-974228a52920aeb89b3f1f19184aa9ffcc037ef5.zip |
Don't successfully save a savegame if a buffer overrun occurs, and don't
overwrite the existing savegame.
Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 1119
Diffstat (limited to 'src/g_game.c')
-rw-r--r-- | src/g_game.c | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/src/g_game.c b/src/g_game.c index c9e3879f..4f7a3d43 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -1485,21 +1485,25 @@ G_SaveGame void G_DoSaveGame (void) { - char name[100]; - char* description; - - strcpy(name, P_SaveGameFile(savegameslot)); + char *savegame_file; + char *temp_savegame_file; - description = savedescription; - - save_stream = fopen(name, "wb"); + temp_savegame_file = P_TempSaveGameFile(); + savegame_file = P_SaveGameFile(savegameslot); + + // Open the savegame file for writing. We write to a temporary file + // and then rename it at the end if it was successfully written. + // This prevents an existing savegame from being overwritten by + // a corrupted one, or if a savegame buffer overrun occurs. + + save_stream = fopen(temp_savegame_file, "wb"); if (save_stream == NULL) { return; } - P_WriteSaveGameHeader(description); + P_WriteSaveGameHeader(savedescription); P_ArchivePlayers (); P_ArchiveWorld (); @@ -1516,11 +1520,19 @@ void G_DoSaveGame (void) I_Error ("Savegame buffer overrun"); } + // Finish up, close the savegame file. + fclose(save_stream); + // Now rename the temporary savegame file to the actual savegame + // file, overwriting the old savegame if there was one there. + + remove(savegame_file); + rename(temp_savegame_file, savegame_file); + gameaction = ga_nothing; strcpy(savedescription, ""); - + players[consoleplayer].message = DEH_String(GGSAVED); // draw the pattern into the back screen |