Skip to content

Fix memory and buffer leaks on error paths#330

Open
devdanzin wants to merge 1 commit intopython-lz4:masterfrom
devdanzin:fix/memory-leaks-error-paths
Open

Fix memory and buffer leaks on error paths#330
devdanzin wants to merge 1 commit intopython-lz4:masterfrom
devdanzin:fix/memory-leaks-error-paths

Conversation

@devdanzin
Copy link
Copy Markdown

Summary

  • Fix Py_buffer leak in block.compress() and block.decompress() when PyMem_Malloc fails — source and dict buffers not released before returning PyErr_NoMemory()
  • Fix Py_buffer leak in frame.compress() when block_checksum is requested on LZ4 < 1.8.0 — source buffer not released before error return
  • Fix destination memory leak in frame.compress_begin() when LZ4F_compressBegin fails — allocated buffer not freed before error return (same pattern as the bug fixed in commit 43fe65d, not propagated to this site)
  • Fix destination memory leak in frame.__decompress() when PyMem_Realloc fails — original buffer pointer lost on realloc failure

Context

These are the same class of bug that was partially fixed in commit 43fe65d. That fix addressed one error path in __decompress but missed the PyMem_Realloc failure path in the same function and the identical pattern in compress_begin.

Found using cext-review-toolkit.

Note

This PR was authored and submitted by Claude Code (Anthropic).
It was reviewed by a human before submission.

Test plan

  • Existing tests pass
  • Code review confirms each error path now releases all acquired resources

Fix memory and buffer leaks on five error paths:

- block/compress: release source and dict Py_buffers on PyMem_Malloc failure
- block/decompress: release source and dict Py_buffers on PyMem_Malloc failure
- frame/compress: release source Py_buffer on block_checksum version error
- frame/compress_begin: free destination buffer on LZ4F_compressBegin failure
  (same pattern as the bug fixed in 43fe65d, not propagated to this site)
- frame/__decompress: free original destination buffer on PyMem_Realloc failure
  (realloc returns NULL without freeing the original pointer)

Found using cext-review-toolkit (https://github.com/devdanzin/cext-review-toolkit).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

1 participant