Skip to content

Commit

Permalink
Fixed SystemError when decoding bad data for Fractional
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Dec 26, 2023
1 parent 850545c commit c7ef1df
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Version history

This library adheres to `Semantic Versioning <http://semver.org/>`_.

**UNRELEASED**

- Fixed ``SystemError`` when decoding a ``Fractional`` with a bad number of arguments

**5.5.1** (2023-11-02)

- Fixed ``CBORSimpleValue`` allowing the use of reserved values (24 to 31) which resulted in
Expand Down
12 changes: 5 additions & 7 deletions source/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1367,16 +1367,14 @@ CBORDecoder_decode_rational(CBORDecoderObject *self)
// NOTE: see semantic type 4
tuple = decode(self, DECODE_IMMUTABLE | DECODE_UNSHARED);
if (tuple) {
if (PyTuple_CheckExact(tuple) && PyTuple_GET_SIZE(tuple) == 2) {
ret = PyObject_CallFunctionObjArgs(
_CBOR2_Fraction,
PyTuple_GET_ITEM(tuple, 0),
PyTuple_GET_ITEM(tuple, 1),
NULL);
if (PyTuple_CheckExact(tuple)) {
ret = PyObject_Call(_CBOR2_Fraction, tuple, NULL);
if (ret) {
set_shareable(self, ret);
}
}
Py_DECREF(tuple);
}
set_shareable(self, ret);
return ret;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/test_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,18 @@ def test_decimal_payload_unpacking(impl, data, expected):
with pytest.raises(impl.CBORDecodeValueError) as exc_info:
impl.loads(unhexlify(data))
assert exc_info.value.args[0] == f"Incorrect tag {expected} payload"


@pytest.mark.parametrize(
"payload, exception, pattern",
[
pytest.param(
b"\xd8\x1e\x84\xff\xff\xff\xff",
TypeError,
r"__new__\(\) takes from 1 to 3 positional arguments but 5 were given",
)
],
)
def test_invalid_data(impl, payload, exception, pattern) -> None:
with pytest.raises(exception, match=pattern):
impl.loads(b"\xd8\x1e\x84\xff\xff\xff\xff")

0 comments on commit c7ef1df

Please sign in to comment.