Skip to content

Commit

Permalink
Fixed exceptions raised from decoder object hook causing SystemError
Browse files Browse the repository at this point in the history
Fixes #201.
  • Loading branch information
agronholm committed Dec 27, 2023
1 parent af84761 commit 415599f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
5 changes: 4 additions & 1 deletion docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ This library adheres to `Semantic Versioning <http://semver.org/>`_.

**UNRELEASED**

- Fixed ``SystemError`` when decoding a ``Fractional`` with a bad number of arguments
- Fixed ``SystemError`` in the C extension when decoding a ``Fractional`` with a bad
number of arguments
- Fixed ``SystemError`` in the C extension when the decoder object hook raises an
exception
- Fixed a segmentation fault when decoding invalid unicode data

**5.5.1** (2023-11-02)
Expand Down
11 changes: 6 additions & 5 deletions source/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -939,11 +939,12 @@ decode_map(CBORDecoderObject *self, uint8_t subtype)
}
if (ret && self->object_hook != Py_None) {
map = PyObject_CallFunctionObjArgs(self->object_hook, self, ret, NULL);
if (map) {
set_shareable(self, map);
Py_DECREF(ret);
ret = map;
}
if (!map)
return NULL;

set_shareable(self, map);
Py_DECREF(ret);
ret = map;
}
return ret;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/test_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,15 @@ def __init__(self, state):
assert decoded.state == {"a": 3, "b": 5}


def test_object_hook_exception(impl):
def object_hook(decoder, data):
raise RuntimeError("foo")

payload = unhexlify("A2616103616205")
with pytest.raises(RuntimeError, match="foo"):
impl.loads(payload, object_hook=object_hook)


def test_load_from_file(impl, tmpdir):
path = tmpdir.join("testdata.cbor")
path.write_binary(b"\x82\x01\x0a")
Expand Down

0 comments on commit 415599f

Please sign in to comment.