diff --git a/src/credentials/CHIPCertFromX509.cpp b/src/credentials/CHIPCertFromX509.cpp index 15cc47669511bf..099709facf0525 100644 --- a/src/credentials/CHIPCertFromX509.cpp +++ b/src/credentials/CHIPCertFromX509.cpp @@ -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(chipCertArray.size()); - writer.Init(chipCertArray.data(), chipCertBufLen); + writer.Init(chipCertArray.data(), chipCertArray.size()); TLVType outerContainer; ReturnErrorOnFailure(writer.StartContainer(AnonymousTag, kTLVType_Array, outerContainer)); @@ -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; diff --git a/src/lib/core/CHIPTLV.h b/src/lib/core/CHIPTLV.h index 680eb60872d944..e50c3b26f75d0a 100644 --- a/src/lib/core/CHIPTLV.h +++ b/src/lib/core/CHIPTLV.h @@ -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. @@ -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. diff --git a/src/lib/core/CHIPTLVReader.cpp b/src/lib/core/CHIPTLVReader.cpp index d614493b38cf9f..42627d1cbd8550 100644 --- a/src/lib/core/CHIPTLVReader.cpp +++ b/src/lib/core/CHIPTLVReader.cpp @@ -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(dataLen); + mBackingStore = nullptr; + mReadPoint = data; + mBufEnd = data + actualDataLen; + mLenRead = 0; + mMaxLen = actualDataLen; ClearElementState(); mContainerType = kTLVType_NotSpecified; SetContainerOpen(false); diff --git a/src/lib/core/CHIPTLVWriter.cpp b/src/lib/core/CHIPTLVWriter.cpp index 1f09fee85fa372..514a0a335e8b81 100644 --- a/src/lib/core/CHIPTLVWriter.cpp +++ b/src/lib/core/CHIPTLVWriter.cpp @@ -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(maxLen); + mBackingStore = nullptr; mBufStart = mWritePoint = buf; - mRemainingLen = maxLen; + mRemainingLen = actualMaxLen; mLenWritten = 0; - mMaxLen = maxLen; + mMaxLen = actualMaxLen; mContainerType = kTLVType_NotSpecified; SetContainerOpen(false); SetCloseContainerReserved(true);