From f44d2f0b13a06d680a535c677ea1f363698b0efa Mon Sep 17 00:00:00 2001 From: Chong Kai Xiong Date: Fri, 24 Jan 2025 21:28:26 +0800 Subject: [PATCH 1/3] Core (LV::Video): Fix PNG loader not resetting stream position on error right after reading signature. --- libvisual/libvisual/private/lv_video_png.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libvisual/libvisual/private/lv_video_png.cpp b/libvisual/libvisual/private/lv_video_png.cpp index 398d7f6c9..452666900 100644 --- a/libvisual/libvisual/private/lv_video_png.cpp +++ b/libvisual/libvisual/private/lv_video_png.cpp @@ -62,6 +62,11 @@ namespace LV { png_byte signature[8]; input.read (reinterpret_cast (signature), sizeof (signature)); + input.seekg (saved_stream_pos); + + if (!input) { + return nullptr; + } bool is_png = !png_sig_cmp (signature, 0, sizeof (signature)); @@ -100,6 +105,8 @@ namespace LV { return nullptr; } + input.seekg (sizeof (signature), std::ios::cur); + png_set_read_fn (png_ptr, &input, handle_png_read); png_set_sig_bytes (png_ptr, sizeof (signature)); From 2cfeb2f34f48cffaed54c5529ed0d0fc83da545e Mon Sep 17 00:00:00 2001 From: Chong Kai Xiong Date: Sat, 25 Jan 2025 00:03:31 +0800 Subject: [PATCH 2/3] Core (LV::Video): Fix and clarify the various seeks in the PNG loader. --- libvisual/libvisual/private/lv_video_png.cpp | 27 ++++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/libvisual/libvisual/private/lv_video_png.cpp b/libvisual/libvisual/private/lv_video_png.cpp index 452666900..653a1af2b 100644 --- a/libvisual/libvisual/private/lv_video_png.cpp +++ b/libvisual/libvisual/private/lv_video_png.cpp @@ -58,18 +58,24 @@ namespace LV { VideoPtr bitmap_load_png (std::istream& input) { - auto saved_stream_pos = input.tellg (); + auto start_stream_pos = input.tellg (); - png_byte signature[8]; - input.read (reinterpret_cast (signature), sizeof (signature)); - input.seekg (saved_stream_pos); + // Check PNG signature. - if (!input) { + png_byte signature[8]; + if (!input.read (reinterpret_cast (signature), sizeof (signature))) { + input.clear (); + input.seekg (start_stream_pos); return nullptr; } bool is_png = !png_sig_cmp (signature, 0, sizeof (signature)); + // Clean up test by rewinding to the beginning, like we have read nothing. + if (!input.seekg (start_stream_pos)) { + return nullptr; + } + if (!is_png) { return nullptr; } @@ -91,11 +97,18 @@ namespace LV { return nullptr; } + // Read PNG image data + + // Skip to the first chunk, which comes right after the signature. + input.seekg (start_stream_pos + std::streampos {sizeof (signature)}); + uint8_t* pixels = nullptr; uint8_t** pixel_row_ptrs = nullptr; if (setjmp (png_jmpbuf (png_ptr))) { - input.seekg (saved_stream_pos); + // Some error happened during reading. Rewind to the beginning, like we have read nothing. + input.clear (); + input.seekg (start_stream_pos); png_destroy_read_struct (&png_ptr, &info_ptr, &end_info); @@ -105,8 +118,6 @@ namespace LV { return nullptr; } - input.seekg (sizeof (signature), std::ios::cur); - png_set_read_fn (png_ptr, &input, handle_png_read); png_set_sig_bytes (png_ptr, sizeof (signature)); From 2ef768f7b42d8a23c949fc121a4aa5ef8fa97a25 Mon Sep 17 00:00:00 2001 From: Chong Kai Xiong Date: Sat, 25 Jan 2025 00:05:18 +0800 Subject: [PATCH 3/3] Core (LV::Video): Use 'delete[] ptr' instead of 'delete []ptr'. --- libvisual/libvisual/private/lv_video_png.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libvisual/libvisual/private/lv_video_png.cpp b/libvisual/libvisual/private/lv_video_png.cpp index 653a1af2b..f0298315c 100644 --- a/libvisual/libvisual/private/lv_video_png.cpp +++ b/libvisual/libvisual/private/lv_video_png.cpp @@ -112,7 +112,7 @@ namespace LV { png_destroy_read_struct (&png_ptr, &info_ptr, &end_info); - delete []pixel_row_ptrs; + delete[] pixel_row_ptrs; visual_mem_free (pixels); return nullptr; @@ -189,7 +189,7 @@ namespace LV { png_destroy_read_struct (&png_ptr, &info_ptr, &end_info); - delete []pixel_row_ptrs; + delete[] pixel_row_ptrs; return Video::wrap (pixels, true, width, height, depth); }