Skip to content

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

Open
iurisilvio wants to merge 1 commit into
Automattic:masterfrom
iurisilvio:fix/jpeg-decode-allocator-mismatch
Open

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

Conversation

@iurisilvio
Copy link
Copy Markdown

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
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

1 participant