8000 FIX: Handles invalid TCK files, prevents infinite loop by anibalsolon · Pull Request #1140 · nipy/nibabel · GitHub
[go: up one dir, main page]

Skip to content

FIX: Handles invalid TCK files, prevents infinite loop #1140

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
Sep 30, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
fix: use interface from Opener
  • Loading branch information
anibalsolon committed Sep 30, 2022
commit a4c420df71c9fc912204cac69406d7cc8ca8910e
18 changes: 9 additions & 9 deletions nibabel/streamlines/tck.py
7088
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ def is_correct_format(cls, fileobj):
otherwise returns False.
"""
with Opener(fileobj) as f:
magic_number = asstr(f.fobj.read(len(cls.MAGIC_NUMBER)))
magic_number = f.read(len(cls.MAGIC_NUMBER))
f.seek(-len(cls.MAGIC_NUMBER), os.SEEK_CUR)

return magic_number.strip() == cls.MAGIC_NUMBER
return asstr(magic_number) == cls.MAGIC_NUMBER

@classmethod
def create_empty_header(cls):
Expand Down Expand Up @@ -312,25 +312,25 @@ def _read_header(cls, fileobj):
with Opener(fileobj) as f:

# Record start position
start_position = f.fobj.tell()
start_position = f.tell()

# Make sure we are at the beginning of the file
f.fobj.seek(0, os.SEEK_SET)
f.seek(0, os.SEEK_SET)

# Read magic number
magic_number = asstr(f.fobj.read(len(cls.MAGIC_NUMBER)))
magic_number = f.read(len(cls.MAGIC_NUMBER))

if magic_number != cls.MAGIC_NUMBER:
if asstr(magic_number) != cls.MAGIC_NUMBER:
raise HeaderError(f"Invalid magic number: {magic_number}")

hdr[Field.MAGIC_NUMBER] = magic_number

f.fobj.seek(1, os.SEEK_CUR) # Skip \n
f.seek(1, os.SEEK_CUR) # Skip \n

found_end = False

# Read all key-value pairs contained in the header, stop at EOF
for n_line, line in enumerate(f.fobj, 1):
for n_line, line in enumerate(f, 1):
line = asstr(line).strip()

if not line: # Skip empty lines
Expand All @@ -353,7 +353,7 @@ def _read_header(cls, fileobj):

# Set the file position where it was, in case it was previously open
if start_position is not None:
f.fobj.seek(start_position, os.SEEK_SET)
f.seek(start_position, os.SEEK_SET)

# Check integrity of TCK header.
if 'datatype' not in hdr:
Expand Down
0