Unpacker.__init__ did not safely handle repeated initialization: it replaced self.buf without freeing the previous allocation and reinitialized parser state without clearing in-flight stack objects. This PR makes re-entry cleanup explicit and deterministic.
Unpacker lifecycle cleanup
Initialize parser context in __cinit__ (unpack_init(&self.ctx)) so cleanup paths always operate on initialized state.
On every __init__ entry:
clear parse-stack state (unpack_clear(&self.ctx)),
Unpacker.__init__ overwrites self.buf via PyMem_Malloc without first freeing the existing buffer, so re-calling __init__ (including subclass super().__init__(...)) leaks the previous allocation.
Unpacker.__init__ calls unpack_init rather than clearing existing parser state first, so any live PyObject* references in ctx->stack[1..top] are leaked when re-initializing a mid-stream Unpacker.
Requested change:
Update the Unpacker.__init__ implementation in the Cython source so that, before allocating a new buffer or reinitializing the unpack context, it safely cleans up any existing state.
Specifically, if the instance already has an allocated buffer / active unpack context, clear parser state with unpack_clear(&self.ctx), free the old buffer with PyMem_Free(self.buf), and reset self.buf appropriately before continuing initialization.
Requirements:
Make the fix in the relevant Cython source file for Unpacker.
Ensure the cleanup path is safe for first initialization as well as repeated __init__ calls.
Keep the PR focused on these two Unpacker.__init__ bugs only.
Regenerate checked-in generated files only if the repository expects that for Cython source changes.
Add or update tests if there is an appropriate place to cover re-initialization behavior.
User: "Unpacker.init overwrites self.buf via PyMem_Malloc without first freeing" と "Unpacker.init calls unpack_init (not unpack_clear) before re-initializing" を修正するPRを作成して。
CopilotAI
changed the title
[WIP] Fix Unpacker.__init__ re-initialization bugs
Harden Unpacker.__init__ re-entry cleanup to prevent buffer/context leaks
Jun 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Unpacker.__init__ did not safely handle repeated initialization: it replaced self.buf without freeing the previous allocation and reinitialized parser state without clearing in-flight stack objects. This PR makes re-entry cleanup explicit and deterministic.
Unpacker lifecycle cleanup
Regression coverage for re-initialization
This pull request was created from Copilot chat.