[diffoscope] 04/05: Prevent CBFS comparator from scanning big files needlessly
Jérémy Bobbio
lunar at moszumanska.debian.org
Tue Dec 15 17:21:29 CET 2015
This is an automated email from the git hooks/post-receive script.
lunar pushed a commit to branch master
in repository diffoscope.
commit 15981bcf79e1363aa6da3da09a93287b259ce9cc
Author: Jérémy Bobbio <lunar at debian.org>
Date: Tue Dec 15 16:34:49 2015 +0100
Prevent CBFS comparator from scanning big files needlessly
To recognize CBFS images, we fall back on looking for the magic number
by scanning the entire file. This is expensive! In order to speed up
diffoscope, we assume that any file bigger than 24 MiB will not be a CBFS
image. That's a reasonable assumption given that, as of today, the biggest
image produced by coreboot is 16 MiB.
We also now try to identify CBFS images last. This means other formats have a
chance to be recognized before the file gets fully scanned.
Thanks Mike Hommey for the detailed bug report and suggestions.
Closes: #807997
---
diffoscope/comparators/__init__.py | 2 +-
diffoscope/comparators/cbfs.py | 4 +++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/diffoscope/comparators/__init__.py b/diffoscope/comparators/__init__.py
index b4615c9..ed24f63 100644
--- a/diffoscope/comparators/__init__.py
+++ b/diffoscope/comparators/__init__.py
@@ -129,7 +129,6 @@ FILE_CLASSES = (
DebDataTarFile,
TextFile,
Bzip2File,
- CbfsFile,
CpioFile,
DebFile,
DexFile,
@@ -155,6 +154,7 @@ FILE_CLASSES = (
XzFile,
ZipFile,
ImageFile,
+ CbfsFile,
)
diff --git a/diffoscope/comparators/cbfs.py b/diffoscope/comparators/cbfs.py
index aa9bfb2..88ed200 100644
--- a/diffoscope/comparators/cbfs.py
+++ b/diffoscope/comparators/cbfs.py
@@ -82,6 +82,8 @@ CBFS_HEADER_VERSION1 = 0x31313131
CBFS_HEADER_VERSION2 = 0x31313132
CBFS_HEADER_SIZE = 8 * 4 # 8 * uint32_t
+# On 2015-12-15, the largest image produced by coreboot is 16 MiB
+CBFS_MAXIMUM_FILE_SIZE = 24 * 2 ** 20 # 24 MiB
def is_header_valid(buf, size, offset=0):
magic, version, romsize, bootblocksize, align, cbfs_offset, architecture, pad = struct.unpack_from('!IIIIIIII', buf, offset)
@@ -97,7 +99,7 @@ class CbfsFile(File):
@staticmethod
def recognizes(file):
size = os.stat(file.path).st_size
- if size < CBFS_HEADER_SIZE:
+ if size < CBFS_HEADER_SIZE or size > CBFS_MAXIMUM_FILE_SIZE:
return False
with open(file.path, 'rb') as f:
# pick at the latest byte as it should contain the relative offset of the header
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the diffoscope
mailing list