Skip to content

Fix PNM files being misdetected as TGA#795

Open
Yusufihsangorgel wants to merge 3 commits into
brendan-duncan:mainfrom
Yusufihsangorgel:fix/744-pnm-misdetected-as-tga
Open

Fix PNM files being misdetected as TGA#795
Yusufihsangorgel wants to merge 3 commits into
brendan-duncan:mainfrom
Yusufihsangorgel:fix/744-pnm-misdetected-as-tga

Conversation

@Yusufihsangorgel

Copy link
Copy Markdown

Closes #744.

The problem

Decoding a binary PNM file (for example a P5 PGM) can be misdetected as TGA,
corrupting the result or throwing a RangeError.

findDecoderForData probes TGA before PNM. TGA has no magic bytes, and its
validation 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.isValidFile accepts only an exact P1 to
P6 magic token, so no genuine TGA or other file can be captured by the earlier
check, 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 binary P5, whose
bytes satisfy TGA's loose validation (byte 16 is 24, a valid pixel depth), is
now detected as ImageFormat.pnm and decodes to the correct image. I checked it
both ways: before the fix findFormatForData returns ImageFormat.tga and the
test fails; after the fix it passes. Full suite green (1109 tests).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PNM file incorrectly decoded as TGA

1 participant