added missing check for invalid position in *DataField::read() and require remainder to be at least 1 byte long, formatting

This commit is contained in:
john30
2016-11-19 11:56:17 +01:00
parent 2d9599a467
commit c3b341d1ba
2 changed files with 16 additions and 15 deletions
+13 -12
View File
@@ -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++) for (unsigned int i = 0; i < sizeof(dayNames) / sizeof(dayNames[0]); i++)
values[numType->getMinValue() + i] = dayNames[i]; values[numType->getMinValue() + i] = dayNames[i];
} }
result_t result = numType->derive(divisor, bitCount, numType);
if (result!=RESULT_OK) {
return result;
}
if (values.empty()) { 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); returnField = new SingleDataField(name, comment, unit, numType, partType, byteCount);
return RESULT_OK; return RESULT_OK;
} }
@@ -329,11 +330,11 @@ result_t SingleDataField::read(const PartType partType,
default: default:
return RESULT_ERR_INVALID_PART; 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))) { 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 RESULT_EMPTY;
} }
return m_dataType->readRawValue(data, offset, m_length, output); return m_dataType->readRawValue(data, offset, m_length, output);
@@ -358,11 +359,11 @@ result_t SingleDataField::read(const PartType partType,
default: default:
return RESULT_ERR_INVALID_PART; 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))) { 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 RESULT_EMPTY;
} }
@@ -502,7 +503,7 @@ bool SingleDataField::hasFullByteOffset(bool after)
} }
NumberDataType* num = (NumberDataType*)m_dataType; NumberDataType* num = (NumberDataType*)m_dataType;
return (num->getBitCount() % 8) == 0 return (num->getBitCount() % 8) == 0
|| (after && num->getFirstBit() + (num->getBitCount() % 8) >= 8); || (after && num->getFirstBit() + (num->getBitCount() % 8) >= 8);
} }
+3 -3
View File
@@ -294,11 +294,11 @@ public:
/** /**
* Get whether this field uses a full byte offset. * Get whether this field uses a full byte offset.
* @param after @p true to check after consuming the bits, false to check before. * @param after @p true to check after consuming the bits, @p false to check before.
* @return true if this field uses a full byte offset, false if this field * @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. * 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 // @copydoc
virtual void dump(ostream& output); virtual void dump(ostream& output);