Tansible

Whitepapers · November 2026

FIT C++ SDK: five memory faults, one on a valid file

To: the maintainers of the FIT C++ SDK, github.com/garmin/fit-cpp-sdk

Releases checked: 21.205.0 and 21.221.0, the newest at the time of writing; the code below is the same in both, and the line numbers are 21.221.0's.

Found by: AddressSanitizer, and libFuzzer, running FIT files through fit::Decode; the first fault was then reduced to a valid file by hand.

Updated: 21 November 2026, with the fifth fault, a developer field freed twice; it is in 21.222.0 as well.

Summary

Five faults, each reached from Decode::CheckIntegrity and Decode::Read, so any application that opens a FIT file is exposed to them.

The first is the most serious: it needs no damaged file. A valid recording, of the kind a device writes, is read from freed memory. The other four need a malformed file.

  1. A valid file read from freed memory. Expanding a compressed_speed_distance field adds fields to the message, which can move the field being expanded; ExpandComponents then reads its next component through the stale pointer. A well-formed record with that field among four reads past a freed heap block.
  2. A field declared shorter than its type is read past the end of its value buffer. A uint32 timestamp declared one byte long makes FieldBase::GetValue read three bytes beyond a one-byte heap allocation.
  3. A timestamp declared with a narrower type, such as four bytes of an enum, makes GetValue return the narrower type's invalid value read as a uint32: four bytes from a one-byte global.
  4. A developer field declared zero bytes long makes Decode::ReadByte ask for the developer field definition after the last, which is null, and call GetSize() through it: a crash.
  5. A developer field assigned over another shares its definition and frees it twice. DeveloperField owns its definition through a pointer and has a copy constructor but no copy assignment, so Mesg::AddDeveloperField, replacing a field it already holds, copies the pointer; the field it was copied from then frees it, and the message frees it again.

The first is shown by a valid file of 50 bytes; the other four by files of 27 to 39 bytes, each valid in its header, its CRCs and its record structure. A patch of five changes ends all five.

1. A valid file read from freed memory

This one needs no damaged file. Decode::Read, having decoded a message, expands every field that carries components (src/fit_decode.cpp line 883):

// Now that the entire message is decoded we may evaluate subfields and expand components
for (FIT_UINT16 i=0; i<mesg.GetNumFields(); i++)
{
    FIT_UINT16 activeSubField = mesg.GetActiveSubFieldIndexByFieldIndex(i);
    if( !suppressComponentExpansion )
    {
        if (activeSubField == FIT_SUBFIELD_INDEX_MAIN_FIELD)
        {
            if (mesg.GetFieldByIndex(i)->GetNumComponents() > 0)
            {
                ExpandComponents(mesg.GetFieldByIndex(i), mesg.GetFieldByIndex(i)->GetComponent(0), mesg.GetFieldByIndex(i)->GetNumComponents());
            }
        }

mesg.GetFieldByIndex(i) is a pointer into mesg's fields, a std::vector<Field>. ExpandComponents reads one component of that field at a time and adds each expanded component back to the same message (src/fit_decode.cpp, line 1009 reads, line 1034 adds):

    bitsValue = containingField->GetBitsValue64(offset, component->bits);   // reads *containingField
    ...
    mesg.AddField(componentField);                                          // grows mesg's vector<Field>

AddField can reallocate the vector, which frees the storage containingField points at; the next component is then read through the freed pointer. A field with more than one component, in a message whose vector reallocates on the add, is enough: compressed_speed_distance (two components) in a record of four fields is one such shape.

old-device.fit, 50 bytes: a record (global message 20) defining a timestamp, compressed_speed_distance (field 8) and two more fields, and one valid record of them:

0e 20 54 08 22 00 00 00 2e 46 49 54 3a 10 40 00
00 14 00 04 fd 04 86 08 03 0d 00 04 85 01 04 85
00 00 ca 9a 3b 10 32 05 00 10 db 21 00 28 07 a9
79 39
==ERROR: AddressSanitizer: heap-use-after-free on address 0x512000000098
READ of size 8 at 0x512000000098 thread T0
    #0 in std::vector<unsigned char>::size() const
    #1 in fit::FieldBase::GetBitsValueT<unsigned long>(unsigned short, unsigned char) const src/fit_field_base.cpp:1219
    #2 in fit::Decode::ExpandComponents(fit::Field*, fit::Profile::FIELD_COMPONENT const*, unsigned short) src/fit_decode.cpp:1009
    #3 in fit::Decode::ReadByte(unsigned char) src/fit_decode.cpp
    #4 in fit::Decode::CheckIntegrity(std::istream&) src/fit_decode.cpp:113
0x512000000098 is located 88 bytes inside of a 288-byte region
freed by thread T0 here, the same expansion growing the vector:
    #4 in void std::vector<fit::Field>::_M_realloc_insert<fit::Field const&>(...)
    #5 in fit::Decode::ExpandComponents(...) src/fit_decode.cpp

A device writes files of this kind: this is reached by a recording the SDK is meant to read, not only by a crafted one. The fix expands a copy of the field, so growing the message cannot move what is being read.

2. A field declared shorter than its type

src/fit_field_base.cpp, FieldBase::GetValue (line 1282):

template <typename T>
T FieldBase::GetValue(const FIT_UINT8 fieldArrayIndex) const
{
    FIT_UINT8 size = baseTypeSizes[GetType() & FIT_BASE_TYPE_NUM_MASK];
    if (fieldArrayIndex >= (FIT_UINT8)values.size())
    {
        return *reinterpret_cast<const T*>(baseTypeInvalids[GetType() & FIT_BASE_TYPE_NUM_MASK]);
    }

    T rv = 0;

    for (int i = 0; i < size; i++)
    {
        rv |= static_cast<T>(values[(fieldArrayIndex * size) + i]) << (i * 8);
    }

    return rv;
}

values holds the field's bytes (std::vector<FIT_BYTE>), and GetNumValues() divides its length by the type's size. The guard compares an element index against the byte count, and the loop then reads size bytes from fieldArrayIndex * size. A uint32 declared 1 byte has values.size() == 1; GetValue(0) passes the guard and reads bytes 0 to 3. Decode::ReadByte asks for exactly this for every timestamp field (timestamp = field.GetUINT32Value();, src/fit_decode.cpp line 841). The same guard lets a caller read past a well-formed field's buffer by asking for an index at or above GetNumValues(): a uint32 of one value has values.size() == 4, and GetValue(1) to GetValue(3) read up to 12 bytes past it.

short-field.fit, 27 bytes: one definition, local message 0 for global message 20 (record) with one field, 253 (timestamp), declared 1 byte of base type 0x86 (uint32), and one record carrying that byte:

0e 20 54 08 0b 00 00 00 2e 46 49 54 f8 62 40 00
00 14 00 01 fd 01 86 00 2a 58 f1
==636==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x502000000051
READ of size 1 at 0x502000000051 thread T0
    #0 in unsigned int fit::FieldBase::GetValue<unsigned int>(unsigned char) const src/fit_field_base.cpp:1294:30
    #1 in fit::FieldBase::GetUINT32Value(unsigned char) const src/fit_field_base.cpp:214:12
    #2 in fit::Decode::ReadByte(unsigned char) src/fit_decode.cpp:841:47
    #3 in fit::Decode::CheckIntegrity(std::istream&) src/fit_decode.cpp:113:25
0x502000000051 is located 0 bytes after 1-byte region [0x502000000050,0x502000000051)

3. A timestamp declared with a narrower type

When a field's declared type is smaller than the profile's, ReadByte takes the declared type (field.SetBaseType(fldDefn->GetType()), line 822), so a timestamp declared as an enum becomes a one-byte field. ReadByte still reads it as a uint32, and when GetValue finds no element to read it returns *reinterpret_cast<const T*>(baseTypeInvalids[...]): four bytes read from enumInvalid, a one-byte global.

enum-timestamp.fit, 30 bytes: the same record with its timestamp declared 4 bytes of base type 0x00 (enum), each byte the enum's invalid value:

0e 20 54 08 0e 00 00 00 2e 46 49 54 38 5d 40 00
00 14 00 01 fd 04 00 00 ff ff ff ff 87 69
==641==ERROR: AddressSanitizer: global-buffer-overflow on address 0x55f7e42a4460
READ of size 4 at 0x55f7e42a4460 thread T0
    #0 in unsigned int fit::FieldBase::GetValue<unsigned int>(unsigned char) const src/fit_field_base.cpp:1287:16
0x55f7e42a4461 is located 0 bytes after global variable 'fit::enumInvalid' defined in 'src/fit.cpp:68' of size 1

4. A developer field of zero bytes

STATE_FIELD_SIZE refuses a field of size 0 ("Invalid Field Size"); STATE_DEV_FIELD_SIZE (line 749) does not. In STATE_DEV_FIELD_DATA (line 920), an empty developer field is skipped by asking for the next one:

fieldBytesLeft = fieldDef->GetSize();

if (fieldBytesLeft == 0)
{
    fieldBytesLeft = localMesgDefs->
        GetDevFieldByIndex(++fieldIndex)->GetSize();
}

Past the last developer field GetDevFieldByIndex returns null, and GetSize() reads through it. The lookup also goes through localMesgDefs->, local message 0's definition, and not localMesgDefs[localMesgIndex], and it skips one empty field, not a run of them.

empty-developer.fit, 35 bytes: a definition of global message 0 (file_id) with no fields and three developer fields of 0, 1 and 0 bytes, one message of its one byte, and the next message's header:

0e 20 54 08 13 00 00 00 2e 46 49 54 f8 c8 60 00
00 00 00 00 03 00 00 00 01 01 00 02 00 00 00 2a
00 49 15
==1160==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000009
    #0 in fit::DeveloperFieldDefinition::GetSize() const src/fit_developer_field_definition.cpp:99:12
    #1 in fit::Decode::ReadByte(unsigned char) src/fit_decode.cpp:932:60
    #2 in fit::Decode::CheckIntegrity(std::istream&) src/fit_decode.cpp:113:25

Without a sanitizer this one is a segmentation fault.

5. A developer field assigned over another

DeveloperField (src/fit_developer_field.hpp) holds its definition as a DeveloperFieldDefinition* that its destructor deletes (line 51 of the .cpp). It declares a copy constructor, which copies the definition, and no copy assignment, so the one the compiler writes copies the pointer.

Mesg::AddDeveloperField (src/fit_mesg.cpp line 202) assigns a field over one it already holds with an equal definition, the same field number and developer index:

if (devFields[i].GetDefinition() == field.GetDefinition())
{
    devFields[i] = field;
    return;
}

Decode::ReadByte passes it a local (src/fit_decode.cpp lines 947 to 951):

DeveloperField field(*fldDefn);
...
mesg.AddDeveloperField(field);

The message's field and the local now hold one definition. The local is destroyed at the end of its block and deletes it. The message's field deletes it again when the message goes: at the next record, where mesg = Mesg(...) (lines 584 and 611) replaces it, or at the end of the decode. A message that declares the same developer field twice is enough.

repeated-developer.fit, 39 bytes: a record with a timestamp and developer field 0 of developer 0 declared twice, one byte each, and one record of them:

0e 20 54 08 17 00 00 00 2e 46 49 54 f9 3b 60 00
00 14 00 01 fd 04 86 02 00 01 00 00 01 00 00 00
ca 9a 3b 01 02 ac 05
==609==ERROR: AddressSanitizer: heap-use-after-free on address 0x503000006430
READ of size 8 at 0x503000006430 thread T0
    #0 in fit::DeveloperField::~DeveloperField() src/fit_developer_field.cpp:51:9
    #1 in void std::_Destroy<fit::DeveloperField>(fit::DeveloperField*)
0x503000006430 is located 0 bytes inside of 32-byte region
freed by thread T0 here:
    #1 in fit::DeveloperField::~DeveloperField() src/fit_developer_field.cpp:51:9
    #2 in fit::Decode::ReadByte(unsigned char) src/fit_decode.cpp:952:18
    #3 in fit::Decode::CheckIntegrity(std::istream&) src/fit_decode.cpp:113:25
previously allocated by thread T0 here:
    #1 in fit::DeveloperField::DeveloperField(fit::DeveloperFieldDefinition const&) src/fit_developer_field.cpp:37:19
    #2 in fit::Decode::ReadByte(unsigned char) src/fit_decode.cpp:947:37

Without a sanitizer it is a double free, which most allocators abort on and some do not notice.

Reproducing them

make_fault_files.py writes the five files. repro.cpp reads a file as an application does: CheckIntegrity, then Read with a MesgBroadcaster. prove.sh, given an SDK checkout, builds it under AddressSanitizer and reads each:

clang++ -std=c++17 -g -O1 -fsanitize=address -I<sdk>/src repro.cpp <sdk>/src/*.cpp -o repro
./repro old-device.fit

A fix

fix.patch, against 21.221.0, makes five changes:

cpp Field containing = *mesg.GetFieldByIndex(i); ExpandComponents(&containing, containing.GetComponent(0), containing.GetNumComponents());

cpp if (((size_t)fieldArrayIndex + 1) * size > values.size()) { T rv = 0; memcpy(&rv, baseTypeInvalids[GetType() & FIT_BASE_TYPE_NUM_MASK], size < sizeof(T) ? size : sizeof(T)); return rv; }

A caller that compares the result with T's own invalid value may want that value instead; the change only stops the read past the global. - STATE_DEV_FIELD_SIZE refuses a size of 0, as STATE_FIELD_SIZE does. - ReadByte takes the size only of a base type the table holds ((baseType < FIT_BASE_TYPES) ? baseTypeSizes[baseType] : 0, line 807). baseType can be up to 31 and baseTypeSizes has 17 entries; the byte was read before the check and used only inside it. - DeveloperField gains the copy assignment its copy constructor implies: it copies the definition, and deletes its own.

cpp DeveloperField& DeveloperField::operator=(const DeveloperField& other) { if (this != &other) { FieldBase::operator=(other); DeveloperFieldDefinition* copy = nullptr; if (nullptr != other.mDefinition) { copy = new DeveloperFieldDefinition(*other.mDefinition); } delete mDefinition; mDefinition = copy; } return *this; }

With the patch, the same build reads old-device.fit, short-field.fit, enum-timestamp.fit and repeated-developer.fit without a report and refuses empty-developer.fit as damaged. patch.sh applies the patch to a copy of a checkout and runs the proof on it; it applies to 21.205.0 as well.

Also seen

FIT_STRUCT_OFFSET (fit.hpp) takes a member's offset through a null pointer, which UndefinedBehaviorSanitizer reports in Encode::WriteFileHeader (src/fit_encode.cpp line 120). offsetof from <cstddef> gives the same value without it.