diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index 03ff347c..859bbe97 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -274,10 +274,11 @@ result_t SingleDataField::create(const string id, const unsigned char length, for (unsigned int i = 0; i < sizeof(dayNames) / sizeof(dayNames[0]); i++) values[numType->getMinValue() + i] = dayNames[i]; } + result_t result = numType->derive(divisor, bitCount, numType); + if (result!=RESULT_OK) { + return result; + } if (values.empty()) { - result_t result = numType->derive(divisor, bitCount, numType); - if (result!=RESULT_OK) - return result; returnField = new SingleDataField(name, comment, unit, numType, partType, byteCount); return RESULT_OK; } @@ -329,11 +330,11 @@ result_t SingleDataField::read(const PartType partType, default: return RESULT_ERR_INVALID_PART; } + bool remainder = m_length==REMAIN_LEN && m_dataType->isAdjustableLength(); + if (offset + (remainder?1:m_length) > data.size()) { + return RESULT_ERR_INVALID_POS; + } if (isIgnored() || (fieldName != NULL && (m_name != fieldName || fieldIndex > 0))) { - bool remainder = m_length==REMAIN_LEN && m_dataType->isAdjustableLength(); - if (!remainder && offset + m_length > data.size()) { - return RESULT_ERR_INVALID_POS; - } return RESULT_EMPTY; } return m_dataType->readRawValue(data, offset, m_length, output); @@ -358,11 +359,11 @@ result_t SingleDataField::read(const PartType partType, default: return RESULT_ERR_INVALID_PART; } + bool remainder = m_length==REMAIN_LEN && m_dataType->isAdjustableLength(); + if (offset + (remainder?1:m_length) > data.size()) { + return RESULT_ERR_INVALID_POS; + } if (isIgnored() || (fieldName != NULL && (m_name != fieldName || fieldIndex > 0))) { - bool remainder = m_length==REMAIN_LEN && m_dataType->isAdjustableLength(); - if (!remainder && offset + m_length > data.size()) { - return RESULT_ERR_INVALID_POS; - } return RESULT_EMPTY; } @@ -502,7 +503,7 @@ bool SingleDataField::hasFullByteOffset(bool after) } NumberDataType* num = (NumberDataType*)m_dataType; return (num->getBitCount() % 8) == 0 - || (after && num->getFirstBit() + (num->getBitCount() % 8) >= 8); + || (after && num->getFirstBit() + (num->getBitCount() % 8) >= 8); } diff --git a/src/lib/ebus/data.h b/src/lib/ebus/data.h index 852ac472..3b2071c0 100644 --- a/src/lib/ebus/data.h +++ b/src/lib/ebus/data.h @@ -294,11 +294,11 @@ public: /** * Get whether this field uses a full byte offset. - * @param after @p true to check after consuming the bits, false to check before. - * @return true if this field uses a full byte offset, false if this field + * @param after @p true to check after consuming the bits, @p false to check before. + * @return @p true if this field uses a full byte offset, @p false if this field * only consumes a part of a byte and a subsequent field may re-use the same offset. */ - virtual bool hasFullByteOffset(bool after); + bool hasFullByteOffset(bool after); // @copydoc virtual void dump(ostream& output);