Track data and src on canvas_jpeg_error_mgr to free on libjpeg longjmp#2581
Open
iurisilvio wants to merge 1 commit into
Open
Track data and src on canvas_jpeg_error_mgr to free on libjpeg longjmp#2581iurisilvio wants to merge 1 commit into
iurisilvio wants to merge 1 commit into
Conversation
If libjpeg calls error_exit during jpeg_read_scanlines (corrupt or truncated JPEG that decoded the header but fails in the scan), it longjmps back to the setjmp point in loadJPEGFromBuffer. That bypasses the cleanup at the bottom of decodeJPEGIntoSurface, leaking both 'data' (width * height * 4) and 'src' (width * output_components). Store both pointers on the canvas_jpeg_error_mgr so the error handler can delete[] them before longjmp. Also reorder cleanup to destroy libjpeg state and free src before creating the cairo surface, so the success path is symmetric with the new failure path.
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 #2577.
When libjpeg calls
error_exitduringjpeg_read_scanlines()(the common failure for corrupt or truncated JPEGs where the header decodes but the entropy-coded scan data is malformed), it doeslongjmpback to the setjmp inloadJPEGFromBuffer. That bypasses the cleanup code indecodeJPEGIntoSurface, leaking bothdata(width * height * 4bytes) andsrc(scanline buffer) on every call.Standalone repro on
canvas@3.2.3shows ~4 MiB leaked per call — process OOMs in seconds under sustained load on malformed JPEGs. Full numbers in #2577.The fix tracks both pointers on
canvas_jpeg_error_mgrso the error handler candelete[]them before the longjmp. The decode flow is reordered sojpeg_destroy_decompressanddelete[] srchappen before the cairo surface is created — that way the success path's ownership transfer is symmetric with the failure path, with no double-free.