Skip to content

Commit

Permalink
Fixed encoding of simple values between 20 and 23
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Nov 2, 2023
1 parent 49b0452 commit 4839594
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cbor2/_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def encode_ipnetwork(self, value: IPv4Network | IPv6Network) -> None:
#

def encode_simple_value(self, value: CBORSimpleValue) -> None:
if value.value < 20:
if value.value < 24:
self._fp_write(struct.pack(">B", 0xE0 | value.value))
else:
self._fp_write(struct.pack(">BB", 0xF8, value.value))
Expand Down
1 change: 1 addition & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This library adheres to `Semantic Versioning <http://semver.org/>`_.

- Fixed ``CBORSimpleValue`` allowing the use of reserved values (24 to 31) which resulted in
invalid byte sequences
- Fixed encoding of simple values from 20 to 23 producing the wrong byte sequences

**5.5.0** (2023-10-21)

Expand Down
2 changes: 1 addition & 1 deletion source/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,7 @@ CBOREncoder_encode_simple_value(CBOREncoderObject *self, PyObject *args)

if (!PyArg_ParseTuple(args, "B", &value))
return NULL;
if (value < 20) {
if (value < 24) {
value |= 0xE0;
if (fp_write(self, (char *)&value, 1) == -1)
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion tests/test_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def test_special(impl, special_values):
assert impl.dumps(value) == expected


@pytest.fixture(params=[(0, "e0"), (2, "e2"), (19, "f3"), (32, "f820")])
@pytest.fixture(params=[(0, "e0"), (2, "e2"), (23, "f7"), (32, "f820")])
def simple_values(request, impl):
value, expected = request.param
return impl.CBORSimpleValue(value), expected
Expand Down

0 comments on commit 4839594

Please sign in to comment.