Skip to content

Validate extension type codes in Packer.pack_ext_type - #751

Open
zhechen-cs wants to merge 1 commit into
msgpack:mainfrom
zhechen-cs:codex/validate-extension-type-codes
Open

zhechen-cs wants to merge 1 commit into
msgpack:mainfrom
zhechen-cs:codex/validate-extension-type-codes

Conversation

@zhechen-cs

Copy link
Copy Markdown

The C extension accepts pack_ext_type(1.5, b"A") and silently writes type code 1. It also accepts negative codes reserved by the MessagePack format, including -1 for timestamps. The pure-Python packer and ExtType already reject these inputs.

Validate the code before writing an extension header, matching the fallback's TypeError for non-integers and ValueError outside 0–127. Tests cover both autoreset modes, unchanged buffered output after a rejected call, subsequent valid packing, and the two valid boundaries.

Validation on macOS arm64, CPython 3.12:

  • The new tests fail on the unmodified C extension: 14 failed, 6 passed.
  • C extension full suite: 165 passed, 1 skipped.
  • Pure-Python full suite: 162 passed, 4 skipped.
  • Ruff lint and format checks, and git diff --check, pass.

The existing thread_unsafe marker warning is also present on the unmodified base. Other platforms and free-threaded Python are left to CI.

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.

1 participant