Changeset - r27226:c2796b7b2845
[Not reviewed]
master
0 1 0
Patric Stout - 19 months ago 2023-05-02 21:22:09
truebrain@openttd.org
Fix: crash in emscripten when saving games (#10758)

Don't allocate 128KB on stack, but rather on the heap.
1 file changed with 10 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/saveload/saveload.cpp
Show inline comments
 
@@ -2582,6 +2582,7 @@ struct ZlibLoadFilter : LoadFilter {
 
/** Filter using Zlib compression. */
 
struct ZlibSaveFilter : SaveFilter {
 
	z_stream z; ///< Stream state we are writing to.
 
	byte fwrite_buf[MEMORY_CHUNK_SIZE]; ///< Buffer for writing to the file.
 

	
 
	/**
 
	 * Initialise this filter.
 
@@ -2608,13 +2609,12 @@ struct ZlibSaveFilter : SaveFilter {
 
	 */
 
	void WriteLoop(byte *p, size_t len, int mode)
 
	{
 
		byte buf[MEMORY_CHUNK_SIZE]; // output buffer
 
		uint n;
 
		this->z.next_in = p;
 
		this->z.avail_in = (uInt)len;
 
		do {
 
			this->z.next_out = buf;
 
			this->z.avail_out = sizeof(buf);
 
			this->z.next_out = this->fwrite_buf;
 
			this->z.avail_out = sizeof(this->fwrite_buf);
 

	
 
			/**
 
			 * For the poor next soul who sees many valgrind warnings of the
 
@@ -2626,8 +2626,8 @@ struct ZlibSaveFilter : SaveFilter {
 
			int r = deflate(&this->z, mode);
 

	
 
			/* bytes were emitted? */
 
			if ((n = sizeof(buf) - this->z.avail_out) != 0) {
 
				this->chain->Write(buf, n);
 
			if ((n = sizeof(this->fwrite_buf) - this->z.avail_out) != 0) {
 
				this->chain->Write(this->fwrite_buf, n);
 
			}
 
			if (r == Z_STREAM_END) break;
 

	
 
@@ -2710,6 +2710,7 @@ struct LZMALoadFilter : LoadFilter {
 
/** Filter using LZMA compression. */
 
struct LZMASaveFilter : SaveFilter {
 
	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.
 
@@ -2735,19 +2736,18 @@ struct LZMASaveFilter : SaveFilter {
 
	 */
 
	void WriteLoop(byte *p, size_t len, lzma_action action)
 
	{
 
		byte buf[MEMORY_CHUNK_SIZE]; // output buffer
 
		size_t n;
 
		this->lzma.next_in = p;
 
		this->lzma.avail_in = len;
 
		do {
 
			this->lzma.next_out = buf;
 
			this->lzma.avail_out = sizeof(buf);
 
			this->lzma.next_out = this->fwrite_buf;
 
			this->lzma.avail_out = sizeof(this->fwrite_buf);
 

	
 
			lzma_ret r = lzma_code(&this->lzma, action);
 

	
 
			/* bytes were emitted? */
 
			if ((n = sizeof(buf) - this->lzma.avail_out) != 0) {
 
				this->chain->Write(buf, n);
 
			if ((n = sizeof(this->fwrite_buf) - this->lzma.avail_out) != 0) {
 
				this->chain->Write(this->fwrite_buf, n);
 
			}
 
			if (r == LZMA_STREAM_END) break;
 
			if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
0 comments (0 inline, 0 general)