Check exported buffers after the default callback returns - #735
Draft
AbhinavMir wants to merge 1 commit into
Draft
Check exported buffers after the default callback returns#735AbhinavMir wants to merge 1 commit into
AbhinavMir wants to merge 1 commit into
Conversation
The default callback can export the internal buffer with getbuffer(). The packer then continues and can reallocate that buffer. The export points at freed memory after the reallocation. Call _check_exports() after the callback returns. The packer now raises BufferError. The pure Python packer already raises BufferError in this case.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Other supported user-code hooks can still export the buffer before a reallocating write.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Prevents buffer invalidation after default exports the packer’s internal buffer.
Changes:
- Checks active exports after
defaultreturns. - Adds regression coverage for the unsafe reallocation scenario.
File summaries
| File | Description |
|---|---|
msgpack/_packer.pyx |
Adds post-callback export validation. |
test/test_buffer.py |
Tests exported-buffer safety during callbacks. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+267
to
+269
| # The callback may have exported the internal buffer. | ||
| # Packing on would reallocate it and invalidate the export. | ||
| self._check_exports() |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Packer.pack()calls thedefaultcallback for an unsupported object. The callback can export the internal buffer withgetbuffer()ormemoryview(packer). The packer then packs the value that the callback returns.msgpack_pack_write()inpack.hreallocates the buffer when that value does not fit. The export then points at freed memory. A read of the export is a heap use-after-free._check_exports()already guards the six public entry points ofPacker. It does not run again after the callback returns. This change calls it right after the callback returns.Packer.pack()now raisesBufferError.The pure Python packer already raises
BufferErrorfor the same code, becauseBytesIO.write()refuses to resize a buffer that has an export. This change makes the two implementations agree.I also considered a guard inside
msgpack_pack_write(). That needs an export count in the plain Cmsgpack_packerstruct. The callback is the only place that runs user code during a pack, so I kept the check at the call site. I am happy to move it if you prefer the lower layer.The new test is
test/test_buffer.py::test_packer_getbuffer_in_default. It sits next to the existingtest_packer_getbuffertest. It passes for both the C extension and the pure Python fallback. It fails on the current code, because the pack succeeds and leaves a dangling export.