1
0
Fork 0

Fix: crash in emscripten when saving games (#10758)

Don't allocate 128KB on stack, but rather on the heap.
pull/10922/head
Patric Stout 2023-05-02 23:22:09 +02:00 committed by Loïc Guilloux
parent 439ecbc759
commit 0569331f6b
1 changed files with 10 additions and 10 deletions

View File

@ -2598,6 +2598,7 @@ struct ZlibLoadFilter : LoadFilter {
/** Filter using Zlib compression. */ /** Filter using Zlib compression. */
struct ZlibSaveFilter : SaveFilter { struct ZlibSaveFilter : SaveFilter {
z_stream z; ///< Stream state we are writing to. z_stream z; ///< Stream state we are writing to.
byte fwrite_buf[MEMORY_CHUNK_SIZE]; ///< Buffer for writing to the file.
/** /**
* Initialise this filter. * Initialise this filter.
@ -2624,13 +2625,12 @@ struct ZlibSaveFilter : SaveFilter {
*/ */
void WriteLoop(byte *p, size_t len, int mode) void WriteLoop(byte *p, size_t len, int mode)
{ {
byte buf[MEMORY_CHUNK_SIZE]; // output buffer
uint n; uint n;
this->z.next_in = p; this->z.next_in = p;
this->z.avail_in = (uInt)len; this->z.avail_in = (uInt)len;
do { do {
this->z.next_out = buf; this->z.next_out = this->fwrite_buf;
this->z.avail_out = sizeof(buf); this->z.avail_out = sizeof(this->fwrite_buf);
/** /**
* For the poor next soul who sees many valgrind warnings of the * For the poor next soul who sees many valgrind warnings of the
@ -2642,8 +2642,8 @@ struct ZlibSaveFilter : SaveFilter {
int r = deflate(&this->z, mode); int r = deflate(&this->z, mode);
/* bytes were emitted? */ /* bytes were emitted? */
if ((n = sizeof(buf) - this->z.avail_out) != 0) { if ((n = sizeof(this->fwrite_buf) - this->z.avail_out) != 0) {
this->chain->Write(buf, n); this->chain->Write(this->fwrite_buf, n);
} }
if (r == Z_STREAM_END) break; if (r == Z_STREAM_END) break;
@ -2726,6 +2726,7 @@ struct LZMALoadFilter : LoadFilter {
/** Filter using LZMA compression. */ /** Filter using LZMA compression. */
struct LZMASaveFilter : SaveFilter { struct LZMASaveFilter : SaveFilter {
lzma_stream lzma; ///< Stream state that we are writing to. lzma_stream lzma; ///< Stream state that we are writing to.
byte fwrite_buf[MEMORY_CHUNK_SIZE]; ///< Buffer for writing to the file.
/** /**
* Initialise this filter. * Initialise this filter.
@ -2751,19 +2752,18 @@ struct LZMASaveFilter : SaveFilter {
*/ */
void WriteLoop(byte *p, size_t len, lzma_action action) void WriteLoop(byte *p, size_t len, lzma_action action)
{ {
byte buf[MEMORY_CHUNK_SIZE]; // output buffer
size_t n; size_t n;
this->lzma.next_in = p; this->lzma.next_in = p;
this->lzma.avail_in = len; this->lzma.avail_in = len;
do { do {
this->lzma.next_out = buf; this->lzma.next_out = this->fwrite_buf;
this->lzma.avail_out = sizeof(buf); this->lzma.avail_out = sizeof(this->fwrite_buf);
lzma_ret r = lzma_code(&this->lzma, action); lzma_ret r = lzma_code(&this->lzma, action);
/* bytes were emitted? */ /* bytes were emitted? */
if ((n = sizeof(buf) - this->lzma.avail_out) != 0) { if ((n = sizeof(this->fwrite_buf) - this->lzma.avail_out) != 0) {
this->chain->Write(buf, n); this->chain->Write(this->fwrite_buf, n);
} }
if (r == LZMA_STREAM_END) break; if (r == LZMA_STREAM_END) break;
if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code"); if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");