Skip to content

Commit

Permalink
Refactor min/max checking code a bit to make it more readable. (#13059)
Browse files Browse the repository at this point in the history
This is addressing
#13026 (comment)
which did not happen until after the PR merged.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Jan 13, 2022
1 parent 09747f9 commit d29c6e9
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/app/util/attribute-table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,39 +586,37 @@ EmberAfStatus emAfWriteAttribute(EndpointId endpoint, ClusterId cluster, Attribu
{
EmberAfDefaultAttributeValue minv = metadata->defaultValue.ptrToMinMaxValue->minValue;
EmberAfDefaultAttributeValue maxv = metadata->defaultValue.ptrToMinMaxValue->maxValue;
bool isAttributeSigned = emberAfIsTypeSigned(metadata->attributeType);
uint16_t dataLen = emberAfAttributeSize(metadata);
const uint8_t * minBytes;
const uint8_t * maxBytes;
if (dataLen <= 2)
{
int8_t minR, maxR;
uint8_t * minI = (uint8_t *) &(minv.defaultValue);
uint8_t * maxI = (uint8_t *) &(maxv.defaultValue);
minBytes = reinterpret_cast<const uint8_t *>(&(minv.defaultValue));
maxBytes = reinterpret_cast<const uint8_t *>(&(maxv.defaultValue));
// On big endian cpu with length 1 only the second byte counts
#if (BIGENDIAN_CPU)
if (dataLen == 1)
{
minI++;
maxI++;
minBytes++;
maxBytes++;
}
#endif // BIGENDIAN_CPU
minR = emberAfCompareValues(minI, data, dataLen, isAttributeSigned);
maxR = emberAfCompareValues(maxI, data, dataLen, isAttributeSigned);
if (((minR == 1) || (maxR == -1)) &&
// null value is always in-range for a nullable attribute.
(!metadata->IsNullable() || !IsNullValue(data, dataLen, isAttributeSigned)))
{
return EMBER_ZCL_STATUS_INVALID_VALUE;
}
}
else
{
if (((emberAfCompareValues(minv.ptrToDefaultValue, data, dataLen, isAttributeSigned) == 1) ||
(emberAfCompareValues(maxv.ptrToDefaultValue, data, dataLen, isAttributeSigned) == -1)) &&
// null value is always in-range for a nullable attribute.
(!metadata->IsNullable() || !IsNullValue(data, dataLen, isAttributeSigned)))
{
return EMBER_ZCL_STATUS_INVALID_VALUE;
}
minBytes = minv.ptrToDefaultValue;
maxBytes = maxv.ptrToDefaultValue;
}

bool isAttributeSigned = emberAfIsTypeSigned(metadata->attributeType);
bool isOutOfRange = emberAfCompareValues(minBytes, data, dataLen, isAttributeSigned) == 1 ||
emberAfCompareValues(maxBytes, data, dataLen, isAttributeSigned) == -1;

if (isOutOfRange &&
// null value is always in-range for a nullable attribute.
(!metadata->IsNullable() || !IsNullValue(data, dataLen, isAttributeSigned)))
{
return EMBER_ZCL_STATUS_INVALID_VALUE;
}
}

Expand Down

0 comments on commit d29c6e9

Please sign in to comment.