Repository navigation
Prevent JP2/J2K assertions after failed stream length probes - #232
Conversation
|
Investigation of the assertion reported in #198, including when the bug appeared and why the tests missed it. When did this regress?The available history does not establish a recent regression. The unchecked callback error handling is already present in the repository's earliest commit, 279da23, FreeImage 3.16.0, 4 September 2014:
The 19 August 2026 OpenJPEG 2.0.0 → 2.5.4 update, 229de19, did not introduce this assertion: it exists in the parent revision too. The 25 October 2026 display tests, 30ed59f / PR #207, added successful decode, format selection, native pixel-type and preview coverage. They did not add this fault case. The assertion was reported in the issue on 27 October, followed by the linked follow-up on 28 October. Therefore the supported conclusion is: a longstanding callback-handling bug, present since at least the 2014 import, now reproduced on pre-fix master Reproduction and causeI reproduced the exact assertion in a macOS Debug + ASan build; it is not restricted to Windows:
The generated 32×32 regression fixture and all four supplied JPEG 2000 files reproduce this failure on the unmodified baseline:
Normal callbacks still load every input after the fix. This demonstrates a sufficient cause for the reported assertion; the reporter's actual callback implementation has not been supplied, so its exact trigger is not yet confirmed. Incorrect length reporting or other inconsistent stream callbacks can produce similar symptoms. Why did the existing unit tests miss it?
As a direct check, all 11 existing CTests pass on the pre-fix Debug + ASan baseline with JXR/OpenEXR disabled. Linking the new regression test against that same baseline library immediately reproduces the assertion. The fixed build passes all 12 CTests in that configuration. Fix and preventionThe adapter now validates the initial tell, seek to EOF, end tell, restoration seek, and positive remaining input length. On failure it rejects stream creation, so JP2/J2K loading returns
It also checks content detection/position preservation, byte-exact lossless pixels, saving without Callers should implement the return FreeImage_SeekMemory(memory, offset, origin) ? 0 : -1;Do not remove the OpenJPEG assertion or advertise a fabricated length to suppress it. Returning Separate ASan finding during verificationWith JXR enabled, the full suite exposes a pre-existing stack buffer over-read while saving EXIF metadata. It reproduces identically on baseline and fixed builds. The JP2 callback test and the other 10 tests pass in that JXR-enabled fixed build; |
|
okay @haobinnan I got my local ai models to really smash the analysis on this one. Qwen and Glimmer and I think it's fixed. Goes back a long way |
When a JP2/J2K read callback rejects
SEEK_END, the stream adapter ignores the error and can declare a zero-byte stream to OpenJPEG. Reading the signature then violates OpenJPEG's stream-length invariant and aborts Debug builds with the assertion reported in #198.Validate both tell results, the seek to EOF, restoration of the original position, and a positive readable length before creating the decoder stream. Invalid probes now make
FreeImage_LoadFromHandle()returnNULL. Output streams no longer run an unnecessary length probe. Successful lengths also avoid the old intermediateunsignedtruncation.Add a self-contained callback regression test for JP2 and raw J2K, covering seven callback faults, signature detection, full/header-only loading, exact lossless pixels, and saving without
SEEK_END. Run it in the existing JP2/JXR workflow on Linux, macOS, and MSVC static/shared builds, with Windows DLL deployment.Validation on macOS, Debug + ASan, against pre-fix master
c210b8b:SEEK_ENDis rejected; fixed builds returnNULLwithout an ASan report. Normal callbacks still load all four files.MainTestSuitepass.MainTestSuiteexposes the same pre-existing EXIF/JXR stack over-read atExif.cpp:1228in both baseline and fixed builds; it is not fixed by this PR.Refs #198. This prevents a reproduced cause of the assertion; the reporter's actual callback implementation is still needed to confirm their exact trigger. Historical analysis and the reason existing tests missed it are in the investigation comment.