Fix PNM files being misdetected as TGA#795
Open
Yusufihsangorgel wants to merge 3 commits into
Open
Conversation
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.
Closes #744.
The problem
Decoding a binary PNM file (for example a
P5PGM) can be misdetected as TGA,corrupting the result or throwing a
RangeError.findDecoderForDataprobes TGA before PNM. TGA has no magic bytes, and itsvalidation only checks the pixel depth and colormap fields. A binary PNM header
often lines up: byte 2 is a newline (
0x0a), which is a valid TGA image type,and byte 16 frequently lands on a valid TGA pixel depth (8, 16, 24 or 32). So
TGA claims the file and its reader overruns.
The fix
Probe PNM before TGA.
PnmDecoder.isValidFileaccepts only an exactP1toP6magic token, so no genuine TGA or other file can be captured by the earliercheck, and the strictly identified format simply wins. It is a one-block
reorder, with no public API or output change.
Tests
Added
test/formats/pnm_detection_test.dart: a crafted 4x4 binaryP5, whosebytes satisfy TGA's loose validation (byte 16 is 24, a valid pixel depth), is
now detected as
ImageFormat.pnmand decodes to the correct image. I checked itboth ways: before the fix
findFormatForDatareturnsImageFormat.tgaand thetest fails; after the fix it passes. Full suite green (1109 tests).