Skip to content

Commit 76440f2

Browse files
committed
RF: Fail quietly on too-short header blocks
1 parent 5814435 commit 76440f2

File tree

7 files changed

+9
-22
lines changed

7 files changed

+9
-22
lines changed

nibabel/analyze.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,7 @@ def _chk_pixdims(hdr, fix=False):
895895
@classmethod
896896
def may_contain_header(klass, binaryblock):
897897
if len(binaryblock) < klass.sizeof_hdr:
898-
raise ValueError('Must pass a binary block >= %d bytes' %
899-
klass.sizeof_hdr)
898+
return False
900899

901900
hdr_struct = np.ndarray(shape=(), dtype=header_dtype,
902901
buffer=binaryblock[:klass.sizeof_hdr])

nibabel/minc1.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,6 @@ def data_from_fileobj(self, fileobj):
282282
class Minc1Header(MincHeader):
283283
@classmethod
284284
def may_contain_header(klass, binaryblock):
285-
if len(binaryblock) < 4:
286-
raise ValueError('Must pass a binary block >= 4 bytes')
287-
288285
return binaryblock[:4] == b'CDF\x01'
289286

290287

nibabel/minc2.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ def get_scaled_data(self, sliceobj=()):
137137
class Minc2Header(MincHeader):
138138
@classmethod
139139
def may_contain_header(klass, binaryblock):
140-
if len(binaryblock) < 4:
141-
raise ValueError('Must pass a binary block >= 4 bytes')
142-
143140
return binaryblock[:4] == b'\211HDF'
144141

145142

nibabel/nifti1.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,8 +1614,7 @@ def _chk_xform_code(klass, code_type, hdr, fix):
16141614
@classmethod
16151615
def may_contain_header(klass, binaryblock):
16161616
if len(binaryblock) < klass.sizeof_hdr:
1617-
raise ValueError('Must pass a binary block >= %d bytes' %
1618-
klass.sizeof_hdr)
1617+
return False
16191618

16201619
hdr_struct = np.ndarray(shape=(), dtype=header_dtype,
16211620
buffer=binaryblock[:klass.sizeof_hdr])

nibabel/nifti2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ def _chk_eol_check(hdr, fix=False):
224224
@classmethod
225225
def may_contain_header(klass, binaryblock):
226226
if len(binaryblock) < klass.sizeof_hdr:
227-
raise ValueError('Must pass a binary block >= %d bytes' %
228-
klass.sizeof_hdr)
227+
return False
229228

230229
hdr_struct = np.ndarray(shape=(), dtype=header_dtype,
231230
buffer=binaryblock[:klass.sizeof_hdr])

nibabel/spm2analyze.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ def get_slope_inter(self):
116116
@classmethod
117117
def may_contain_header(klass, binaryblock):
118118
if len(binaryblock) < klass.sizeof_hdr:
119-
raise ValueError('Must pass a binary block >= %d bytes' %
120-
klass.sizeof_hdr)
119+
return False
121120

122121
hdr_struct = np.ndarray(shape=(), dtype=header_dtype,
123122
buffer=binaryblock[:klass.sizeof_hdr])

nibabel/tests/test_image_types.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Spm2AnalyzeImage, Spm99AnalyzeImage,
2222
MGHImage, all_image_classes)
2323

24-
from nose.tools import assert_true, assert_raises
24+
from nose.tools import assert_true
2525

2626
DATA_PATH = pjoin(dirname(__file__), 'data')
2727

@@ -46,17 +46,13 @@ def check_img(img_path, img_klass, sniff_mode, sniff, expect_success,
4646
msg):
4747
"""Embedded function to do the actual checks expected."""
4848

49-
if sniff_mode == 'empty' and \
50-
hasattr(img_klass.header_class, 'may_contain_header'):
51-
assert_raises(ValueError,
52-
img_klass.header_class.may_contain_header, sniff)
53-
5449
if sniff_mode == 'no_sniff':
5550
# Don't pass any sniff--not even "None"
5651
is_img, new_sniff = img_klass.path_maybe_image(img_path)
5752
elif sniff_mode in ('empty', 'irrelevant', 'bad_sniff'):
5853
# Add img_path to binaryblock sniff parameters
59-
is_img, new_sniff = img_klass.path_maybe_image(img_path, (sniff, img_path))
54+
is_img, new_sniff = img_klass.path_maybe_image(
55+
img_path, (sniff, img_path))
6056
else:
6157
# Pass a sniff, but don't reuse across images.
6258
is_img, new_sniff = img_klass.path_maybe_image(img_path, sniff)
@@ -67,7 +63,8 @@ def check_img(img_path, img_klass, sniff_mode, sniff, expect_success,
6763
msg)
6864
expected_sizeof_hdr = getattr(img_klass.header_class,
6965
'sizeof_hdr', 0)
70-
current_sizeof_hdr = 0 if new_sniff is None else len(new_sniff[0])
66+
current_sizeof_hdr = 0 if new_sniff is None else \
67+
len(new_sniff[0])
7168
assert_true(current_sizeof_hdr >= expected_sizeof_hdr, new_msg)
7269

7370
# Check that the image type was recognized.

0 commit comments

Comments
 (0)