From 572ff74586d9b1daab904c6f7f7009ce0143bb75 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Mon, 9 Aug 2021 20:44:51 -0700 Subject: [PATCH] Fix checking if typecode is valid while decoding. This bug will cause rencode to hang if the invalid typecode is included in a sequence type (list, dict) since the position will not change and the loop checking for the termination byte never returns. This change is a copy of PR #29 with a few aesthetic changes. --- rencode/rencode.pyx | 2 ++ tests/test_rencode.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/rencode/rencode.pyx b/rencode/rencode.pyx index d649c85..3db1180 100644 --- a/rencode/rencode.pyx +++ b/rencode/rencode.pyx @@ -527,6 +527,8 @@ cdef decode(char *data, unsigned int *pos): return decode_fixed_dict(data, pos) elif typecode == CHR_DICT: return decode_dict(data, pos) + else: + raise ValueError("Invalid typecode: %d at pos: %d" % (typecode, pos[0])) def loads(data, decode_utf8=False): """ diff --git a/tests/test_rencode.py b/tests/test_rencode.py index 7233fd6..74737eb 100644 --- a/tests/test_rencode.py +++ b/tests/test_rencode.py @@ -401,6 +401,11 @@ def test_version_exposed(self): "version number does not match", ) + def test_invalid_typecode(self): + s = b";\x2f\x7f" + with self.assertRaises(ValueError): + rencode.loads(s) + if __name__ == "__main__": unittest.main()