SaveState: Use malloc to avoid Android OOM crash.

We have exceptions disabled on Android, which is exactly where we need the
OOM check most.
This commit is contained in:
Unknown W. Brackets committed 2017-11-05 10:04:22 -08:00
1 parent 9e65ae1cef
commit 66ddf592b4
2 files changed
+24 -28

No files matched your search

+21 -18
View File
@@ -15,6 +15,7 @@
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include <cstdlib>
#include <cstring>
#include <snappy-c.h>
@@ -271,18 +272,23 @@ CChunkFileReader::Error CChunkFileReader::SaveFile(const std::string &filename,
INFO_LOG(SAVESTATE, "ChunkReader: Writing %s", filename.c_str());
File::IOFile pFile(filename, "wb");
if (!pFile)
{
if (!pFile) {
ERROR_LOG(SAVESTATE, "ChunkReader: Error opening file for write");
delete[] buffer;
free(buffer);
return ERROR_BAD_FILE;
}
bool compress = true;
// Make sure we can allocate a buffer to compress before compressing.
size_t comp_len = snappy_max_compressed_length(sz);
u8 *compressed_buffer = (u8 *)malloc(comp_len);
if (!compressed_buffer) {
ERROR_LOG(SAVESTATE, "ChunkReader: Unable to allocate compressed buffer");
// We'll save uncompressed. Better than not saving...
}
// Create header
SChunkHeader header;
header.Compress = compress ? 1 : 0;
header.Compress = compressed_buffer ? 1 : 0;
header.Revision = REVISION_CURRENT;
header.ExpectedSize = (u32)sz;
header.UncompressedSize = (u32)sz;
@@ -293,12 +299,11 @@ CChunkFileReader::Error CChunkFileReader::SaveFile(const std::string &filename,
truncate_cpy(titleFixed, title.c_str());
// Write to file
if (compress) {
size_t comp_len = snappy_max_compressed_length(sz);
u8 *compressed_buffer = new u8[comp_len];
if (compressed_buffer) {
snappy_compress((const char *)buffer, sz, (char *)compressed_buffer, &comp_len);
delete [] buffer;
free(buffer);
header.ExpectedSize = (u32)comp_len;
if (!pFile.WriteArray(&header, 1)) {
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing header");
return ERROR_BAD_FILE;
@@ -307,27 +312,25 @@ CChunkFileReader::Error CChunkFileReader::SaveFile(const std::string &filename,
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing title");
return ERROR_BAD_FILE;
}
if (!pFile.WriteBytes(&compressed_buffer[0], comp_len)) {
if (!pFile.WriteBytes(compressed_buffer, comp_len)) {
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing compressed data");
return ERROR_BAD_FILE;
} else {
INFO_LOG(SAVESTATE, "Savestate: Compressed %i bytes into %i", (int)sz, (int)comp_len);
}
delete [] compressed_buffer;
free(compressed_buffer);
} else {
if (!pFile.WriteArray(&header, 1))
{
if (!pFile.WriteArray(&header, 1)) {
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing header");
delete[] buffer;
free(buffer);
return ERROR_BAD_FILE;
}
if (!pFile.WriteBytes(&buffer[0], sz))
{
if (!pFile.WriteBytes(&buffer[0], sz)) {
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing data");
delete[] buffer;
free(buffer);
return ERROR_BAD_FILE;
}
delete [] buffer;
free(buffer);
}
INFO_LOG(SAVESTATE, "ChunkReader: Done writing %s", filename.c_str());