Fix memory leak in JPEG EXIF rotation (rotate90/rotate270) - #2574
Merged
Conversation
The rotate90 and rotate270 lambdas in Image::rotatePixels allocate a temporary 'unrotated' buffer via 'new uint8_t[n_bytes]' but never free it. Every JPEG decoded with EXIF orientation 5, 6, 7, or 8 leaks the full rotated pixel buffer (width * height * 4 bytes) on each loadImage call. For a 1200x1800 test image (recurser/exif-orientation-examples Landscape_6.jpg, EXIF Orientation=6), 100 sequential loads leak 837 MiB (~8.4 MiB/iter, matching width * height * 4 = 8,640,000 bytes exactly). After this fix, RSS plateaus after the first ~10 iterations. Fixes Automattic#2572
chearon
approved these changes
Jun 21, 2026
iurisilvio
added a commit
to iurisilvio/node-canvas
that referenced
this pull request
Jun 25, 2026
Trim the fork to only the memory leaks reproduced with before/after RSS: - JPEG EXIF rotation (rotate90/rotate270) leak (Automattic#2574, merged upstream) - RsvgHandle / partial cairo surface leak on SVG decode error paths (Automattic#2585) Revert everything else back to v3.2.3 after testing: image.src-on-error (Automattic#2580, no measurable effect), libjpeg longjmp data/src free (Automattic#2581, not reproducible on libjpeg-turbo), cairo_pattern_t refcount (Automattic#2582), CanvasPattern source-retention UAF guard (Automattic#2583), rare decoder error-path frees (Automattic#2587), and the node-addon-api 8 / NAPI_EXPERIMENTAL experiment. Built on node-addon-api 7. Co-Authored-By: Claude Opus 4.8 <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 #2572.
The
rotate90androtate270lambdas inImage::rotatePixelsallocate a temporaryunrotatedbuffer vianew uint8_t[n_bytes]but never free it. Every JPEG decoded with EXIF orientation 5, 6, 7, or 8 leaks the full rotated pixel buffer (width * height * 4bytes) on eachloadImagecall.For a 1200×1800 test image (
recurser/exif-orientation-examplesLandscape_6.jpg, EXIFOrientation=6), 100 sequential loads leak 837 MiB (~8.4 MiB/iter, matchingwidth * height * 4 = 8,640,000bytes exactly). After this fix, RSS plateaus after the first ~10 iterations and stays flat.The full repro script and observed numbers are in #2572.
Diff is one line added to each lambda:
auto rotate90 = [](uint8_t* pixels, int width, int height, int channels) { ... + delete[] unrotated; }; auto rotate270 = [](uint8_t* pixels, int width, int height, int channels) { ... + delete[] unrotated; };