Skip to content

Fix memory leak in JPEG EXIF rotation (rotate90/rotate270)#2574

Open
iurisilvio wants to merge 1 commit into
Automattic:masterfrom
iurisilvio:fix/jpeg-rotate-leak
Open

Fix memory leak in JPEG EXIF rotation (rotate90/rotate270)#2574
iurisilvio wants to merge 1 commit into
Automattic:masterfrom
iurisilvio:fix/jpeg-rotate-leak

Conversation

@iurisilvio
Copy link
Copy Markdown

Fixes #2572.

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 1200×1800 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 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;
   };

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak in JPEG EXIF rotation: rotate90/rotate270 leak the rotated pixel buffer

1 participant