Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes IMAGING-401.
TiffReader.readDirectoryrecursed 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 aStackOverflowError. Each level of recursion also kept aByteSourceinput stream open.Changes:
readDirectorybecomesreadSingleDirectory, which reads one directory and its sub-directories and returns the offset of the next directory.readDirectoryfollows the chain in awhileloop. Directories reach the listener in the same order as before, and each directory's stream is closed before the next one is opened.ignoreNextDirectoryflag, which was always "this is a sub-directory", is replaced with the nesting depth. Sub-directories nested deeper than 8 throw anImagingException, 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.visitedis aHashSet<Long>instead of aList<Number>, making the check O(1).Note for #564: it uses
ignoreNextDirectoryinreadDirectory; after this change the equivalent condition isdepth > 0.Tests: the new
TiffReaderTestbuilds 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 withStackOverflowError.mvn.