Skip to content

Commit

Permalink
Fix M_SaveBitmap stream buffer size
Browse files Browse the repository at this point in the history
- credits to dpJudas for the solution
  • Loading branch information
MrRaveYard committed Dec 4, 2024
1 parent ab9b632 commit f425076
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/common/textures/m_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
// MACROS ------------------------------------------------------------------

// The maximum size of an IDAT chunk ZDoom will write. This is also the
// size of the compression buffer it allocates on the stack.
// size of the compression buffer it allocates on the heap.
#define PNG_WRITE_SIZE 32768

// Set this to 1 to use a simple heuristic to select the filter to apply
Expand Down Expand Up @@ -926,8 +926,7 @@ bool M_SaveBitmap(const uint8_t *from, ESSType color_type, int width, int height
temprow[i] = &temprow_storage[temprow_size * i];
}

TArray<Byte> array(PNG_WRITE_SIZE, true);
auto buffer = array.data();
TArray<Byte> buffer(PNG_WRITE_SIZE, true);
z_stream stream;
int err;
int y;
Expand All @@ -944,8 +943,8 @@ bool M_SaveBitmap(const uint8_t *from, ESSType color_type, int width, int height
}

y = height;
stream.next_out = buffer;
stream.avail_out = sizeof(buffer);
stream.next_out = buffer.data();
stream.avail_out = buffer.size();

temprow[0][0] = 0;
#if USE_FILTER_HEURISTIC
Expand Down Expand Up @@ -1007,12 +1006,12 @@ bool M_SaveBitmap(const uint8_t *from, ESSType color_type, int width, int height
}
while (stream.avail_out == 0)
{
if (!WriteIDAT (file, buffer, sizeof(buffer)))
if (!WriteIDAT (file, buffer.data(), buffer.size()))
{
return false;
}
stream.next_out = buffer;
stream.avail_out = sizeof(buffer);
stream.next_out = buffer.data();
stream.avail_out = buffer.size();
if (stream.avail_in != 0)
{
err = deflate (&stream, (y == 0) ? Z_FINISH : 0);
Expand All @@ -1033,12 +1032,12 @@ bool M_SaveBitmap(const uint8_t *from, ESSType color_type, int width, int height
}
if (stream.avail_out == 0)
{
if (!WriteIDAT (file, buffer, sizeof(buffer)))
if (!WriteIDAT (file, buffer.data(), buffer.size()))
{
return false;
}
stream.next_out = buffer;
stream.avail_out = sizeof(buffer);
stream.next_out = buffer.data();
stream.avail_out = buffer.size();
}
}

Expand All @@ -1048,7 +1047,7 @@ bool M_SaveBitmap(const uint8_t *from, ESSType color_type, int width, int height
{
return false;
}
return WriteIDAT (file, buffer, sizeof(buffer)-stream.avail_out);
return WriteIDAT (file, buffer.data(), buffer.size() - stream.avail_out);
}

//==========================================================================
Expand Down

0 comments on commit f425076

Please sign in to comment.