Skip to content

Kernel: TiffImageLoader null options, a second ColorMap escape, and a 1GB allocation from 138 bytes #534

Open

Description

@Tim81

Three findings on TiffImageLoader from the #504 documentation review, all measured, none introduced by that branch.

A null options is an input-dependent null dereference. Load(byte[], ImageLoadOptions) is public and reads options.BitDepth only inside its 16-bit branches, so whether a null argument survives depends on the file:

2x2 uncompressed grey,  8-bit, options=null -> OK 2x2
2x2 uncompressed grey, 16-bit, options=null -> NullReferenceException

CcittImageLoader.Load explicitly documents that its options accepts null and takes the default, so a caller carrying that expectation across gets a crash that depends on the image rather than on the call. Either guard it or document it; guarding it matches the sibling.

A second input escapes both documented exception types, at a different site from #512. A palette TIFF whose ColorMap tag carries count 0 and a wholly positive in-file offset reaches BuildPalette and throws IndexOutOfRangeException from colorMap[0], with no negative value anywhere in the file. Bisected: count 0 throws; counts 1, 2, 3 and 6 load. paletteSize = colorMap.Length / 3 is 0 for counts 0 through 2, but only length 0 makes the indexer out of range. #512 covers the negative-offset route in ReadColorMap; this is a separate one and needs its own guard.

An arbitrary allocation precedes the #512 throw. A 138-byte TIFF declaring a ColorMap count of 500,000,000 allocated 1,000,001,280 bytes on the calling thread before raising. Measured at count 6 → 14,928 bytes and count 60,000,000 → 120,001,024 bytes, so it scales with the declared count. ReadColorMap allocates new ushort[count] from an unvalidated tag count and lacks the plausibility guard ReadTagArray carries. The pixel cap does not help: it is checked against the image dimensions, not against a tag count. Higher counts were not tested, so the supported claim is one gigabyte from 138 bytes rather than an unbounded one.

All three are in the same method family and are worth one pass. Commit a fixture per case.

Activity

  1. added this to the v2.3.4 — Hardening milestone on Sep 14, 2026
  2. Tim81 commented on Sep 14, 2026

    @Tim81
    OwnerAuthor

    A fourth item for this issue, found by the round-four contract review of #504.

    ExtraSamples is read and discarded. TiffImageLoader assigns the tag at the switch and never reads it again; hasAlpha is derived solely from samplesPerPixel == 4 && photometric == 2. Measured, the tag makes no difference to the output at all:

    ExtraSamples absent : RGB=C86432…  SMask present  identical to baseline
    ExtraSamples = 0    : RGB=C86432…  SMask present  identical to baseline
    ExtraSamples = 1    : RGB=C86432…  SMask present  identical to baseline
    ExtraSamples = 2    : RGB=C86432…  SMask present  identical to baseline
    

    TIFF 6.0 p. 31 defines 1 as associated alpha, with the colour pre-multiplied, and 2 as unassociated alpha. ISO 32000-2 §11.6.5.2 requires that pre-blended image data carry a Matte entry in the soft-mask image dictionary. So a conforming pre-multiplied TIFF is emitted as a plain /SMask with no /Matte and composites wrong, and ExtraSamples = 0, which means unspecified data rather than alpha, is treated as alpha anyway.

    This is the same class as the rest of this issue: a tag the loader neither honours nor refuses, producing a different image with nothing reported.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions