Skip to content
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
28 changes: 23 additions & 5 deletions libvisual/libvisual/private/lv_video_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,24 @@ namespace LV {

VideoPtr bitmap_load_png (std::istream& input)
{
auto saved_stream_pos = input.tellg ();
auto start_stream_pos = input.tellg ();

// Check PNG signature.

png_byte signature[8];
input.read (reinterpret_cast<char*> (signature), sizeof (signature));
if (!input.read (reinterpret_cast<char*> (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;
}
Expand All @@ -86,15 +97,22 @@ 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)});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
input.seekg (start_stream_pos + std::streampos {sizeof (signature)});
if (!input.seekg (start_stream_pos + std::streampos {sizeof (signature)})) {
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
return nullptr;
}

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subsequent read() calls (called via libpng hooks) will continue to keep the fail flag and trigger the cleanup in the setjmp() block, which also resets the stream position.

Specifically, read() first checks if good() is false (this happens if the seek fails), sets the fail flag again before returning.

One thing about the iostream (with exceptions off) is that once an operation fails, it basically stops trying to do anything. The remaining operations become effectively no-ops. So, one can safely issue a series of operations (e.g. read) and only check the status at the end.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaixiong I think you're saying that you consider status quo good enough. That's alright with me, I will approve now…

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaixiong I think you're saying that you consider status quo good enough. That's alright with me, I will approve now…

@hartwork Thank you.

In case I wasn't clear, there's a subtle difference between your suggested change and the original.

Namely that, with the change, when that seek() does fails, in particular when there's no more data to read (e.g. immediate end-of-file after signature), the handler fails to reset the stream position (only freeing the read struct).

The status quo on the other hand, will go through the entire cleanup process triggered on the first subsequent read() that is guaranteed to fail.


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);

delete []pixel_row_ptrs;
delete[] pixel_row_ptrs;
visual_mem_free (pixels);

return nullptr;
Expand Down Expand Up @@ -171,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);
}
Expand Down