Skip to content

Fix module init error handling in block and stream modules#333

Open
devdanzin wants to merge 1 commit into
python-lz4:masterfrom
devdanzin:fix/module-init-error-handling
Open

Fix module init error handling in block and stream modules#333
devdanzin wants to merge 1 commit into
python-lz4:masterfrom
devdanzin:fix/module-init-error-handling

Conversation

@devdanzin

Copy link
Copy Markdown

Summary

  • Add Py_DECREF(module) before returning NULL when PyErr_NewExceptionWithDoc fails in both _block and _stream module init — previously leaked the module object
  • Check PyModule_AddObject return value when adding LZ4BlockError and LZ4StreamError — previously ignored failures, leaking the exception class reference

Context

PyModule_AddObject steals a reference on success but not on failure. The previous code did not check its return value, so on failure the Py_INCREF'd exception class was leaked. The fix checks the return and decrefs both the exception and the module on failure.

Note: PyModule_AddObjectRef (Python 3.10+) would simplify this, but the project supports Python 3.9 so we use PyModule_AddObject with proper error checking.

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
  • Module init failure paths correctly clean up all references

…Object

- Add Py_DECREF(module) before returning NULL when PyErr_NewExceptionWithDoc
  fails in both block and stream module init — previously leaked the module
- Check PyModule_AddObject return value when adding LZ4BlockError and
  LZ4StreamError — previously ignored failures, leaking the exception ref

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