Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GS: Fix crash and file leak during dump #11123

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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