Skip to content

Commit ebdcc01

Browse files
committed
BF+TST: Python 3 zero bytes error for offset
Nipy testing revealed that when the data offset is beyond the end of the header we hit the unguarded use of '\x00' for a zero byte string, rather than the needed ZEROB constant.
1 parent 1e171a3 commit ebdcc01

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

nibabel/nifti1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ def _write_header(self, header_file, header, slope, inter):
17791779
offset = header.get_data_offset()
17801780
diff = offset-header_file.tell()
17811781
if diff > 0:
1782-
header_file.write('\x00' * diff)
1782+
header_file.write(ZEROB * diff)
17831783

17841784
def update_header(self):
17851785
''' Harmonize header with image data and affine '''

nibabel/tests/test_nifti1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,15 @@ def test_set_sform(self):
469469
# Unexpected keyword raises error
470470
assert_raises(TypeError, img.get_sform, strange=True)
471471

472+
def test_hdr_diff(self):
473+
# Check an offset beyond data does not raise an error
474+
img = self.image_class(np.zeros((2,3,4)), np.eye(4))
475+
ext = dict(img.files_types)['image']
476+
hdr = img.get_header()
477+
hdr['vox_offset'] = 400
478+
with InTemporaryDirectory():
479+
img.to_filename('another_file' + ext)
480+
472481

473482
class TestNifti1Pair(TestNifti1Image):
474483
# Run analyze-flavor spatialimage tests

0 commit comments

Comments
 (0)