format_hash() should not consume unbounded memory

This commit is contained in:
Kovid Goyal 2011-06-22 16:11:53 -06:00
parent 8da8075ee2
commit a94b78cfed

View File

@ -1132,11 +1132,16 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
return ans return ans
def format_hash(self, id_, fmt): def format_hash(self, id_, fmt):
data = self.format(id_, fmt, index_is_id=True) path = self.format_abspath(id_, fmt, index_is_id=True)
if data is None: if path is None:
raise NoSuchFormat('Record %d has no fmt: %s'%(id_, fmt)) raise NoSuchFormat('Record %d has no fmt: %s'%(id_, fmt))
sha = hashlib.sha256() sha = hashlib.sha256()
sha.update(data) with lopen(path, 'rb') as f:
while True:
raw = f.read(SPOOL_SIZE)
sha.update(raw)
if len(raw) < SPOOL_SIZE:
break
return sha.hexdigest() return sha.hexdigest()
def format_abspath(self, index, format, index_is_id=False): def format_abspath(self, index, format, index_is_id=False):