mirror of
https://github.com/simon987/nyaa.git
synced 2025-12-14 15:49:02 +00:00
bencode: fix nontermination on empty or malformed input
read(1) returns b'' on EOF, which wasn't checked for in a couple cases, which could cause empty/truncated input to enter one of the `while True` loops without hope of exit. programming is hard.
This commit is contained in:
@@ -55,13 +55,15 @@ def _bencode_decode(file_object, decode_keys_as_utf8=True):
|
||||
return items
|
||||
|
||||
kind = file_object.read(1)
|
||||
if not kind:
|
||||
raise create_ex('EOF, expecting kind')
|
||||
|
||||
if kind == _B_INT: # Integer
|
||||
int_bytes = b''
|
||||
while True:
|
||||
c = file_object.read(1)
|
||||
if not c:
|
||||
raise create_ex('Unexpected end while reading an integer')
|
||||
raise create_ex('EOF, expecting more integer')
|
||||
elif c == _B_END:
|
||||
try:
|
||||
return int(int_bytes.decode('utf8'))
|
||||
@@ -97,6 +99,8 @@ def _bencode_decode(file_object, decode_keys_as_utf8=True):
|
||||
# Read string length until a ':'
|
||||
while True:
|
||||
c = file_object.read(1)
|
||||
if not c:
|
||||
raise create_ex('EOF, expecting more string len')
|
||||
if c in _DIGITS:
|
||||
str_len_bytes += c
|
||||
elif c == b':':
|
||||
|
||||
Reference in New Issue
Block a user