diff options
author | Jörn Heissler <nosuchaddress@joern.heissler.de> | 2019-08-08 08:38:37 +0200 |
---|---|---|
committer | Jörn Heissler <nosuchaddress@joern.heissler.de> | 2019-08-08 09:10:22 +0200 |
commit | a5cd347b818679f833c929241e31a15d084e256b (patch) | |
tree | 69992f08935de99efb9825d8a8187c4a7e156c19 /asn1crypto | |
parent | 74d66a95cb1bbea52d21c85283fb6c707123f494 (diff) | |
download | asn1crypto-a5cd347b818679f833c929241e31a15d084e256b.tar.gz |
Corner cases in int_to_bytes
int_to_bytes(0, width=0) returns b'' in py3 but b'\x00' in py2.
py2 implementation will now return b'' too.
int_to_bytes(12345, width=1) raises OverflowError in py3 but not in py2.
py2 will now also raise the Exception
Also completed test coverage for int_to_bytes and int_from_bytes
Diffstat (limited to 'asn1crypto')
-rw-r--r-- | asn1crypto/util.py | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/asn1crypto/util.py b/asn1crypto/util.py index 2e55ef8..d8b0f98 100644 --- a/asn1crypto/util.py +++ b/asn1crypto/util.py @@ -49,13 +49,16 @@ if sys.version_info <= (3,): If the byte string should be encoded using two's complement :param width: - None == auto, otherwise an integer of the byte width for the return - value + If None, the minimal possible size (but at least 1), + otherwise an integer of the byte width for the return value :return: A byte string """ + if value == 0 and width == 0: + return b'' + # Handle negatives in two's complement is_neg = False if signed and value < 0: @@ -73,6 +76,8 @@ if sys.version_info <= (3,): output = b'\x00' + output if width is not None: + if len(output) > width: + raise OverflowError('int too big to convert') if is_neg: pad_char = b'\xFF' else: @@ -146,8 +151,8 @@ else: If the byte string should be encoded using two's complement :param width: - None == auto, otherwise an integer of the byte width for the return - value + If None, the minimal possible size (but at least 1), + otherwise an integer of the byte width for the return value :return: A byte string |