Title: Harden `Unpacker.__init__` re-entry cleanup to prevent buffer/context leaks by Copilot · Pull Request #687 · msgpack/msgpack-python · GitHub
Open Graph Title: Harden `Unpacker.__init__` re-entry cleanup to prevent buffer/context leaks by Copilot · Pull Request #687 · msgpack/msgpack-python
X Title: Harden `Unpacker.__init__` re-entry cleanup to prevent buffer/context leaks by Copilot · Pull Request #687 · msgpack/msgpack-python
Description: 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)), reset parser bookkeeping (unpack_init(&self.ctx)), free old internal buffer when present (PyMem_Free(self.buf)), null out the pointer before reallocating (self.buf = NULL). Regression coverage for re-initialization Add a focused unpacker test that drives a mid-parse state (live object held in parse stack), re-calls __init__, and asserts: prior stack-held object is released, parser is reset to empty state, unpacker remains reusable for new input. # __init__ re-entry prologue unpack_clear(&self.ctx) unpack_init(&self.ctx) if self.buf != NULL: PyMem_Free(self.buf) self.buf = NULL Original prompt Create a pull request in repository msgpack/msgpack-python to fix the two Unpacker.__init__ re-initialization bugs described in issue #683. Repository: msgpack/msgpack-python Issue context: Issue #683 reports two related bugs in Unpacker.__init__: 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. Open a pull request that references issue #683 and explains that it fixes both the buffer leak and parse-stack leak on Unpacker.__init__ re-entry. Suggested validation: Run relevant tests for unpacker behavior. If practical, add a regression test covering repeated __init__ calls or subclass reinitialization. The following is the prior conversation context from the user's chat exploration (may be truncated): User: Packer.__getbuffer__ と __releasebuffer__ に @cython.critical_section を追加するPRを作って。 Assistant: PR作成の確認待ちです。GitHub 側の承認が完了したら、その変更で続けて進めます。 User: @copilot Accepted Confirmation: Confirm cloud agent session Assistant: 開始しました。進捗はタスクページで追えます。 User: "Unpacker.init overwrites self.buf via PyMem_Malloc without first freeing" と "Unpacker.init calls unpack_init (not unpack_clear) before re-initializing" を修正するPRを作成して。 Assistant: 確認待ちです。GitHub の承認ダイアログで許可してもらえれば、この PR 作成を続けます。 This pull request was created from Copilot chat.
Open Graph Description: 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...
X Description: 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...
Opengraph URL: https://github.com/msgpack/msgpack-python/pull/687
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:eac70f6e-3490-84bf-513b-c71a203fa9bd |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C612:2FB335:2967A9:381DA3:6A525984 |
| html-safe-nonce | ef211201ddbcdd69390edaf6fa95e0b3b29898ceb8ca8f073165792bb19ac355 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNjEyOjJGQjMzNToyOTY3QTk6MzgxREEzOjZBNTI1OTg0IiwidmlzaXRvcl9pZCI6IjI2NDAyNDA5MTQwMzE4NjAxMDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 498d42b0c931b3bd38467e48d4e42689bbac99c044e21d28c6ae441131716880 |
| hovercard-subject-tag | pull_request:3793470472 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/msgpack/msgpack-python/pull/687/files |
| twitter:image | https://avatars.githubusercontent.com/in/1143301?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/in/1143301?s=400&v=4 |
| og:image:alt | 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... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/msgpack/msgpack-python git https://github.com/msgpack/msgpack-python.git |
| octolytics-dimension-user_id | 198264 |
| octolytics-dimension-user_login | msgpack |
| octolytics-dimension-repository_id | 2242705 |
| octolytics-dimension-repository_nwo | msgpack/msgpack-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 2242705 |
| octolytics-dimension-repository_network_root_nwo | msgpack/msgpack-python |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width