Skip to content

Fix loading of oversized ICOs and TIFFs with mismatched JPEG sampling… - #191

Merged
danoli3 merged 1 commit into
danoli3:masterfrom
haobinnan:master
Oct 22, 2026
Merged

danoli3 merged 1 commit into
danoli3:masterfrom
haobinnan:master

Conversation

@haobinnan

Copy link
Copy Markdown
Contributor

… factors

  • PluginICO: reject icons whose pixel count exceeds 4096*1024 (~4M pixels) to prevent memory allocation errors caused by abnormally large dimensions.

  • LibTIFF4/tif_jpeg.c: comment out the JPEG sampling-factor mismatch check in JPEGComputeStrileWidthHeightBytesPerLine(). The strict check caused JPEG-compressed TIFF images with mismatched sampling factors to fail to load; decoding now proceeds instead of returning early.

… factors

- PluginICO: reject icons whose pixel count exceeds 4096*1024 (~4M pixels)
  to prevent memory allocation errors caused by abnormally large dimensions.

- LibTIFF4/tif_jpeg.c: comment out the JPEG sampling-factor mismatch check
  in JPEGComputeStrileWidthHeightBytesPerLine(). The strict check caused
  JPEG-compressed TIFF images with mismatched sampling factors to fail to
  load; decoding now proceeds instead of returning early.

Signed-off-by: haobinnan <[email protected]>
@danoli3

danoli3 commented Oct 21, 2026

Copy link
Copy Markdown
Owner

Thanks for the change. I understand the goal is to load JPEG-compressed TIFFs whose sampling factors don’t match. The concern is that commenting out this check allows decoding to continue while the TIFF and JPEG sampling factors may still drive different buffer and output calculations.

Could you include a sample file and regression test, then handle the mismatch explicitly and safely? If the decoder can’t safely support a given mismatch, it should reject that case rather than proceed with inconsistent sizing.

Here’s a regression test you can suggest for TestAPI/testVendorCodecs.cpp. It writes a JPEG-compressed YCbCr TIFF with 2×2 sampling, then changes only the TIFF’s YCbCrSubsampling tag to 1×1. That leaves the embedded JPEG sampling factors mismatched with the TIFF tag.

static void tiffJpegSamplingMismatch() {
    const char *path = "vendor-jpeg-sampling-mismatch.tif";
    const uint32_t w = 64, h = 32;
    TIFF *tif = openTiff(path, "w");
    check(tif, "JPEG TIFF open failed");

    TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, w);
    TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h);
    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
    TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
    TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
    TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);
    TIFFSetField(tif, TIFFTAG_YCBCRSUBSAMPLING, 2, 2);
    TIFFSetField(tif, TIFFTAG_JPEGQUALITY, 90);
    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, h);

    std::vector<unsigned char> row(w * 3);
    for (uint32_t y = 0; y < h; ++y) {
        for (uint32_t x = 0; x < w; ++x) {
            row[x * 3 + 0] = static_cast<unsigned char>(x * 3);
            row[x * 3 + 1] = static_cast<unsigned char>(y * 7);
            row[x * 3 + 2] = static_cast<unsigned char>((x + y) * 5);
        }
        check(TIFFWriteScanline(tif, row.data(), y, 0) >= 0,
              "JPEG TIFF scanline write failed");
    }
    TIFFClose(tif);

    // Change the TIFF metadata while preserving the JPEG-compressed strip.
    tif = openTiff(path, "r+");
    check(tif, "JPEG TIFF reopen failed");
    check(TIFFSetField(tif, TIFFTAG_YCBCRSUBSAMPLING, 1, 1) != 0,
          "Could not change TIFF subsampling tag");
    check(TIFFRewriteDirectory(tif) != 0, "Could not rewrite TIFF directory");
    TIFFClose(tif);

    FIBITMAP *image = FreeImage_Load(FIF_TIFF, path);
    check(image != nullptr, "Mismatched-sampling JPEG TIFF failed to load");
    check(FreeImage_GetWidth(image) == w && FreeImage_GetHeight(image) == h,
          "Mismatched-sampling JPEG TIFF dimensions are wrong");

    FreeImage_Unload(image);
    std::remove(path);
    puts("JPEG TIFF with mismatched sampling factors passed");
}

Call tiffJpegSamplingMismatch(); from main() alongside the other TIFF tests.
This tests that the reported file loads, but it doesn’t by itself prove the mismatch is handled safely or decoded correctly. The author should verify the generated file really retains 2×2 sampling

danoli3 added a commit that referenced this pull request Oct 21, 2026
Decode supported YCbCr sampling mismatches through the full-resolution RGB path, where TIFF raw-plane sizing is not used. Reject unsupported mismatches and add a generated regression fixture that checks decoded pixels against a matching reference.

Use division-based ICO pixel-limit validation to prevent signed multiplication overflow, with a header-only crafted ICO regression test.

Follow-up to haobinnan's report and compatibility change in PR #191.
@danoli3

danoli3 commented Oct 21, 2026

Copy link
Copy Markdown
Owner

Okay I've added to the TEST and forked off ur PR retaining commit history for tests

@haobinnan

Copy link
Copy Markdown
Contributor Author

I've also shared the problematic files with you in case you'd like to take a look. Thanks!

danoli3 added a commit that referenced this pull request Oct 22, 2026
Decode supported YCbCr sampling mismatches through the full-resolution RGB path, where TIFF raw-plane sizing is not used. Reject unsupported mismatches and add a generated regression fixture that checks decoded pixels against a matching reference.

Use division-based ICO pixel-limit validation to prevent signed multiplication overflow, with a header-only crafted ICO regression test.

Follow-up to haobinnan's report and compatibility change in PR #191.
@danoli3
danoli3 merged commit 23f4c9a into danoli3:master Oct 22, 2026
66 checks passed
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.

2 participants