Skip to content

IMAGING-401: Read TIFF directory chains without recursion - #584

Open
ppkarwasz wants to merge 2 commits into
apache:masterfrom
ppkarwasz:fix/IMAGING-401-tiff-directory-recursion
Open

ppkarwasz wants to merge 2 commits into
apache:masterfrom
ppkarwasz:fix/IMAGING-401-tiff-directory-recursion

Conversation

@ppkarwasz

Copy link
Copy Markdown
Member

Fixes IMAGING-401.

TiffReader.readDirectory recursed once per directory in the next-IFD chain and once per EXIF, GPS or interoperability sub-directory, without a depth limit. The visited-offset list stopped loops, but not a long chain of distinct offsets: an empty IFD is 6 bytes, so a ~120 KB file with 20,000 chained IFDs, or a file with deeply nested EXIF directories, caused a StackOverflowError. Each level of recursion also kept a ByteSource input stream open.

Changes:

  • The body of readDirectory becomes readSingleDirectory, which reads one directory and its sub-directories and returns the offset of the next directory. readDirectory follows the chain in a while loop. Directories reach the listener in the same order as before, and each directory's stream is closed before the next one is opened.
  • The ignoreNextDirectory flag, which was always "this is a sub-directory", is replaced with the nesting depth. Sub-directories nested deeper than 8 throw an ImagingException, which the existing sub-directory handling rethrows in strict mode and turns into a skipped offset field otherwise. The specifications only nest the interoperability directory in the EXIF directory, so 8 leaves a wide margin.
  • visited is a HashSet<Long> instead of a List<Number>, making the check O(1).

Note for #564: it uses ignoreNextDirectory in readDirectory; after this change the equivalent condition is depth > 0.

Tests: the new TiffReaderTest builds TIFFs in memory with 20,000 chained IFDs, a looping chain, and EXIF directories nested 8, 9 and 20,000 levels deep, in strict and lenient mode. Without the change, the chain and the deep nesting fail with StackOverflowError.

  • Read the contribution guidelines.
  • Read the ASF Generative Tooling Guidance.
  • I used AI to create any part of, or all of, this pull request: Claude Code (Claude Opus 5.5) helped analyze the directory reader, draft the change and tests, and write this description; I reviewed and verified everything.
  • Run a successful build using the default Maven goal with mvn.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request has a meaningful subject line and body.

TiffReader.readDirectory called itself for the next directory of each
IFD and for each EXIF, GPS and interoperability sub-directory, with no
depth limit, so a long chain of small IFDs or deeply nested EXIF
directories caused a StackOverflowError, and every level kept an input
stream open.

Follow the chain of next directories in a loop, reading one directory
and its sub-directories per iteration, in the same order as before.
Replace the ignoreNextDirectory flag with the nesting depth and reject
sub-directories nested deeper than 8 with an ImagingException, which is
rethrown in strict mode and skipped otherwise. Track visited offsets in
a HashSet.

Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant