From a94b78cfedc84e8cc84d5bc0ac2ee2e6096c0852 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 22 Jun 2011 16:11:53 -0600 Subject: [PATCH] format_hash() should not consume unbounded memory --- src/calibre/library/database2.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 6848e3c6ae..c9ed5c250a 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -1132,11 +1132,16 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): return ans def format_hash(self, id_, fmt): - data = self.format(id_, fmt, index_is_id=True) - if data is None: + path = self.format_abspath(id_, fmt, index_is_id=True) + if path is None: raise NoSuchFormat('Record %d has no fmt: %s'%(id_, fmt)) 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() def format_abspath(self, index, format, index_is_id=False):