Changes to protect servers against broken clients

This commit is contained in:
Guido van Rossum 1992-12-19 00:06:17 +00:00
parent 424c673d2f
commit c91d60a640

View file

@ -89,6 +89,8 @@ class Unpacker:
i = self.pos i = self.pos
self.pos = j = i+4 self.pos = j = i+4
data = self.buf[i:j] data = self.buf[i:j]
if len(data) < 4:
raise EOFError
x = long(ord(data[0]))<<24 | ord(data[1])<<16 | \ x = long(ord(data[0]))<<24 | ord(data[1])<<16 | \
ord(data[2])<<8 | ord(data[3]) ord(data[2])<<8 | ord(data[3])
# Return a Python long only if the value is not representable # Return a Python long only if the value is not representable
@ -99,7 +101,10 @@ class Unpacker:
def unpack_uint(self): def unpack_uint(self):
i = self.pos i = self.pos
self.pos = j = i+4 self.pos = j = i+4
return struct.unpack('l', self.buf[i:j]) data = self.buf[i:j]
if len(data) < 4:
raise EOFError
return struct.unpack('l', data)
def unpack_int(self): def unpack_int(self):
x = self.unpack_uint() x = self.unpack_uint()
@ -126,7 +131,7 @@ class Unpacker:
i = self.pos i = self.pos
j = i + (n+3)/4*4 j = i + (n+3)/4*4
if j > len(self.buf): if j > len(self.buf):
raise RuntimeError, 'buffer overrun' raise EOFError
self.pos = j self.pos = j
return self.buf[i:i+n] return self.buf[i:i+n]