Skip to content

Clear borrowed C pointers before re-initializing Packer and Unpacker - #776

Open
ravi0800 wants to merge 1 commit into
msgpack:mainfrom
ravi0800:reinit-borrowed-pointers
Open

ravi0800 wants to merge 1 commit into
msgpack:mainfrom
ravi0800:reinit-borrowed-pointers

Conversation

@ravi0800

Copy link
Copy Markdown

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.

@oakkaya

oakkaya commented Nov 23, 2026

Copy link
Copy Markdown

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):

  1. 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.
  2. 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.
  3. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants