Skip to content

Use delete[] to free new[]-allocated buffer in decodeJPEGIntoSurface OOM path - #2575

Merged
chearon merged 1 commit into
Automattic:masterfrom
iurisilvio:fix/jpeg-decode-allocator-mismatch
Jun 21, 2026
Merged

Use delete[] to free new[]-allocated buffer in decodeJPEGIntoSurface OOM path#2575
chearon merged 1 commit into
Automattic:masterfrom
iurisilvio:fix/jpeg-decode-allocator-mismatch

Conversation

@iurisilvio

Copy link
Copy Markdown
Contributor

Fixes #2573.

Image::decodeJPEGIntoSurface allocates a buffer with new uint8_t[] and frees it with free() on the OOM error path — undefined behavior in C++ (new[] and free() may use different allocators, and even when they share one they may have different bookkeeping for the array size header).

Every other deallocation of new uint8_t[] buffers in Image.cc (lines 710, 898, 901, 991) already uses delete[] correctly; this is the only outlier. Caught by g++ -Wmismatched-new-delete:

../src/Image.cc:844:9: warning: 'void free(void*)' called on pointer returned from a mismatched allocation function [-Wmismatched-new-delete]
  844 |     free(data);
      |     ~~~~^~~~~~
../src/Image.cc:834:70: note: returned from 'void* operator new []'(std::size_t)'
  834 |   uint8_t *data = new uint8_t[naturalWidth * naturalHeight * channels];

The triggering condition (the second new uint8_t[] returning nullptr after the first succeeds) is unlikely in practice, but the bug is real and the fix is one character.

`Image::decodeJPEGIntoSurface` allocates a buffer with `new uint8_t[]`
and freed it with `free()` on the OOM error path — undefined behavior
in C++ (`new[]` and `free()` may use different allocators, and even
when they share one they may have different bookkeeping for the array
size header).

Every other deallocation of `new uint8_t[]` buffers in Image.cc uses
`delete[]` correctly; this is the only outlier. Caught by
`g++ -Wmismatched-new-delete`.

Fixes Automattic#2573
@chearon
chearon merged commit 8e07bc7 into Automattic:master Jun 21, 2026
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.

Allocator mismatch in decodeJPEGIntoSurface OOM path: free() called on new[]-allocated buffer

2 participants