Skip to content

Commit

Permalink
Change TLV reader/writer init methods to take buffer size as size_t (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored Aug 12, 2021
1 parent 9ff4a17 commit af3fefb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
9 changes: 4 additions & 5 deletions src/credentials/CHIPCertFromX509.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,7 @@ CHIP_ERROR ConvertX509CertsToChipCertArray(const ByteSpan & x509NOC, const ByteS

TLVWriter writer;

// We can still generate the certificate if the output chip cert buffer is bigger than UINT32_MAX,
// since generated cert needs less space than UINT32_MAX.
uint32_t chipCertBufLen = (chipCertArray.size() > UINT32_MAX) ? UINT32_MAX : static_cast<uint32_t>(chipCertArray.size());
writer.Init(chipCertArray.data(), chipCertBufLen);
writer.Init(chipCertArray.data(), chipCertArray.size());

TLVType outerContainer;
ReturnErrorOnFailure(writer.StartContainer(AnonymousTag, kTLVType_Array, outerContainer));
Expand Down Expand Up @@ -767,7 +764,9 @@ CHIP_ERROR ConvertX509CertsToChipCertArray(const ByteSpan & x509NOC, const ByteS
ReturnErrorOnFailure(writer.EndContainer(outerContainer));
ReturnErrorOnFailure(writer.Finalize());

ReturnErrorCodeIf(writer.GetLengthWritten() > chipCertBufLen, CHIP_ERROR_INTERNAL);
// This error return is a bit weird... if we already overran our buffer,
// then fail???
ReturnErrorCodeIf(writer.GetLengthWritten() > chipCertArray.size(), CHIP_ERROR_INTERNAL);
chipCertArray.reduce_size(writer.GetLengthWritten());

return CHIP_NO_ERROR;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/core/CHIPTLV.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class DLL_EXPORT TLVReader
* @param[in] dataLen The length of the TLV data to be parsed.
*
*/
void Init(const uint8_t * data, uint32_t dataLen);
void Init(const uint8_t * data, size_t dataLen);

/**
* Initializes a TLVReader object to read from a TLVBackingStore.
Expand Down Expand Up @@ -849,7 +849,7 @@ class DLL_EXPORT TLVWriter
* @param[in] maxLen The maximum number of bytes that should be written to the output buffer.
*
*/
void Init(uint8_t * buf, uint32_t maxLen);
void Init(uint8_t * buf, size_t maxLen);

/**
* Initializes a TLVWriter object to write into memory provided by a TLVBackingStore.
Expand Down
16 changes: 9 additions & 7 deletions src/lib/core/CHIPTLVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ using namespace chip::Encoding;

static const uint8_t sTagSizes[] = { 0, 1, 2, 4, 2, 4, 6, 8 };

void TLVReader::Init(const uint8_t * data, uint32_t dataLen)
{
mBackingStore = nullptr;
mReadPoint = data;
mBufEnd = data + dataLen;
mLenRead = 0;
mMaxLen = dataLen;
void TLVReader::Init(const uint8_t * data, size_t dataLen)
{
// TODO: Maybe we can just make mMaxLen and mLenRead size_t instead?
uint32_t actualDataLen = dataLen > UINT32_MAX ? UINT32_MAX : static_cast<uint32_t>(dataLen);
mBackingStore = nullptr;
mReadPoint = data;
mBufEnd = data + actualDataLen;
mLenRead = 0;
mMaxLen = actualDataLen;
ClearElementState();
mContainerType = kTLVType_NotSpecified;
SetContainerOpen(false);
Expand Down
10 changes: 6 additions & 4 deletions src/lib/core/CHIPTLVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ namespace TLV {

using namespace chip::Encoding;

NO_INLINE void TLVWriter::Init(uint8_t * buf, uint32_t maxLen)
NO_INLINE void TLVWriter::Init(uint8_t * buf, size_t maxLen)
{
mBackingStore = nullptr;
// TODO: Maybe we can just make mMaxLen, mLenWritten, mRemainingLen size_t instead?
uint32_t actualMaxLen = maxLen > UINT32_MAX ? UINT32_MAX : static_cast<uint32_t>(maxLen);
mBackingStore = nullptr;
mBufStart = mWritePoint = buf;
mRemainingLen = maxLen;
mRemainingLen = actualMaxLen;
mLenWritten = 0;
mMaxLen = maxLen;
mMaxLen = actualMaxLen;
mContainerType = kTLVType_NotSpecified;
SetContainerOpen(false);
SetCloseContainerReserved(true);
Expand Down

0 comments on commit af3fefb

Please sign in to comment.