1
0
Fork 0

Fix: Freeing LanguagePack with wrong size.

pull/8805/head
milek7 2021-03-28 00:12:32 +01:00 committed by Michael Lutz
parent dd798d688b
commit 295f34a9df
3 changed files with 13 additions and 8 deletions

View File

@ -1259,7 +1259,7 @@ void SanitizeFilename(char *filename)
* @return Pointer to new memory containing the loaded data, or \c nullptr if loading failed. * @return Pointer to new memory containing the loaded data, or \c nullptr if loading failed.
* @note If \a maxsize less than the length of the file, loading fails. * @note If \a maxsize less than the length of the file, loading fails.
*/ */
std::unique_ptr<char> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize) std::unique_ptr<char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize)
{ {
FILE *in = fopen(filename.c_str(), "rb"); FILE *in = fopen(filename.c_str(), "rb");
if (in == nullptr) return nullptr; if (in == nullptr) return nullptr;
@ -1271,10 +1271,7 @@ std::unique_ptr<char> ReadFileToMem(const std::string &filename, size_t &lenp, s
fseek(in, 0, SEEK_SET); fseek(in, 0, SEEK_SET);
if (len > maxsize) return nullptr; if (len > maxsize) return nullptr;
/* std::unique_ptr assumes new/delete unless a custom deleter is supplied. std::unique_ptr<char[]> mem = std::make_unique<char[]>(len + 1);
* As we don't want to have to carry that deleter all over the place, use
* new directly to allocate the memory instead of malloc. */
std::unique_ptr<char> mem(static_cast<char *>(::operator new(len + 1)));
mem.get()[len] = 0; mem.get()[len] = 0;
if (fread(mem.get(), len, 1, in) != 1) return nullptr; if (fread(mem.get(), len, 1, in) != 1) return nullptr;

View File

@ -49,7 +49,7 @@ const char *FiosGetScreenshotDir();
void SanitizeFilename(char *filename); void SanitizeFilename(char *filename);
void AppendPathSeparator(std::string &buf); void AppendPathSeparator(std::string &buf);
void DeterminePaths(const char *exe); void DeterminePaths(const char *exe);
std::unique_ptr<char> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize); std::unique_ptr<char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize);
bool FileExists(const std::string &filename); bool FileExists(const std::string &filename);
bool ExtractTar(const std::string &tar_filename, Subdirectory subdir); bool ExtractTar(const std::string &tar_filename, Subdirectory subdir);

View File

@ -185,8 +185,16 @@ struct LanguagePack : public LanguagePackHeader {
char data[]; // list of strings char data[]; // list of strings
}; };
struct LanguagePackDeleter {
void operator()(LanguagePack *langpack)
{
/* LanguagePack is in fact reinterpreted char[], we need to reinterpret it back to free it properly. */
delete[] reinterpret_cast<char*>(langpack);
}
};
struct LoadedLanguagePack { struct LoadedLanguagePack {
std::unique_ptr<LanguagePack> langpack; std::unique_ptr<LanguagePack, LanguagePackDeleter> langpack;
std::vector<char *> offsets; std::vector<char *> offsets;
@ -1713,7 +1721,7 @@ bool ReadLanguagePack(const LanguageMetadata *lang)
{ {
/* Current language pack */ /* Current language pack */
size_t len = 0; size_t len = 0;
std::unique_ptr<LanguagePack> lang_pack(reinterpret_cast<LanguagePack *>(ReadFileToMem(lang->file, len, 1U << 20).release())); std::unique_ptr<LanguagePack, LanguagePackDeleter> lang_pack(reinterpret_cast<LanguagePack *>(ReadFileToMem(lang->file, len, 1U << 20).release()));
if (!lang_pack) return false; if (!lang_pack) return false;
/* End of read data (+ terminating zero added in ReadFileToMem()) */ /* End of read data (+ terminating zero added in ReadFileToMem()) */