mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-27 01:34:24 +03:00
Refactored to reduce risk of infinite loop
This commit is contained in:
parent
d96830115f
commit
4f8173f53f
|
@ -49,44 +49,38 @@ class PpmImageFile(ImageFile.ImageFile):
|
||||||
format = "PPM"
|
format = "PPM"
|
||||||
format_description = "Pbmplus image"
|
format_description = "Pbmplus image"
|
||||||
|
|
||||||
def _read_magic(self, magic=b""):
|
def _read_magic(self):
|
||||||
while True: # read until next whitespace
|
magic = b""
|
||||||
|
# read until whitespace or longest available magic number
|
||||||
|
for _ in range(6):
|
||||||
c = self.fp.read(1)
|
c = self.fp.read(1)
|
||||||
if not c or c in b_whitespace:
|
if not c or c in b_whitespace:
|
||||||
break
|
break
|
||||||
magic += c
|
magic += c
|
||||||
if len(magic) > 6: # exceeded max magic number length
|
|
||||||
break
|
|
||||||
return magic
|
return magic
|
||||||
|
|
||||||
def _read_token(self, token=b""):
|
def _read_token(self):
|
||||||
def _ignore_comment(): # ignores rest of the line; stops at CR, LF or EOF
|
token = b""
|
||||||
while self.fp.read(1) not in b"\r\n":
|
while len(token) <= 10: # read until next whitespace or limit of 10 characters
|
||||||
pass
|
|
||||||
|
|
||||||
while True: # read until non-whitespace is found
|
|
||||||
c = self.fp.read(1)
|
c = self.fp.read(1)
|
||||||
if c == b"#": # found comment, ignore it
|
|
||||||
_ignore_comment()
|
|
||||||
continue
|
|
||||||
if not c:
|
if not c:
|
||||||
raise ValueError("Reached EOF while reading header")
|
|
||||||
if c in b_whitespace: # found whitespace, ignore it
|
|
||||||
continue
|
|
||||||
break
|
|
||||||
|
|
||||||
token += c
|
|
||||||
|
|
||||||
while True: # read until next whitespace
|
|
||||||
c = self.fp.read(1)
|
|
||||||
if c == b"#":
|
|
||||||
_ignore_comment()
|
|
||||||
continue
|
|
||||||
if c in b_whitespace: # token ended
|
|
||||||
break
|
break
|
||||||
|
elif c in b_whitespace: # token ended
|
||||||
|
if not token:
|
||||||
|
# skip whitespace at start
|
||||||
|
continue
|
||||||
|
break
|
||||||
|
elif c == b"#":
|
||||||
|
# ignores rest of the line; stops at CR, LF or EOF
|
||||||
|
while self.fp.read(1) not in b"\r\n":
|
||||||
|
pass
|
||||||
|
continue
|
||||||
token += c
|
token += c
|
||||||
if len(token) > 10:
|
if not token:
|
||||||
raise ValueError(f"Token too long in file header: {token}")
|
# Token was not even 1 byte
|
||||||
|
raise ValueError("Reached EOF while reading header")
|
||||||
|
elif len(token) > 10:
|
||||||
|
raise ValueError(f"Token too long in file header: {token}")
|
||||||
return token
|
return token
|
||||||
|
|
||||||
def _open(self):
|
def _open(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user