From fc78572c11ffedff66eb330ef28e1e77849318b4 Mon Sep 17 00:00:00 2001 From: Kio Smallwood Date: Thu, 24 Jun 2021 12:52:29 +0100 Subject: [PATCH] Fix #114: Raise TypeError when using a negative tag TODO: Fix c-module --- cbor2/types.py | 4 ++-- tests/test_encoder.py | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cbor2/types.py b/cbor2/types.py index 04d1050b..b0ab530c 100644 --- a/cbor2/types.py +++ b/cbor2/types.py @@ -44,8 +44,8 @@ class CBORTag: __slots__ = 'tag', 'value' def __init__(self, tag, value): - if not isinstance(tag, int): - raise TypeError('CBORTag tags must be integer numbers') + if not isinstance(tag, int) or tag not in range(2**64): + raise TypeError('CBORTag tags must be positive integers less than 2**64') self.tag = tag self.value = value diff --git a/tests/test_encoder.py b/tests/test_encoder.py index 3e9346b2..6beaa220 100644 --- a/tests/test_encoder.py +++ b/tests/test_encoder.py @@ -520,3 +520,8 @@ def test_encode_stringrefs_dict(impl): 'd81901' 'd81900' ) assert impl.dumps(value, string_referencing=True, canonical=True) == expected + + +def test_negative_tag(impl): + with pytest.raises(TypeError): + impl.dumps(impl.CBORTag(-1, 'value'))