summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2019-04-02 21:20:19 +0200
committerMichael Lutz <michi@icosahedron.de>2019-04-09 22:45:15 +0200
commitd95c7083ea582ea723107aa42f27b8cf3e3365e1 (patch)
tree7c8c08a1680a4eb29f61cb2099feea435df28797 /src
parent0a883afe19e6bd70cb79515dadb8c890cc3e68b1 (diff)
downloadopenttd-d95c7083ea582ea723107aa42f27b8cf3e3365e1.tar.xz
Fix: Don't crash if reading a GS string file from disk produces an error.
The raw_strings vector may not include NULLs as no consumer can deal with it.
Diffstat (limited to 'src')
-rw-r--r--src/game/game_text.cpp26
1 files changed, 13 insertions, 13 deletions
diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp
index d57b2bc77..5016f967d 100644
--- a/src/game/game_text.cpp
+++ b/src/game/game_text.cpp
@@ -83,13 +83,12 @@ LanguageStrings::~LanguageStrings()
LanguageStrings *ReadRawLanguageStrings(const char *file)
{
LanguageStrings *ret = NULL;
- FILE *fh = NULL;
try {
size_t to_read;
- fh = FioFOpenFile(file, "rb", GAME_DIR, &to_read);
- if (fh == NULL) {
- return NULL;
- }
+ FILE *fh = FioFOpenFile(file, "rb", GAME_DIR, &to_read);
+ if (fh == NULL) return NULL;
+
+ FileCloser fhClose(fh);
const char *langname = strrchr(file, PATHSEPCHAR);
if (langname == NULL) {
@@ -99,10 +98,7 @@ LanguageStrings *ReadRawLanguageStrings(const char *file)
}
/* Check for invalid empty filename */
- if (*langname == '.' || *langname == 0) {
- fclose(fh);
- return NULL;
- }
+ if (*langname == '.' || *langname == 0) return NULL;
ret = new LanguageStrings(langname, strchr(langname, '.'));
@@ -124,10 +120,8 @@ LanguageStrings *ReadRawLanguageStrings(const char *file)
}
}
- fclose(fh);
return ret;
} catch (...) {
- if (fh != NULL) fclose(fh);
delete ret;
return NULL;
}
@@ -246,7 +240,10 @@ public:
{
if (strcmp(filename, exclude) == 0) return true;
- gs->raw_strings.push_back(ReadRawLanguageStrings(filename));
+ auto ls = ReadRawLanguageStrings(filename);
+ if (ls == NULL) return false;
+
+ gs->raw_strings.push_back(std::move(ls));
return true;
}
};
@@ -267,9 +264,12 @@ GameStrings *LoadTranslations()
strecpy(e, "lang" PATHSEP "english.txt", lastof(filename));
if (!FioCheckFileExists(filename, GAME_DIR)) return NULL;
+ auto ls = ReadRawLanguageStrings(filename);
+ if (ls == NULL) return NULL;
+
GameStrings *gs = new GameStrings();
try {
- gs->raw_strings.push_back(ReadRawLanguageStrings(filename));
+ gs->raw_strings.push_back(std::move(ls));
/* Scan for other language files */
LanguageScanner scanner(gs, filename);