ppkarwasz opened a new pull request, #584: URL: https://github.com/apache/commons-imaging/pull/584
Fixes [IMAGING-401](https://issues.apache.org/jira/browse/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`. - [x] Read the contribution guidelines. - [x] Read the ASF Generative Tooling Guidance. - [x] 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. - [x] Run a successful build using the default Maven goal with `mvn`. - [x] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why. - [x] Each commit in the pull request has a meaningful subject line and body. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
