Skip to content

Commit

Permalink
Only read a maximum of 100 bytes at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Sep 28, 2022
1 parent d402fe0 commit cb22437
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/PIL/ImtImagePlugin.py
Expand Up @@ -39,32 +39,44 @@ def _open(self):
# Quick rejection: if there's not a LF among the first
# 100 bytes, this is (probably) not a text header.

if b"\n" not in self.fp.read(100):
buffer = self.fp.read(100)
if b"\n" not in buffer:
raise SyntaxError("not an IM file")
self.fp.seek(0)

xsize = ysize = 0

while True:

s = self.fp.read(1)
if buffer:
s = buffer[:1]
buffer = buffer[1:]
else:
s = self.fp.read(1)
if not s:
break

if s == b"\x0C":

# image data begins
self.tile = [
("raw", (0, 0) + self.size, self.fp.tell(), (self.mode, 0, 1))
(
"raw",
(0, 0) + self.size,
self.fp.tell() - len(buffer),
(self.mode, 0, 1),
)
]

break

else:

# read key/value pair
# FIXME: dangerous, may read whole file
s = s + self.fp.readline()
if b"\n" not in buffer:
buffer += self.fp.read(100)
lines = buffer.split(b"\n")
s += lines.pop(0)
buffer = b"\n".join(lines)
if len(s) == 1 or len(s) > 100:
break
if s[0] == ord(b"*"):
Expand Down

0 comments on commit cb22437

Please sign in to comment.