Skip to content

increase sanity threshold for n_items per csa tag #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion nibabel/nicom/csareader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
'IS': int, # integer string
}

MAX_CSA_ITEMS = 199


class CSAError(Exception):
pass
Expand Down Expand Up @@ -116,7 +118,9 @@ def read(csa_str):
# CSA1 specific length modifier
if tag_no == 1:
tag0_n_items = n_items
assert n_items < 100
if n_items > MAX_CSA_ITEMS:
raise CSAReadError('Expected <= {0} tags, got {1}'.format(
MAX_CSA_ITEMS, n_items))
items = []
for item_no in range(n_items):
x0,x1,x2,x3 = up_str.unpack('4i')
Expand Down
Binary file added nibabel/nicom/tests/data/csa_str_200n_items.bin
Binary file not shown.
Binary file added nibabel/nicom/tests/data/csa_str_valid.bin
Binary file not shown.
18 changes: 18 additions & 0 deletions nibabel/nicom/tests/test_csareader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
CSA2_B0 = open(pjoin(IO_DATA_PATH, 'csa2_b0.bin'), 'rb').read()
CSA2_B1000 = open(pjoin(IO_DATA_PATH, 'csa2_b1000.bin'), 'rb').read()
CSA2_0len = gzip.open(pjoin(IO_DATA_PATH, 'csa2_zero_len.bin.gz'), 'rb').read()
CSA_STR_valid = open(pjoin(IO_DATA_PATH, 'csa_str_valid.bin'), 'rb').read()
CSA_STR_200n_items = open(pjoin(IO_DATA_PATH, 'csa_str_200n_items.bin'), 'rb').read()


@dicom_test
Expand Down Expand Up @@ -65,6 +67,22 @@ def test_csa_len0():
assert_equal(len(tags), 44)


def test_csa_nitem():
# testing csa.read's ability to raise an error when n_items >= 200
assert_raises(csa.CSAReadError, csa.read, CSA_STR_200n_items)
# OK when < 200
csa_info = csa.read(CSA_STR_valid)
assert_equal(len(csa_info['tags']), 1)
# OK after changing module global
n_items_thresh = csa.MAX_CSA_ITEMS
try:
csa.MAX_CSA_ITEMS = 1000
csa_info = csa.read(CSA_STR_200n_items)
assert_equal(len(csa_info['tags']), 1)
finally:
csa.MAX_CSA_ITEMS = n_items_thresh


def test_csa_params():
for csa_str in (CSA2_B0, CSA2_B1000):
csa_info = csa.read(csa_str)
Expand Down