Repository navigation
Conversation
…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>
fallenmi
left a comment
There was a problem hiding this comment.
Codex-assisted review of exact head b1979d5.
The new PyModule_AddObject() failure branches still leak one strong reference to each exception class. PyErr_NewExceptionWithDoc() returns one new reference, and the following Py_INCREF() makes two caller-owned references. CPython documents that PyModule_AddObject() steals a reference only on success, so when it returns < 0 both references are still owned here. The added branch decrements the exception only once; Py_DECREF(module) cannot release the other reference because the add failed. The static LZ4BlockError / LZ4StreamError pointer is also left non-NULL after initialization returns NULL.
Please release the original reference too and clear the static pointer on failure, or move the Py_INCREF() after a successful add (then a single decref plus clearing the pointer handles failure). The same reference-count path is present in both _block.c and _stream.c. The 36 green checks exercise normal initialization, not this forced-error branch.
Summary
Py_DECREF(module)before returning NULL whenPyErr_NewExceptionWithDocfails in both_blockand_streammodule init — previously leaked the module objectPyModule_AddObjectreturn value when addingLZ4BlockErrorandLZ4StreamError— previously ignored failures, leaking the exception class referenceContext
PyModule_AddObjectsteals a reference on success but not on failure. The previous code did not check its return value, so on failure thePy_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 usePyModule_AddObjectwith 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