René's URL Explorer Experiment


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

direct link

Domain: github.com

route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:eac70f6e-3490-84bf-513b-c71a203fa9bd
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idC612:2FB335:2967A9:381DA3:6A525984
html-safe-nonceef211201ddbcdd69390edaf6fa95e0b3b29898ceb8ca8f073165792bb19ac355
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNjEyOjJGQjMzNToyOTY3QTk6MzgxREEzOjZBNTI1OTg0IiwidmlzaXRvcl9pZCI6IjI2NDAyNDA5MTQwMzE4NjAxMDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac498d42b0c931b3bd38467e48d4e42689bbac99c044e21d28c6ae441131716880
hovercard-subject-tagpull_request:3793470472
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/msgpack/msgpack-python/pull/687/files
twitter:imagehttps://avatars.githubusercontent.com/in/1143301?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/in/1143301?s=400&v=4
og:image:altUnpacker.__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_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
Noneb9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/msgpack/msgpack-python git https://github.com/msgpack/msgpack-python.git
octolytics-dimension-user_id198264
octolytics-dimension-user_loginmsgpack
octolytics-dimension-repository_id2242705
octolytics-dimension-repository_nwomsgpack/msgpack-python
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id2242705
octolytics-dimension-repository_network_root_nwomsgpack/msgpack-python
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release7aed05249554b889eb33d002851a973eebcc7e91
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/msgpack/msgpack-python/pull/687/files#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmsgpack%2Fmsgpack-python%2Fpull%2F687%2Ffiles
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmsgpack%2Fmsgpack-python%2Fpull%2F687%2Ffiles
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=msgpack%2Fmsgpack-python
Reloadhttps://github.com/msgpack/msgpack-python/pull/687/files
Reloadhttps://github.com/msgpack/msgpack-python/pull/687/files
Reloadhttps://github.com/msgpack/msgpack-python/pull/687/files
Please reload this pagehttps://github.com/msgpack/msgpack-python/pull/687/files
msgpack https://github.com/msgpack
msgpack-pythonhttps://github.com/msgpack/msgpack-python
Notifications https://github.com/login?return_to=%2Fmsgpack%2Fmsgpack-python
Fork 249 https://github.com/login?return_to=%2Fmsgpack%2Fmsgpack-python
Star 2.1k https://github.com/login?return_to=%2Fmsgpack%2Fmsgpack-python
Code https://github.com/msgpack/msgpack-python
Issues 3 https://github.com/msgpack/msgpack-python/issues
Pull requests 5 https://github.com/msgpack/msgpack-python/pulls
Discussions https://github.com/msgpack/msgpack-python/discussions
Actions https://github.com/msgpack/msgpack-python/actions
Security and quality 1 https://github.com/msgpack/msgpack-python/security
Insights https://github.com/msgpack/msgpack-python/pulse
Code https://github.com/msgpack/msgpack-python
Issues https://github.com/msgpack/msgpack-python/issues
Pull requests https://github.com/msgpack/msgpack-python/pulls
Discussions https://github.com/msgpack/msgpack-python/discussions
Actions https://github.com/msgpack/msgpack-python/actions
Security and quality https://github.com/msgpack/msgpack-python/security
Insights https://github.com/msgpack/msgpack-python/pulse
Sign up for GitHub https://github.com/signup?return_to=%2Fmsgpack%2Fmsgpack-python%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fmsgpack%2Fmsgpack-python%2Fissues%2Fnew%2Fchoose
methanehttps://github.com/methane
mainhttps://github.com/msgpack/msgpack-python/tree/main
copilot/fix-unpacker-init-bugshttps://github.com/msgpack/msgpack-python/tree/copilot/fix-unpacker-init-bugs
Conversation 1 https://github.com/msgpack/msgpack-python/pull/687
Commits 3 https://github.com/msgpack/msgpack-python/pull/687/commits
Checks 35 https://github.com/msgpack/msgpack-python/pull/687/checks
Files changed 2 https://github.com/msgpack/msgpack-python/pull/687/files
Harden Unpacker.__init__ re-entry cleanup to prevent buffer/context leaks https://github.com/msgpack/msgpack-python/pull/687/files#top
Show all changes 3 commits https://github.com/msgpack/msgpack-python/pull/687/files
6afc0cc Initial plan Copilot Jun 3, 2026 https://github.com/msgpack/msgpack-python/pull/687/commits/6afc0cc2ed240dbb818d6fc1e306955fa5170796
ffc1b20 Fix Unpacker __init__ re-entry cleanup leaks Copilot Jun 3, 2026 https://github.com/msgpack/msgpack-python/pull/687/commits/ffc1b206db400317d4ebc1e06306f4d23f265eb7
23da514 Apply suggestion from @methane methane Jun 3, 2026 https://github.com/msgpack/msgpack-python/pull/687/commits/23da5146a44848587eee11b8159b251b5ecf3951
Clear filters https://github.com/msgpack/msgpack-python/pull/687/files
Please reload this pagehttps://github.com/msgpack/msgpack-python/pull/687/files
Please reload this pagehttps://github.com/msgpack/msgpack-python/pull/687/files
_unpacker.pyx https://github.com/msgpack/msgpack-python/pull/687/files#diff-407b1aba76cd8909a978c41341f7fe756567d21c6dab7c7568ca6075614531b1
test_unpack.py https://github.com/msgpack/msgpack-python/pull/687/files#diff-a38bda78674f20f297abfaafa9134ac6025345225b79c4c0012aa592740c2c7d
msgpack/_unpacker.pyxhttps://github.com/msgpack/msgpack-python/pull/687/files#diff-407b1aba76cd8909a978c41341f7fe756567d21c6dab7c7568ca6075614531b1
View file https://github.com/msgpack/msgpack-python/blob/23da5146a44848587eee11b8159b251b5ecf3951/msgpack/_unpacker.pyx
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/msgpack/msgpack-python/pull/687/{{ revealButtonHref }}
https://github.com/msgpack/msgpack-python/pull/687/files#diff-407b1aba76cd8909a978c41341f7fe756567d21c6dab7c7568ca6075614531b1
https://github.com/msgpack/msgpack-python/pull/687/files#diff-407b1aba76cd8909a978c41341f7fe756567d21c6dab7c7568ca6075614531b1
https://github.com/msgpack/msgpack-python/pull/687/files#diff-407b1aba76cd8909a978c41341f7fe756567d21c6dab7c7568ca6075614531b1
test/test_unpack.pyhttps://github.com/msgpack/msgpack-python/pull/687/files#diff-a38bda78674f20f297abfaafa9134ac6025345225b79c4c0012aa592740c2c7d
View file https://github.com/msgpack/msgpack-python/blob/23da5146a44848587eee11b8159b251b5ecf3951/test/test_unpack.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/msgpack/msgpack-python/pull/687/{{ revealButtonHref }}
https://github.com/msgpack/msgpack-python/pull/687/files#diff-a38bda78674f20f297abfaafa9134ac6025345225b79c4c0012aa592740c2c7d
https://github.com/msgpack/msgpack-python/pull/687/files#diff-a38bda78674f20f297abfaafa9134ac6025345225b79c4c0012aa592740c2c7d
Please reload this pagehttps://github.com/msgpack/msgpack-python/pull/687/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.