Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8351110: ImageIO.write for JPEG can write corrupt JPEG for certain thumbnail dimensions #23920

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

mickleness
Copy link
Contributor

@mickleness mickleness commented Mar 5, 2025

JPEG segments can only be 65535-bytes long. (The marker length is expressed as 2 bytes.) The problem in this ticket is that we were writing more than 65535 bytes to a segment, which later caused parsing errors when we tried to read the file back.

This includes 2 changes:

  1. We now clip the thumbnail width/height to fit within the available marker. We were already constraining the width & height to a max of 255. (Because the width & height are expressed as 1 byte.) So this new clipping is similar to something we were already doing, and we issue the same WARNING_THUMB_CLIPPED warning to the writer. (Does this require a CSR?)

  2. This adds a bounds check to write2bytes. IMO the bigger problem in this ticket was the ImageIO.write(..) caller thought they ended up with a valid JPEG. (We were violating the "do no harm / lose no data" doctrine.) Now if something like this ever comes up again: writing will fail with an IIOException. Technically you can argue this change is unnecessary because the new clipping avoids this error condition, but IMO including this safety net is an overall good thing to keep. If anyone disagrees I'm OK with removing it.

Separately:
I included (and reversed) a commit in this branch that provides an alternate solution. That commit anticipates the overflow problem and switches to a JPEG-encoded thumbnail. From an end user perspective this "just works": they get a (slightly lossly) thumbnail of the original dimensions. But I assume it would be more controversial compared to the clipping approach, since the clipping approach has a strong precedent in this codebase.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Error

 ⚠️ Found copyright format issue for oracle in [test/jdk/javax/imageio/plugins/jpeg/WriteJPEGThumbnailTest]

Issue

  • JDK-8351110: ImageIO.write for JPEG can write corrupt JPEG for certain thumbnail dimensions (Bug - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/23920/head:pull/23920
$ git checkout pull/23920

Update a local copy of the PR:
$ git checkout pull/23920
$ git pull https://git.openjdk.org/jdk.git pull/23920/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 23920

View PR using the GUI difftool:
$ git pr show -t 23920

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/23920.diff

Using Webrev

Link to Webrev Comment

mickleness and others added 11 commits May 22, 2022 04:50
Merge openjdk/jdk into mickleness/jdk
Updating mickleness/jdk from openjdk/jdk
updating to openjdk/jdk
IMO the most serious part of this ticket is that the caller receives no indication that the file that was written is corrupt.

With this change the caller will at least see an IIOException.

This does not satisfactorily resolve the ticket (we want some sort of non-corrupt JPEG), but it's a start.

This placement may be invasive, but I think it's appropriate. I'm having a hard time imagining a scenario where it's safe/ok to call `write2bytes` for -1 or 70,000.
This is my preferred resolution to this ticket, however I'm worried it's too unorthodox so I'll revert it shortly.

Now if the thumbnail is going to be too large: we switch from writing an RGB-style thumbnail to a JPEG-compressed thumbnail.

In this ticket's case: now the call to `ImageIO.write(..)` "just works": the caller gets a valid JPEG file with the entire thumbnail.
We were already clipping the thumbnail width/height to 255px.

Now we're also clipping the thumbnail width/height if the total number of bytes we write will exceed the segment capacity.

We may (?) want to introduce a separate warning constant (other than WARNING_THUMB_CLIPPED?), which may involve a CSR.
@bridgekeeper
Copy link

bridgekeeper bot commented Mar 5, 2025

👋 Welcome back mickleness! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Mar 5, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added the rfr Pull request is ready for review label Mar 5, 2025
@openjdk
Copy link

openjdk bot commented Mar 5, 2025

@mickleness The following label will be automatically applied to this pull request:

  • client

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@mlbridge
Copy link

mlbridge bot commented Mar 5, 2025

Webrevs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

1 participant