Skip to content
Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aresch committed Aug 10, 2021
1 parent 5726b24 commit 572ff74
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rencode/_rencode.pyx
Expand Up @@ -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):
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/test_rencode.py
Expand Up @@ -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()

0 comments on commit 572ff74

Please sign in to comment.