diff --git a/fitparse/base.py b/fitparse/base.py index b0ebf87..9967ba2 100644 --- a/fitparse/base.py +++ b/fitparse/base.py @@ -209,6 +209,8 @@ def _parse_file_header(self): # Larger fields are explicitly little endian from SDK header_size, protocol_ver_enc, profile_ver_enc, data_size = self._read_struct('2BHI4x', data=header_data) + if header_size < 12: + raise FitHeaderError('Irregular File Header Size') # Decode the same way the SDK does self.protocol_version = float("%d.%d" % (protocol_ver_enc >> 4, protocol_ver_enc & ((1 << 4) - 1))) diff --git a/tests/test.py b/tests/test.py index e4851d5..ec30be1 100755 --- a/tests/test.py +++ b/tests/test.py @@ -2,6 +2,7 @@ import csv import datetime +import io import os from struct import pack import warnings @@ -74,6 +75,12 @@ def testfile(filename): class FitFileTestCase(unittest.TestCase): + def test_rejects_header_shorter_than_fit_minimum(self): + header = bytes([11, 16, 0, 0, 0, 0, 0, 0]) + b'.FIT' + + with self.assertRaises(FitHeaderError): + FitFile(io.BytesIO(header), check_crc=False) + def test_basic_file_with_one_record(self, endian='<'): f = FitFile(generate_fitfile(endian=endian))