--- a/src/fit_field_base.cpp +++ b/src/fit_field_base.cpp @@ -1282,9 +1282,15 @@ T FieldBase::GetValue(const FIT_UINT8 fieldArrayIndex) const { FIT_UINT8 size = baseTypeSizes[GetType() & FIT_BASE_TYPE_NUM_MASK]; - if (fieldArrayIndex >= (FIT_UINT8)values.size()) + // values holds bytes: the element is read only when all of its bytes are + // there, which a field declared shorter than its type does not have. + if (((size_t)fieldArrayIndex + 1) * size > values.size()) { - return *reinterpret_cast(baseTypeInvalids[GetType() & FIT_BASE_TYPE_NUM_MASK]); + // The field's own type's invalid value, no wider than that value: a + // field read as a wider type than it was declared would read past it. + T rv = 0; + memcpy(&rv, baseTypeInvalids[GetType() & FIT_BASE_TYPE_NUM_MASK], size < sizeof(T) ? size : sizeof(T)); + return rv; } T rv = 0; --- a/src/fit_decode.cpp +++ b/src/fit_decode.cpp @@ -747,6 +747,14 @@ break; case STATE_DEV_FIELD_SIZE: + if ( data == 0 ) + { + // Bad Size, as for a field: an empty developer field sends + // STATE_DEV_FIELD_DATA to a definition past the last. + std::ostringstream message; + message << "FIT decode error: Invalid Developer Field Size " << (int)data << ". Error at byte: " << currentByteOffset; + throw(RuntimeException(message.str())); + } fieldData[DevFieldSizeOffset] = data; state = STATE_DEV_FIELD_INDEX; break; @@ -804,7 +812,7 @@ MesgDefinition defn = localMesgDefs[localMesgIndex]; FieldDefinition* fldDefn = defn.GetFieldByIndex(fieldIndex); FIT_UINT8 baseType = fldDefn->GetType() & FIT_BASE_TYPE_NUM_MASK; - FIT_UINT8 typeSize = baseTypeSizes[baseType]; + FIT_UINT8 typeSize = (baseType < FIT_BASE_TYPES) ? baseTypeSizes[baseType] : 0; FIT_BOOL read = FIT_TRUE; if (baseType < FIT_BASE_TYPES) // Ignore field if base type not supported. @@ -890,14 +898,18 @@ { if (mesg.GetFieldByIndex(i)->GetNumComponents() > 0) { - ExpandComponents(mesg.GetFieldByIndex(i), mesg.GetFieldByIndex(i)->GetComponent(0), mesg.GetFieldByIndex(i)->GetNumComponents()); + // A copy: expanding adds fields to mesg, which can move the + // field mesg holds before its last component is read. + Field containing = *mesg.GetFieldByIndex(i); + ExpandComponents(&containing, containing.GetComponent(0), containing.GetNumComponents()); } } else { if (mesg.GetFieldByIndex(i)->GetSubField(activeSubField)->numComponents > 0) { - ExpandComponents(mesg.GetFieldByIndex(i), mesg.GetFieldByIndex(i)->GetSubField(activeSubField)->components, mesg.GetFieldByIndex(i)->GetSubField(activeSubField)->numComponents); + Field containing = *mesg.GetFieldByIndex(i); + ExpandComponents(&containing, containing.GetSubField(activeSubField)->components, containing.GetSubField(activeSubField)->numComponents); } } } --- a/src/fit_developer_field.cpp +++ b/src/fit_developer_field.cpp @@ -32,6 +32,22 @@ DeveloperField::DeveloperField(const DeveloperField& other) } } +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; +} + DeveloperField::DeveloperField(const DeveloperFieldDefinition& definition) : FieldBase() , mDefinition(new DeveloperFieldDefinition(definition)) --- a/src/fit_developer_field.hpp +++ b/src/fit_developer_field.hpp @@ -27,6 +27,7 @@ class DeveloperField public: DeveloperField(void); DeveloperField(const DeveloperField &field); + DeveloperField& operator=(const DeveloperField& other); DeveloperField(const FieldDescriptionMesg& definition, const DeveloperDataIdMesg& developer); explicit DeveloperField(const DeveloperFieldDefinition& definition); virtual ~DeveloperField();