Skip to content

Commit

Permalink
[fix] emberAfCopyString should accept size_t size[TE4] (project-chip#…
Browse files Browse the repository at this point in the history
…8241)

* [fix] emberAfCopyString should accept size_t size

* Apply suggestions from code review

Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
2 people authored and Nikita committed Sep 23, 2021
1 parent c47f4f6 commit cdf59b7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/app/util/af.h
Original file line number Diff line number Diff line change
Expand Up @@ -559,13 +559,13 @@ void emberAfCopyInt32u(uint8_t * data, uint16_t index, uint32_t x);
* parameter should indicate the maximum number of characters to copy to the
* destination buffer not including the length byte.
*/
void emberAfCopyString(uint8_t * dest, const uint8_t * src, uint8_t size);
void emberAfCopyString(uint8_t * dest, const uint8_t * src, size_t size);
/*
* @brief Function that copies a ZCL long string into a buffer. The size
* parameter should indicate the maximum number of characters to copy to the
* destination buffer not including the length bytes.
*/
void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, uint16_t size);
void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, size_t size);
/*
* @brief Function that determines the length of a zigbee Cluster Library string
* (where the first byte is assumed to be the length).
Expand Down
4 changes: 2 additions & 2 deletions src/app/util/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,15 @@ static EmberAfStatus typeSensitiveMemCopy(ClusterId clusterId, uint8_t * dest, u
{
return EMBER_ZCL_STATUS_INSUFFICIENT_SPACE;
}
emberAfCopyString(dest, src, static_cast<uint8_t>(bufferSize - 1));
emberAfCopyString(dest, src, bufferSize - 1);
}
else if (emberAfIsLongStringAttributeType(attributeType))
{
if (bufferSize < 2)
{
return EMBER_ZCL_STATUS_INSUFFICIENT_SPACE;
}
emberAfCopyLongString(dest, src, static_cast<uint16_t>(bufferSize - 2));
emberAfCopyLongString(dest, src, bufferSize - 2);
}
else if (emberAfIsThisDataTypeAListType(attributeType))
{
Expand Down
10 changes: 6 additions & 4 deletions src/app/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ void emberAfCopyInt32u(uint8_t * data, uint16_t index, uint32_t x)
data[index + 3] = (uint8_t)(((x) >> 24) & 0xFF);
}

void emberAfCopyString(uint8_t * dest, const uint8_t * src, uint8_t size)
void emberAfCopyString(uint8_t * dest, const uint8_t * src, size_t size)
{
if (src == NULL)
{
Expand All @@ -893,14 +893,15 @@ void emberAfCopyString(uint8_t * dest, const uint8_t * src, uint8_t size)
uint8_t length = emberAfStringLength(src);
if (size < length)
{
length = size;
// Since we have checked that size < length, size must be able to fit into the type of length.
length = static_cast<decltype(length)>(size);
}
memmove(dest + 1, src + 1, length);
dest[0] = length;
}
}

void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, uint16_t size)
void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, size_t size)
{
if (src == NULL)
{
Expand All @@ -916,7 +917,8 @@ void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, uint16_t size)
uint16_t length = emberAfLongStringLength(src);
if (size < length)
{
length = size;
// Since we have checked that size < length, size must be able to fit into the type of length.
length = static_cast<decltype(length)>(size);
}
memmove(dest + 2, src + 2, length);
dest[0] = EMBER_LOW_BYTE(length);
Expand Down

0 comments on commit cdf59b7

Please sign in to comment.