Repository navigation
Conversation
An uncompressed bitmap with a bit depth other than 1, 2, 4, 8, 16, 24 or 32 was accepted by Identify, and Load failed with an ImageFormatException or a NotSupportedException depending on the depth. The header reader now rejects such depths with an InvalidImageContentException, so identification and decoding agree. An uncompressed or bit fields header could also declare far more pixel data than the stream holds. The decoder allocated the full pixel buffer before the first row read failed, and a single short row decoded without error. The declared pixel data is now checked against the remaining stream before the image is allocated; only the padding of the last row may be missing.
Member
|
Hi @jafin thank you for these contributions. Could you please raise relevant issues, so we have something to track for release notes. |
3 of 4 tasks
Author
|
@JimBobSquarePants no worries, Issues are there now |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Prerequisites
Description
This PR fixes two problems in how
BmpDecoderCorehandles malformed headers.Invalid bit depth for an uncompressed bitmap
A
BI_RGBbitmap with a bit depth other than 1, 2, 4, 8, 16, 24 or 32 had no consistent handling:Image.Identifyaccepted it. With a depth of 0 it reported 0 bits per pixel.Image.Loadfailed with a different exception for each depth:InvalidImageContentExceptionfor 0 or 33,ImageFormatException(wrapping anIndexOutOfRangeExceptionfrom the palette reader) for 3, 5 or 7, andNotSupportedExceptionfor 12.ReadImageHeadersnow rejects any other depth forBI_RGBwithInvalidImageContentException. Identify and Load go through that method, so both now fail the same way. TheRGB when bitsPerPixel is <= 0 or > 32case inDecodecan no longer be reached, so I removed it.Pixel data smaller than the header declares
The decoder allocated the full pixel buffer before it read any pixel data. A 126-byte file that declared 4000x4000 at 24 bpp requested a 64 MB buffer before the first row read failed. At 10000x10000 the request was about 400 MB. The row readers only fail when a read returns no bytes. Because of this, a file that declared one 100000-pixel row with 72 bytes of data decoded without an error.
Before it allocates the image,
Decodenow computes the pixel data size that the header declares for uncompressed (BI_RGB) and bit fields bitmaps. If the remaining stream is shorter, it throwsInvalidImageContentException. The calculation useslongarithmetic, so very large declared dimensions do not overflow. The padding of the last row can still be missing, as it could before. RLE data has no fixed size, so this check does not apply to it.Testing
New tests in
BmpDecoderTests:BmpDecoder_InvalidUncompressedBitsPerPixel_ThrowsInvalidImageContentExceptioncovers depths 0, 3, 7, 12 and 33. It checks both Identify and Load.BmpDecoder_PixelDataSmallerThanDeclared_ThrowsBeforeAllocatingPixelBuffercovers 4000x4000 and 100000x1 with 72 bytes of pixel data. It usesTestMemoryAllocatorto check that no buffer the size of the declared pixel buffer is requested.BmpDecoder_MissingLastRowPadding_Decodeschecks that a file without the padding of its last row still decodes.The test bitmaps are built in memory. No test images were added.
The first two tests fail on
mainand pass with this change. The third test passes on both.I ran these tests in Release on net10.0:
GeneralFormatTests,InvalidImageDimensionsTestsand theImageTests*classes: 295 passed.Fixes #3235