Skip to content

Commit

Permalink
GS: Fix crash and file leak during dump
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Apr 22, 2024
1 parent 7dd7345 commit 88c476c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pcsx2/GS/GSPng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ namespace GSPng
const int offset = first_image ? 0 : pixel[fmt].bytes_per_pixel_out;
const int bytes_per_pixel_out = first_image ? pixel[fmt].bytes_per_pixel_out : bytes_per_pixel_in - offset;

FILE* fp = FileSystem::OpenCFile(file.c_str(), "wb");
if (fp == nullptr)
auto fp = FileSystem::OpenManagedCFile(file.c_str(), "wb");
if (!fp)
return false;

png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
Expand All @@ -55,7 +55,14 @@ namespace GSPng
if (setjmp(png_jmpbuf(png_ptr)))
return false;

png_init_io(png_ptr, fp);
png_set_write_fn(
png_ptr, fp.get(),
[](png_structp png_ptr, png_bytep data_ptr, png_size_t size) {
if (std::fwrite(data_ptr, size, 1, static_cast<std::FILE*>(png_get_io_ptr(png_ptr))) != 1)
png_error(png_ptr, "file write error");
},
[](png_structp png_ptr) {});

png_set_compression_level(png_ptr, compression);
png_set_IHDR(png_ptr, info_ptr, width, height, channel_bit_depth, type,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
Expand All @@ -77,7 +84,6 @@ namespace GSPng

if (png_ptr)
png_destroy_write_struct(&png_ptr, info_ptr ? &info_ptr : nullptr);
std::fclose(fp);

return true;
}
Expand Down

0 comments on commit 88c476c

Please sign in to comment.