Skip to content

Commit 55f5cdd

Browse files
vyrnsynxSiyet
andauthored
fix(msgpack): raise DecodeError for unhashable map keys (#1209)
## Problem `msgspec.msgpack.decode` currently raises a bare `TypeError` when a MessagePack map or `ext` is used as a map key. That escapes the documented `DecodeError` contract, so `except msgspec.DecodeError` does not catch it. ## Fix In `mpack_decode_dict`, translate `TypeError` from `PyDict_SetItem` into `DecodeError`. Other errors (e.g. OOM) are left unchanged. `mpack_decode_frozendict` already goes through this path. ## Tests Adds coverage for map/ext keys (`818001`, `878048`, `8bd4ec5c03`) and keeps the hashable array-key control (`819001` → `{(): 1}`). Fixes #1208 --------- Co-authored-by: Tseluiko Aleksandr <[email protected]>
1 parent b2d1b7a commit 55f5cdd

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/msgspec/_core.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16290,8 +16290,17 @@ mpack_decode_dict(
1629016290
val = mpack_decode(self, val_type, &val_path, false);
1629116291
if (MS_UNLIKELY(val == NULL))
1629216292
goto error;
16293-
if (MS_UNLIKELY(PyDict_SetItem(res, key, val) < 0))
16293+
if (MS_UNLIKELY(PyDict_SetItem(res, key, val) < 0)) {
16294+
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
16295+
PyErr_Clear();
16296+
PyErr_Format(
16297+
msgspec_get_global_state()->DecodeError,
16298+
"MessagePack data is malformed: map keys must be hashable (byte %zd)",
16299+
(Py_ssize_t)(self->input_pos - self->input_start)
16300+
);
16301+
}
1629416302
goto error;
16303+
}
1629516304
Py_CLEAR(key);
1629616305
Py_CLEAR(val);
1629716306
}

‎tests/unit/test_msgpack.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,21 @@ def test_decode_invalid_opcode_error(self):
539539
with pytest.raises(msgspec.DecodeError, match="invalid opcode '\\\\xc1'"):
540540
msgspec.msgpack.decode(b"\xc1abc")
541541

542+
@pytest.mark.parametrize(
543+
"hex_data",
544+
[
545+
"818001", # map used as map key
546+
"878048", # map used as map key
547+
"8bd4ec5c03", # ext used as map key
548+
],
549+
)
550+
def test_decode_unhashable_map_key_raises_decode_error(self, hex_data):
551+
with pytest.raises(msgspec.DecodeError, match="map keys must be hashable"):
552+
msgspec.msgpack.decode(bytes.fromhex(hex_data))
553+
554+
def test_decode_hashable_array_map_key_ok(self):
555+
assert msgspec.msgpack.decode(bytes.fromhex("819001")) == {(): 1}
556+
542557
def test_decode_skip_invalid_submessage_raises(self):
543558
"""Ensure errors in submessage skipping are raised"""
544559

0 commit comments

Comments
 (0)