Skip to content

Commit 484fdd4

Browse files
committed
Merge pull request #242 from kevlarkevin/increase_csa_nitems_assertion
MRG: increase sanity threshold for n_items per csa tag Allow more items in csa tags.
2 parents 27e4feb + eab18a3 commit 484fdd4

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

nibabel/nicom/csareader.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
'IS': int, # integer string
1919
}
2020

21+
MAX_CSA_ITEMS = 199
22+
2123

2224
class CSAError(Exception):
2325
pass
@@ -116,7 +118,9 @@ def read(csa_str):
116118
# CSA1 specific length modifier
117119
if tag_no == 1:
118120
tag0_n_items = n_items
119-
assert n_items < 100
121+
if n_items > MAX_CSA_ITEMS:
122+
raise CSAReadError('Expected <= {0} tags, got {1}'.format(
123+
MAX_CSA_ITEMS, n_items))
120124
items = []
121125
for item_no in range(n_items):
122126
x0,x1,x2,x3 = up_str.unpack('4i')
3.22 KB
Binary file not shown.
132 Bytes
Binary file not shown.

nibabel/nicom/tests/test_csareader.py

+18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
CSA2_B0 = open(pjoin(IO_DATA_PATH, 'csa2_b0.bin'), 'rb').read()
1919
CSA2_B1000 = open(pjoin(IO_DATA_PATH, 'csa2_b1000.bin'), 'rb').read()
2020
CSA2_0len = gzip.open(pjoin(IO_DATA_PATH, 'csa2_zero_len.bin.gz'), 'rb').read()
21+
CSA_STR_valid = open(pjoin(IO_DATA_PATH, 'csa_str_valid.bin'), 'rb').read()
22+
CSA_STR_200n_items = open(pjoin(IO_DATA_PATH, 'csa_str_200n_items.bin'), 'rb').read()
2123

2224

2325
@dicom_test
@@ -65,6 +67,22 @@ def test_csa_len0():
6567
assert_equal(len(tags), 44)
6668

6769

70+
def test_csa_nitem():
71+
# testing csa.read's ability to raise an error when n_items >= 200
72+
assert_raises(csa.CSAReadError, csa.read, CSA_STR_200n_items)
73+
# OK when < 200
74+
csa_info = csa.read(CSA_STR_valid)
75+
assert_equal(len(csa_info['tags']), 1)
76+
# OK after changing module global
77+
n_items_thresh = csa.MAX_CSA_ITEMS
78+
try:
79+
csa.MAX_CSA_ITEMS = 1000
80+
csa_info = csa.read(CSA_STR_200n_items)
81+
assert_equal(len(csa_info['tags']), 1)
82+
finally:
83+
csa.MAX_CSA_ITEMS = n_items_thresh
84+
85+
6886
def test_csa_params():
6987
for csa_str in (CSA2_B0, CSA2_B1000):
7088
csa_info = csa.read(csa_str)

0 commit comments

Comments
 (0)