You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Clear borrowed C pointers before re-initializing Packer and Unpacker - #776
Packer.__init__ and Unpacker.__init__ replace the attributes that own unicode_errors and the object/list/ext hooks before the C side pointers borrowed from them are refreshed. When a later step of a re-__init__ raises (for example unicode_errors=1, or read_size > max_buffer_size), the old objects are already released, so the next unpack() calls a freed object_hook (access violation on CPython 3.10/Windows) and the next pack() passes a freed error handler name to PyUnicode_AsEncodedString. In Unpacker the failed path can also keep the old buf_size/buf_head/buf_tail after self.buf was freed, so feed() copies into a NULL buffer.
Clear the borrowed pointers and the buffer bookkeeping at the top of __init__, before their owners are replaced, so a failed __init__ leaves the object with no hooks and an empty buffer instead of dangling state. The two new tests crash or fail on main and pass here; the full suite passes for both the C extension and MSGPACK_PUREPYTHON=1.
Independent ASan/UBSan review of this patch (applied cleanly to HEAD e3a7160, C extension rebuilt with -fsanitize=address,undefined):
The fix is correct for the three borrowed-pointer lifetimes it targets. On unpatched HEAD I reproduced all three failures — a freed object_hook being called, a NULL-destination append causing a SEGV in Unpacker_append_buffer (_cmsgpack.c:15509), and a dangling unicode_errors pointer — and all three are resolved with the patch applied. A 16,000-sequence / 151,808-op stateful campaign on the patched tree was clean: 0 crashes, 0 ASan errors, 0 UBSan fatals (positive control on unpatched HEAD crashes at seed 0). The guards are correctly placed before the owners are replaced.
Three residual correctness items I noticed while reviewing (none are memory-safety on the default path, so just flagging them):
If ext_hook=None is passed to a failed __init__ that is then re-initialized, the cleared ext_hook pointer can lead to an AssertionError on the next unpack() that hits an EXT type, rather than a clean state.
A stale file_like from the failed init can still block a subsequent feed() with an assert, because that reference is not cleared with the other borrowed pointers.
With buf=NULL, head=tail=0, the buffer reset does a zero-length memcpy(dest, NULL, 0), which UBSan flags (_cmsgpack.c:15471:14: null pointer passed as argument 2). It is harmless in practice (0 bytes, no dereference), but it is a persistent UBSan warning under -fsanitize=undefined.
Overall the patch is a clear improvement and I would support merging it; the above are nit-level / follow-up candidates. Happy to share the reproducer scripts if useful.
This branch has not been deployed
No deployments
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.
Packer.__init__andUnpacker.__init__replace the attributes that ownunicode_errorsand the object/list/ext hooks before the C side pointers borrowed from them are refreshed. When a later step of a re-__init__raises (for exampleunicode_errors=1, orread_size > max_buffer_size), the old objects are already released, so the nextunpack()calls a freedobject_hook(access violation on CPython 3.10/Windows) and the nextpack()passes a freed error handler name toPyUnicode_AsEncodedString. InUnpackerthe failed path can also keep the oldbuf_size/buf_head/buf_tailafterself.bufwas freed, sofeed()copies into a NULL buffer.Clear the borrowed pointers and the buffer bookkeeping at the top of
__init__, before their owners are replaced, so a failed__init__leaves the object with no hooks and an empty buffer instead of dangling state. The two new tests crash or fail on main and pass here; the full suite passes for both the C extension andMSGPACK_PUREPYTHON=1.