From 87bdcd1a9501e398bccb03fce606cf6925a75d16 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 10 Jun 2018 00:56:55 +0200 Subject: Improve a bit our coverage wrt. torrent files handling --- libmat2/torrent.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'libmat2') diff --git a/libmat2/torrent.py b/libmat2/torrent.py index f5935e6..b598065 100644 --- a/libmat2/torrent.py +++ b/libmat2/torrent.py @@ -1,4 +1,6 @@ +import logging from typing import Union, Tuple, Dict + from . import abstract @@ -58,6 +60,8 @@ class _BencodeHandler(object): def __decode_int(s: bytes) -> Tuple[int, bytes]: s = s[1:] next_idx = s.index(b'e') + if next_idx is None: + raise ValueError # missing suffix if s.startswith(b'-0'): raise ValueError # negative zero doesn't exist elif s.startswith(b'0') and next_idx != 1: @@ -67,6 +71,8 @@ class _BencodeHandler(object): @staticmethod def __decode_string(s: bytes) -> Tuple[bytes, bytes]: sep = s.index(b':') + if set is None: + raise ValueError # missing suffix str_len = int(s[:sep]) if str_len < 0: raise ValueError @@ -119,9 +125,9 @@ class _BencodeHandler(object): try: r, l = self.__decode_func[s[0]](s) except (IndexError, KeyError, ValueError) as e: - print("not a valid bencoded string: %s" % e) + logging.debug("Not a valid bencoded string: %s" % e) return None if l != b'': - print("invalid bencoded value (data after valid prefix)") + logging.debug("Invalid bencoded value (data after valid prefix)") return None return r -- cgit v1.3