Fixed a crash when reading PNG files with an invalid header. #385
+13
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Use of
BOOST_ASSERT
inpng_ptr_read_deleter
andpng_ptr_write_deleter
in include/boost/gil/extension/io/png/detail/reader_backend.hpp
will produce a crash with PNG files with an invalid header.In such a case (bad PNG file), we expect a
std::ios_base::failure
exception (see below).Unfortunately, the assertions will prevent the exception to be caught by terminating the program.
This hotfix replaces the assertions with conditional checks, so we can get the expected exception handling.
Steps to reproduce
ABCD
).Explanation
A
reader_backend
object is created:include/boost/gil/extension/io/png/detail/reader_backend.hpp
.The base class' constructor (
png_struct_info_wrapper
) has astd::shared_ptr
member for thepng_ptr_wrapper
.The shared pointer is created with a custom deleter (
png_ptr_read_deleter
andpng_ptr_write_deleter
).So inside the backend constructor, the
png_ptr_wrapper
is non-null.Then, still from the constructor, the PNG header is read by calling
read_header
.So for an invalid header,
io_error_if
will throw an exception.This is the expected behavior.
But as the exception is thrown, the backend object gets deallocated.
So is the
std::shared_ptr
. And as the deleter is called:png_ptr
is valid, but all fields will benull
, as we haven't read the PNG file yet.Assertion will fail, making the program crash, and thus preventing the expected exception to be caught.
References
Tasklist