summaryrefslogtreecommitdiff
path: root/src/players.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-05-06 21:07:47 +0000
committerrubidium <rubidium@openttd.org>2008-05-06 21:07:47 +0000
commit7b6ce158fe49d95f4e732ceb8372e2b06fabc559 (patch)
tree6a341492b6896f31cc2f5729b0b04ce807084287 /src/players.cpp
parent9e977b78ffab9e2f603b33c75110eec1e9506d6b (diff)
downloadopenttd-7b6ce158fe49d95f4e732ceb8372e2b06fabc559.tar.xz
(svn r12974) -Codechange: check whether loading/saving highscores goes as it should be.
Diffstat (limited to 'src/players.cpp')
-rw-r--r--src/players.cpp31
1 files changed, 20 insertions, 11 deletions
diff --git a/src/players.cpp b/src/players.cpp
index b66626caf..d72bc2b95 100644
--- a/src/players.cpp
+++ b/src/players.cpp
@@ -36,6 +36,7 @@
#include "road_func.h"
#include "rail.h"
#include "sprite.h"
+#include "debug.h"
#include "table/strings.h"
#include "table/sprites.h"
@@ -1072,12 +1073,16 @@ void SaveToHighScore()
for (i = 0; i < LAST_HS_ITEM; i++) { // don't save network highscores
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
/* First character is a command character, so strlen will fail on that */
- byte length = min(sizeof(hs->company), (hs->company[0] == '\0') ? 0 : (int)strlen(&hs->company[1]) + 1);
-
- fwrite(&length, sizeof(length), 1, fp); // write away string length
- fwrite(hs->company, length, 1, fp);
- fwrite(&hs->score, sizeof(hs->score), 1, fp);
- fwrite("", 2, 1, fp); // XXX - placeholder for hs->title, not saved anymore; compatibility
+ byte length = min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : (int)strlen(&hs->company[1]) + 1);
+
+ if (fwrite(&length, sizeof(length), 1, fp) != 1 || // write away string length
+ fwrite(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too
+ fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
+ fwrite(" ", 2, 1, fp) != 1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
+ DEBUG(misc, 1, "Could not save highscore.");
+ i = LAST_HS_ITEM;
+ break;
+ }
}
}
fclose(fp);
@@ -1098,11 +1103,15 @@ void LoadFromHighScore()
for (i = 0; i < LAST_HS_ITEM; i++) { // don't load network highscores
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
byte length;
- fread(&length, sizeof(length), 1, fp);
-
- fread(hs->company, 1, length, fp);
- fread(&hs->score, sizeof(hs->score), 1, fp);
- fseek(fp, 2, SEEK_CUR); // XXX - placeholder for hs->title, not saved anymore; compatibility
+ if (fread(&length, sizeof(length), 1, fp) != 1 ||
+ fread(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too
+ fread(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
+ fseek(fp, 2, SEEK_CUR) == -1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
+ DEBUG(misc, 1, "Highscore corrupted");
+ i = LAST_HS_ITEM;
+ break;
+ }
+ *lastof(hs->company) = '\0';
hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
}
}