From: Zooko O'Whielacronx Date: Sat, 3 Jan 2009 20:22:22 +0000 (-0700) Subject: immutable: when storage server reads from immutable share, don't try to read past... X-Git-Url: https://git.rkrishnan.org/%3C?a=commitdiff_plain;h=53b28c1650c74624c54753712c7625dd68aa747f;p=tahoe-lafs%2Ftahoe-lafs.git immutable: when storage server reads from immutable share, don't try to read past the end of the file (Python allocates space according to the amount of data requested, so if there is corruption and that number is huge it will do a huge memory allocation) --- diff --git a/src/allmydata/storage.py b/src/allmydata/storage.py index d3ae8675..1bed937d 100644 --- a/src/allmydata/storage.py +++ b/src/allmydata/storage.py @@ -135,9 +135,17 @@ class ShareFile: def read_share_data(self, offset, length): precondition(offset >= 0) + # reads beyond the end of the data are truncated. Reads that start beyond the end of the + # data return an empty string. + # I wonder why Python doesn't do the following computation for me? f = open(self.home, 'rb') - f.seek(self._data_offset+offset) - return f.read(length) + seekpos = self._data_offset+offset + fsize = os.path.getsize(self.home) + actuallength = min(0, length, fsize-seekpos) + if actuallength == 0: + return "" + f.seek(seekpos) + return f.read(actuallength) def write_share_data(self, offset, data): length = len(data)