diff --git a/src/ble/BtpEngine.cpp b/src/ble/BtpEngine.cpp index 3183c6db79aed1..7f4f1e27d7945f 100644 --- a/src/ble/BtpEngine.cpp +++ b/src/ble/BtpEngine.cpp @@ -310,7 +310,7 @@ CHIP_ERROR BtpEngine::HandleCharacteristicReceived(System::PacketBufferHandle && // mRxFragnentSize may be smaller than the characteristic size. Make sure // we're not truncating to a data length smaller than what we have already consumed. VerifyOrExit(reader.OctetsRead() <= mRxFragmentSize, err = BLE_ERROR_REASSEMBLER_INCORRECT_STATE); - data->SetDataLength(chip::min(data->DataLength(), mRxFragmentSize)); + data->SetDataLength(chip::min(data->DataLength(), static_cast(mRxFragmentSize))); // Now mark the bytes we consumed as consumed. data->ConsumeHead(static_cast(reader.OctetsRead())); @@ -374,7 +374,7 @@ CHIP_ERROR BtpEngine::HandleCharacteristicReceived(System::PacketBufferHandle && if (rx_flags.Has(HeaderFlags::kEndMessage)) { // Trim remainder, if any, of the received packet buffer based on sender-specified length of reassembled message. - int padding = mRxBuf->DataLength() - mRxLength; + uint32_t padding = mRxBuf->DataLength() - mRxLength; if (padding > 0) { @@ -456,7 +456,7 @@ bool BtpEngine::HandleCharacteristicSend(System::PacketBufferHandle data, bool s mTxBuf = std::move(data); mTxState = kState_InProgress; - mTxLength = mTxBuf->DataLength(); + mTxLength = static_cast(mTxBuf->DataLength()); ChipLogDebugBtpEngine(Ble, ">>> CHIPoBle preparing to send whole message:"); PrintBufDebug(mTxBuf); diff --git a/src/inet/TCPEndPoint.cpp b/src/inet/TCPEndPoint.cpp index 15b14151a235ce..db9c345a56774b 100644 --- a/src/inet/TCPEndPoint.cpp +++ b/src/inet/TCPEndPoint.cpp @@ -333,7 +333,7 @@ void TCPEndPoint::DriveReceiving() { // Acknowledgement is done after handling the buffers to allow the // application processing to throttle flow. - uint16_t ackLength = mRcvQueue->TotalLength(); + uint32_t ackLength = mRcvQueue->TotalLength(); CHIP_ERROR err = OnDataReceived(this, std::move(mRcvQueue)); if (err != CHIP_NO_ERROR) { diff --git a/src/inet/TCPEndPoint.h b/src/inet/TCPEndPoint.h index 0e4c17b12d5876..6357a3a774fb3e 100644 --- a/src/inet/TCPEndPoint.h +++ b/src/inet/TCPEndPoint.h @@ -274,7 +274,7 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis * received. The operational semantics are undefined if \c len is larger * than the total outstanding unacknowledged received data. */ - virtual CHIP_ERROR AckReceive(uint16_t len) = 0; + virtual CHIP_ERROR AckReceive(uint32_t len) = 0; /** * @brief Set the receive queue, for testing. @@ -301,7 +301,7 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis * @brief Extract the length of the unacknowledged receive data. * * @return Number of bytes in the receive queue that have not yet been - * acknowledged with AckReceive(uint16_t len). + * acknowledged with AckReceive(uint32_t len). */ uint32_t PendingReceiveLength(); @@ -447,7 +447,7 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis * is the length of the message text added to the TCP transmit window, * which are eligible for sending by the underlying network stack. */ - typedef void (*OnDataSentFunct)(TCPEndPoint * endPoint, uint16_t len); + typedef void (*OnDataSentFunct)(TCPEndPoint * endPoint, uint32_t len); /** * The endpoint's message text transmission event handling function diff --git a/src/inet/TCPEndPointImplLwIP.cpp b/src/inet/TCPEndPointImplLwIP.cpp index 02e5ac97b46d16..b3e7834c616199 100644 --- a/src/inet/TCPEndPointImplLwIP.cpp +++ b/src/inet/TCPEndPointImplLwIP.cpp @@ -503,7 +503,7 @@ void TCPEndPointImplLwIP::DoCloseImpl(CHIP_ERROR err, State oldState) } } -CHIP_ERROR TCPEndPointImplLwIP::AckReceive(uint16_t len) +CHIP_ERROR TCPEndPointImplLwIP::AckReceive(uint32_t len) { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); CHIP_ERROR res = CHIP_NO_ERROR; diff --git a/src/inet/TCPEndPointImplLwIP.h b/src/inet/TCPEndPointImplLwIP.h index b680f86b3b64e5..c98e28902491fb 100644 --- a/src/inet/TCPEndPointImplLwIP.h +++ b/src/inet/TCPEndPointImplLwIP.h @@ -51,7 +51,7 @@ class TCPEndPointImplLwIP : public TCPEndPoint, public EndPointStateLwIP CHIP_ERROR EnableNoDelay() override; CHIP_ERROR EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) override; CHIP_ERROR DisableKeepAlive() override; - CHIP_ERROR AckReceive(uint16_t len) override; + CHIP_ERROR AckReceive(uint32_t len) override; #if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT void TCPUserTimeoutHandler() override; #endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT diff --git a/src/inet/TCPEndPointImplOpenThread.cpp b/src/inet/TCPEndPointImplOpenThread.cpp index 36522ca3c47459..35f3766d3e639d 100644 --- a/src/inet/TCPEndPointImplOpenThread.cpp +++ b/src/inet/TCPEndPointImplOpenThread.cpp @@ -54,7 +54,7 @@ CHIP_ERROR TCPEndPointImplOT::DisableKeepAlive() { return CHIP_ERROR_NOT_IMPLEMENTED; } -CHIP_ERROR TCPEndPointImplOT::AckReceive(uint16_t len) +CHIP_ERROR TCPEndPointImplOT::AckReceive(uint32_t len) { return CHIP_ERROR_NOT_IMPLEMENTED; } diff --git a/src/inet/TCPEndPointImplOpenThread.h b/src/inet/TCPEndPointImplOpenThread.h index c62c63cdc85f55..166750f1a1e5ea 100644 --- a/src/inet/TCPEndPointImplOpenThread.h +++ b/src/inet/TCPEndPointImplOpenThread.h @@ -46,7 +46,7 @@ class TCPEndPointImplOT : public TCPEndPoint, public EndPointStateOpenThread CHIP_ERROR EnableNoDelay() override; CHIP_ERROR EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) override; CHIP_ERROR DisableKeepAlive() override; - CHIP_ERROR AckReceive(uint16_t len) override; + CHIP_ERROR AckReceive(uint32_t len) override; #if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT void TCPUserTimeoutHandler() override; #endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT diff --git a/src/inet/TCPEndPointImplSockets.cpp b/src/inet/TCPEndPointImplSockets.cpp index fc8c6e2da2a77e..da1eeb52b8c186 100644 --- a/src/inet/TCPEndPointImplSockets.cpp +++ b/src/inet/TCPEndPointImplSockets.cpp @@ -441,7 +441,7 @@ CHIP_ERROR TCPEndPointImplSockets::DisableKeepAlive() return CHIP_NO_ERROR; } -CHIP_ERROR TCPEndPointImplSockets::AckReceive(uint16_t len) +CHIP_ERROR TCPEndPointImplSockets::AckReceive(uint32_t len) { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); @@ -483,7 +483,7 @@ CHIP_ERROR TCPEndPointImplSockets::DriveSendingImpl() while (!mSendQueue.IsNull()) { - uint16_t bufLen = mSendQueue->DataLength(); + uint32_t bufLen = mSendQueue->DataLength(); ssize_t lenSentRaw = send(mSocket, mSendQueue->Start(), bufLen, sendFlags); @@ -502,8 +502,8 @@ CHIP_ERROR TCPEndPointImplSockets::DriveSendingImpl() break; } - // Cast is safe because bufLen is uint16_t. - uint16_t lenSent = static_cast(lenSentRaw); + // Cast is safe because bufLen is uint32_t. + uint32_t lenSent = static_cast(lenSentRaw); // Mark the connection as being active. MarkActive(); @@ -948,10 +948,10 @@ void TCPEndPointImplSockets::ReceiveData() { VerifyOrDie(rcvLen > 0); size_t newDataLength = rcvBuf->DataLength() + static_cast(rcvLen); - VerifyOrDie(CanCastTo(newDataLength)); + VerifyOrDie(CanCastTo(newDataLength)); if (isNewBuf) { - rcvBuf->SetDataLength(static_cast(newDataLength)); + rcvBuf->SetDataLength(static_cast(newDataLength)); rcvBuf.RightSize(); if (mRcvQueue.IsNull()) { @@ -964,7 +964,7 @@ void TCPEndPointImplSockets::ReceiveData() } else { - rcvBuf->SetDataLength(static_cast(newDataLength), mRcvQueue); + rcvBuf->SetDataLength(static_cast(newDataLength), mRcvQueue); } } } diff --git a/src/inet/TCPEndPointImplSockets.h b/src/inet/TCPEndPointImplSockets.h index 40e81eeb6a0d06..ab076420cd2b57 100644 --- a/src/inet/TCPEndPointImplSockets.h +++ b/src/inet/TCPEndPointImplSockets.h @@ -46,7 +46,7 @@ class TCPEndPointImplSockets : public TCPEndPoint, public EndPointStateSockets CHIP_ERROR EnableNoDelay() override; CHIP_ERROR EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) override; CHIP_ERROR DisableKeepAlive() override; - CHIP_ERROR AckReceive(uint16_t len) override; + CHIP_ERROR AckReceive(uint32_t len) override; #if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT void TCPUserTimeoutHandler() override; #endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT diff --git a/src/inet/tests/TestInetLayer.cpp b/src/inet/tests/TestInetLayer.cpp index 47e83316228b5f..8affc7076d322b 100644 --- a/src/inet/tests/TestInetLayer.cpp +++ b/src/inet/tests/TestInetLayer.cpp @@ -569,7 +569,7 @@ static void HandleTCPConnectionClosed(TCPEndPoint * aEndPoint, CHIP_ERROR aError } } -static void HandleTCPDataSent(TCPEndPoint * aEndPoint, uint16_t len) {} +static void HandleTCPDataSent(TCPEndPoint * aEndPoint, uint32_t len) {} static CHIP_ERROR HandleTCPDataReceived(TCPEndPoint * aEndPoint, PacketBufferHandle && aBuffer) { diff --git a/src/inet/tests/TestInetLayerCommon.cpp b/src/inet/tests/TestInetLayerCommon.cpp index 7a60a2778239ba..8e40ff573d5447 100644 --- a/src/inet/tests/TestInetLayerCommon.cpp +++ b/src/inet/tests/TestInetLayerCommon.cpp @@ -123,9 +123,9 @@ bool WasSuccessful(const TestStatus & aTestStatus) return (lStatus); } -static void FillDataBufferPattern(uint8_t * aBuffer, uint16_t aLength, uint16_t aPatternStartOffset, uint8_t aFirstValue) +static void FillDataBufferPattern(uint8_t * aBuffer, uint32_t aLength, uint16_t aPatternStartOffset, uint8_t aFirstValue) { - for (uint16_t i = aPatternStartOffset; i < aLength; i++) + for (uint32_t i = aPatternStartOffset; i < aLength; i++) { const uint8_t lValue = static_cast(aFirstValue & 0xFF); @@ -135,9 +135,9 @@ static void FillDataBufferPattern(uint8_t * aBuffer, uint16_t aLength, uint16_t } } -static bool CheckDataBufferPattern(const uint8_t * aBuffer, uint16_t aLength, uint16_t aPatternStartOffset, uint8_t aFirstValue) +static bool CheckDataBufferPattern(const uint8_t * aBuffer, uint32_t aLength, uint16_t aPatternStartOffset, uint8_t aFirstValue) { - for (uint16_t i = aPatternStartOffset; i < aLength; i++) + for (uint32_t i = aPatternStartOffset; i < aLength; i++) { const uint8_t lValue = aBuffer[i]; @@ -156,7 +156,7 @@ static bool CheckDataBufferPattern(const uint8_t * aBuffer, uint16_t aLength, ui return true; } -static PacketBufferHandle MakeDataBuffer(uint16_t aDesiredLength, uint16_t aPatternStartOffset, uint8_t aFirstValue) +static PacketBufferHandle MakeDataBuffer(uint32_t aDesiredLength, uint16_t aPatternStartOffset, uint8_t aFirstValue) { VerifyOrReturnError(aPatternStartOffset <= aDesiredLength, PacketBufferHandle()); @@ -244,7 +244,7 @@ static bool HandleDataReceived(const PacketBufferHandle & aBuffer, TransferStats for (PacketBufferHandle lBuffer = aBuffer.Retain(); !lBuffer.IsNull(); lBuffer.Advance()) { - const uint16_t lDataLength = lBuffer->DataLength(); + const uint32_t lDataLength = lBuffer->DataLength(); const uint8_t * const p = lBuffer->Start(); if (aCheckBuffer && !CheckDataBufferPattern(p, lDataLength, aPatternStartOffset, aFirstValue)) diff --git a/src/lib/core/tests/TestTLV.cpp b/src/lib/core/tests/TestTLV.cpp index fce0a5d17ae484..959f88228949f7 100644 --- a/src/lib/core/tests/TestTLV.cpp +++ b/src/lib/core/tests/TestTLV.cpp @@ -277,7 +277,7 @@ void TestBufferContents(nlTestSuite * inSuite, const System::PacketBufferHandle System::PacketBufferHandle buf = buffer.Retain(); while (!buf.IsNull()) { - uint16_t len = buf->DataLength(); + uint32_t len = buf->DataLength(); NL_TEST_ASSERT(inSuite, len <= expectedLen); NL_TEST_ASSERT(inSuite, memcmp(buf->Start(), expectedVal, len) == 0); @@ -2990,7 +2990,7 @@ void CheckBufferOverflow(nlTestSuite * inSuite, void * inContext) System::PacketBufferTLVReader reader; System::PacketBufferHandle buf = System::PacketBufferHandle::New(sizeof(Encoding1), 0); - uint16_t maxDataLen = buf->MaxDataLength(); + uint32_t maxDataLen = buf->MaxDataLength(); uint16_t reserve = static_cast((sizeof(Encoding1) < maxDataLen) ? (maxDataLen - sizeof(Encoding1)) + 2 : 0); // Repeatedly write and read a TLV encoding to a chain of PacketBuffers. Use progressively larger diff --git a/src/protocols/secure_channel/CASESession.cpp b/src/protocols/secure_channel/CASESession.cpp index 8573d1565686fe..547aec15f0733d 100644 --- a/src/protocols/secure_channel/CASESession.cpp +++ b/src/protocols/secure_channel/CASESession.cpp @@ -1571,7 +1571,7 @@ CHIP_ERROR CASESession::HandleSigma3a(System::PacketBufferHandle && msg) TLV::TLVType containerType = TLV::kTLVType_Structure; const uint8_t * buf = msg->Start(); - const uint16_t bufLen = msg->DataLength(); + const uint32_t bufLen = msg->DataLength(); constexpr size_t kCaseOverheadForFutureTbeData = 128; diff --git a/src/system/SystemPacketBuffer.cpp b/src/system/SystemPacketBuffer.cpp index 32f4b8e3830077..b698122942c28b 100644 --- a/src/system/SystemPacketBuffer.cpp +++ b/src/system/SystemPacketBuffer.cpp @@ -127,7 +127,7 @@ void PacketBuffer::InternalCheck(const PacketBuffer * buffer) #endif // CHIP_SYSTEM_PACKETBUFFER_HAS_CHECK // Number of unused bytes below which \c RightSize() won't bother reallocating. -constexpr uint16_t kRightSizingThreshold = 16; +constexpr uint32_t kRightSizingThreshold = 16; void PacketBufferHandle::InternalRightSize() { @@ -140,7 +140,7 @@ void PacketBufferHandle::InternalRightSize() // Reallocate only if enough space will be saved. const uint8_t * const start = mBuffer->ReserveStart(); const uint8_t * const payload = mBuffer->Start(); - const uint16_t usedSize = static_cast(payload - start + mBuffer->len); + const uint32_t usedSize = static_cast(payload - start + mBuffer->len); if (usedSize + kRightSizingThreshold > mBuffer->alloc_size) { return; @@ -160,7 +160,7 @@ void PacketBufferHandle::InternalRightSize() newBuffer->tot_len = mBuffer->tot_len; newBuffer->len = mBuffer->len; newBuffer->ref = 1; - newBuffer->alloc_size = static_cast(usedSize); + newBuffer->alloc_size = static_cast(usedSize); memcpy(newStart, start, usedSize); PacketBuffer::Free(mBuffer); @@ -210,14 +210,14 @@ void PacketBuffer::SetStart(uint8_t * aNewStart) if (lDelta > this->len) lDelta = this->len; - this->len = static_cast(static_cast(this->len) - lDelta); - this->tot_len = static_cast(static_cast(this->tot_len) - lDelta); + this->len = static_cast(static_cast(this->len) - lDelta); + this->tot_len = static_cast(static_cast(this->tot_len) - lDelta); this->payload = aNewStart; } -void PacketBuffer::SetDataLength(uint16_t aNewLen, PacketBuffer * aChainHead) +void PacketBuffer::SetDataLength(uint32_t aNewLen, PacketBuffer * aChainHead) { - const uint16_t kMaxDataLen = this->MaxDataLength(); + const uint32_t kMaxDataLen = this->MaxDataLength(); if (aNewLen > kMaxDataLen) aNewLen = kMaxDataLen; @@ -225,7 +225,7 @@ void PacketBuffer::SetDataLength(uint16_t aNewLen, PacketBuffer * aChainHead) ptrdiff_t lDelta = static_cast(aNewLen) - static_cast(this->len); this->len = aNewLen; - this->tot_len = static_cast(this->tot_len + lDelta); + this->tot_len = static_cast(this->tot_len + lDelta); // SetDataLength is often called after a client finished writing to the buffer, // so it's a good time to check for possible corruption. @@ -234,12 +234,12 @@ void PacketBuffer::SetDataLength(uint16_t aNewLen, PacketBuffer * aChainHead) while (aChainHead != nullptr && aChainHead != this) { Check(aChainHead); - aChainHead->tot_len = static_cast(aChainHead->tot_len + lDelta); + aChainHead->tot_len = static_cast(aChainHead->tot_len + lDelta); aChainHead = aChainHead->ChainedBuffer(); } } -uint16_t PacketBuffer::MaxDataLength() const +uint32_t PacketBuffer::MaxDataLength() const { #if CHIP_SYSTEM_CONFIG_USE_LWIP if (!(PBUF_STRUCT_DATA_CONTIGUOUS(this))) @@ -247,17 +247,17 @@ uint16_t PacketBuffer::MaxDataLength() const return DataLength(); } #endif - return static_cast(AllocSize() - ReservedSize()); + return static_cast(AllocSize() - ReservedSize()); } -uint16_t PacketBuffer::AvailableDataLength() const +uint32_t PacketBuffer::AvailableDataLength() const { - return static_cast(this->MaxDataLength() - this->DataLength()); + return static_cast(this->MaxDataLength() - this->DataLength()); } uint16_t PacketBuffer::ReservedSize() const { - // Cast to uint16_t is safe because Start() always points to "after" + // Cast to uint32_t is safe because Start() always points to "after" // ReserveStart(). At least when the payload is stored inline. return static_cast(Start() - ReserveStart()); } @@ -296,8 +296,8 @@ void PacketBuffer::AddToEnd(PacketBufferHandle && aPacketHandle) while (true) { - uint16_t old_total_length = lCursor->tot_len; - lCursor->tot_len = static_cast(lCursor->tot_len + aPacket->tot_len); + uint32_t old_total_length = lCursor->tot_len; + lCursor->tot_len = static_cast(lCursor->tot_len + aPacket->tot_len); VerifyOrDieWithMsg(lCursor->tot_len >= old_total_length, chipSystemLayer, "buffer chain too large"); if (!lCursor->HasChainedBuffer()) { @@ -320,37 +320,37 @@ void PacketBuffer::CompactHead() this->payload = kStart; } - uint16_t lAvailLength = this->AvailableDataLength(); + uint32_t lAvailLength = this->AvailableDataLength(); while (lAvailLength > 0 && HasChainedBuffer()) { PacketBuffer & lNextPacket = *ChainedBuffer(); VerifyOrDieWithMsg(lNextPacket.ref == 1, chipSystemLayer, "next buffer %p is not exclusive to this chain", &lNextPacket); - uint16_t lMoveLength = lNextPacket.len; + uint32_t lMoveLength = lNextPacket.len; if (lMoveLength > lAvailLength) lMoveLength = lAvailLength; memcpy(static_cast(this->payload) + this->len, lNextPacket.payload, lMoveLength); lNextPacket.payload = static_cast(lNextPacket.payload) + lMoveLength; - this->len = static_cast(this->len + lMoveLength); - lAvailLength = static_cast(lAvailLength - lMoveLength); - lNextPacket.len = static_cast(lNextPacket.len - lMoveLength); - lNextPacket.tot_len = static_cast(lNextPacket.tot_len - lMoveLength); + this->len = static_cast(this->len + lMoveLength); + lAvailLength = static_cast(lAvailLength - lMoveLength); + lNextPacket.len = static_cast(lNextPacket.len - lMoveLength); + lNextPacket.tot_len = static_cast(lNextPacket.tot_len - lMoveLength); if (lNextPacket.len == 0) this->next = this->FreeHead(&lNextPacket); } } -void PacketBuffer::ConsumeHead(uint16_t aConsumeLength) +void PacketBuffer::ConsumeHead(uint32_t aConsumeLength) { if (aConsumeLength > this->len) aConsumeLength = this->len; this->payload = static_cast(this->payload) + aConsumeLength; - this->len = static_cast(this->len - aConsumeLength); - this->tot_len = static_cast(this->tot_len - aConsumeLength); + this->len = static_cast(this->len - aConsumeLength); + this->tot_len = static_cast(this->tot_len - aConsumeLength); } /** @@ -364,18 +364,18 @@ void PacketBuffer::ConsumeHead(uint16_t aConsumeLength) * * @return the first buffer from the current chain that contains any remaining data. If no data remains, nullptr is returned. */ -PacketBuffer * PacketBuffer::Consume(uint16_t aConsumeLength) +PacketBuffer * PacketBuffer::Consume(uint32_t aConsumeLength) { PacketBuffer * lPacket = this; while (lPacket != nullptr && aConsumeLength > 0) { - const uint16_t kLength = lPacket->DataLength(); + const uint32_t kLength = lPacket->DataLength(); if (aConsumeLength >= kLength) { lPacket = PacketBuffer::FreeHead(lPacket); - aConsumeLength = static_cast(aConsumeLength - kLength); + aConsumeLength = static_cast(aConsumeLength - kLength); } else { @@ -480,11 +480,11 @@ PacketBufferHandle PacketBufferHandle::New(size_t aAvailableSize, uint16_t aRese // assuming int is at least 32 bits. static_assert(INT_MAX >= INT32_MAX, "int is not big enough"); static_assert(PacketBuffer::kStructureSize == sizeof(PacketBuffer), "PacketBuffer size mismatch"); - static_assert(PacketBuffer::kStructureSize < UINT16_MAX, "Check for overflow more carefully"); + static_assert(PacketBuffer::kStructureSize < UINT32_MAX, "Check for overflow more carefully"); static_assert(SIZE_MAX >= INT_MAX, "Our additions might not fit in size_t"); - static_assert(PacketBuffer::kMaxSizeWithoutReserve <= UINT16_MAX, "PacketBuffer may have size not fitting uint16_t"); + static_assert(PacketBuffer::kMaxSizeWithoutReserve <= UINT32_MAX, "PacketBuffer may have size not fitting uint32_t"); - // When `aAvailableSize` fits in uint16_t (as tested below) and size_t is at least 32 bits (as asserted above), + // When `aAvailableSize` fits in uint32_t (as tested below) and size_t is at least 32 bits (as asserted above), // these additions will not overflow. const size_t lAllocSize = aReservedSize + aAvailableSize; const size_t lBlockSize = PacketBuffer::kStructureSize + lAllocSize; @@ -492,7 +492,7 @@ PacketBufferHandle PacketBufferHandle::New(size_t aAvailableSize, uint16_t aRese CHIP_SYSTEM_FAULT_INJECT(FaultInjection::kFault_PacketBufferNew, return PacketBufferHandle()); - if (aAvailableSize > UINT16_MAX || lAllocSize > PacketBuffer::kMaxSizeWithoutReserve || lBlockSize > UINT16_MAX) + if (aAvailableSize > UINT32_MAX || lAllocSize > PacketBuffer::kMaxSizeWithoutReserve || lBlockSize > UINT32_MAX) { ChipLogError(chipSystemLayer, "PacketBuffer: allocation too large."); return PacketBufferHandle(); @@ -501,7 +501,7 @@ PacketBufferHandle PacketBufferHandle::New(size_t aAvailableSize, uint16_t aRese #if CHIP_SYSTEM_CONFIG_USE_LWIP lPacket = static_cast( - pbuf_alloc(PBUF_RAW, static_cast(lAllocSize), CHIP_SYSTEM_PACKETBUFFER_LWIP_PBUF_TYPE)); + pbuf_alloc(PBUF_RAW, static_cast(lAllocSize), CHIP_SYSTEM_PACKETBUFFER_LWIP_PBUF_TYPE)); SYSTEM_STATS_UPDATE_LWIP_PBUF_COUNTS(); @@ -545,27 +545,27 @@ PacketBufferHandle PacketBufferHandle::New(size_t aAvailableSize, uint16_t aRese lPacket->next = nullptr; lPacket->ref = 1; #if CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_HEAP - lPacket->alloc_size = static_cast(lAllocSize); + lPacket->alloc_size = static_cast(lAllocSize); #endif return PacketBufferHandle(lPacket); } -PacketBufferHandle PacketBufferHandle::NewWithData(const void * aData, size_t aDataSize, uint16_t aAdditionalSize, +PacketBufferHandle PacketBufferHandle::NewWithData(const void * aData, size_t aDataSize, uint32_t aAdditionalSize, uint16_t aReservedSize) { - if (aDataSize > UINT16_MAX) + if (aDataSize > UINT32_MAX) { ChipLogError(chipSystemLayer, "PacketBuffer: allocation too large."); return PacketBufferHandle(); } - // Since `aDataSize` fits in uint16_t, the sum `aDataSize + aAdditionalSize` will not overflow. + // Since `aDataSize` fits in uint32_t, the sum `aDataSize + aAdditionalSize` will not overflow. // `New()` will only return a non-null buffer if the total allocation size does not overflow. PacketBufferHandle buffer = New(aDataSize + aAdditionalSize, aReservedSize); if (buffer.mBuffer != nullptr) { memcpy(buffer.mBuffer->payload, aData, aDataSize); - buffer.mBuffer->len = buffer.mBuffer->tot_len = static_cast(aDataSize); + buffer.mBuffer->len = buffer.mBuffer->tot_len = static_cast(aDataSize); } return buffer; } @@ -681,7 +681,7 @@ PacketBufferHandle PacketBufferHandle::CloneData() const for (PacketBuffer * original = mBuffer; original != nullptr; original = original->ChainedBuffer()) { - uint16_t originalDataSize = original->MaxDataLength(); + uint32_t originalDataSize = original->MaxDataLength(); uint16_t originalReservedSize = original->ReservedSize(); if (originalDataSize + originalReservedSize > PacketBuffer::kMaxSizeWithoutReserve) @@ -695,7 +695,7 @@ PacketBufferHandle PacketBufferHandle::CloneData() const } // Otherwise, reduce the requested data size. This subtraction can not underflow because the above test // guarantees originalReservedSize <= PacketBuffer::kMaxSizeWithoutReserve. - originalDataSize = static_cast(PacketBuffer::kMaxSizeWithoutReserve - originalReservedSize); + originalDataSize = static_cast(PacketBuffer::kMaxSizeWithoutReserve - originalReservedSize); } PacketBufferHandle clone = PacketBufferHandle::New(originalDataSize, originalReservedSize); @@ -728,8 +728,8 @@ System::PacketBufferHandle PacketBufferWriterUtil::Finalize(BufferWriter & aBuff if (!aPacket.IsNull() && aBufferWriter.Fit()) { // Since mPacket was successfully allocated to hold the maximum length, - // we know that the actual length fits in a uint16_t. - aPacket->SetDataLength(static_cast(aBufferWriter.Needed())); + // we know that the actual length fits in a uint32_t. + aPacket->SetDataLength(static_cast(aBufferWriter.Needed())); } else { diff --git a/src/system/SystemPacketBuffer.h b/src/system/SystemPacketBuffer.h index 88db4196438c1d..b61b3606909e96 100644 --- a/src/system/SystemPacketBuffer.h +++ b/src/system/SystemPacketBuffer.h @@ -56,11 +56,11 @@ struct pbuf { struct pbuf * next; void * payload; - uint16_t tot_len; - uint16_t len; - uint16_t ref; + uint32_t tot_len; + uint32_t len; + uint32_t ref; #if CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_HEAP - uint16_t alloc_size; + uint32_t alloc_size; #endif }; #endif // !CHIP_SYSTEM_CONFIG_USE_LWIP @@ -114,9 +114,9 @@ class DLL_EXPORT PacketBuffer : private pbuf private: // The effective size of the packet buffer structure. #if CHIP_SYSTEM_CONFIG_USE_LWIP - static constexpr uint16_t kStructureSize = LWIP_MEM_ALIGN_SIZE(sizeof(struct ::pbuf)); + static constexpr uint32_t kStructureSize = LWIP_MEM_ALIGN_SIZE(sizeof(struct ::pbuf)); #else // CHIP_SYSTEM_CONFIG_USE_LWIP - static constexpr uint16_t kStructureSize = CHIP_SYSTEM_ALIGN_SIZE(sizeof(::chip::System::pbuf), 4u); + static constexpr uint32_t kStructureSize = CHIP_SYSTEM_ALIGN_SIZE(sizeof(::chip::System::pbuf), 4u); #endif // CHIP_SYSTEM_CONFIG_USE_LWIP public: @@ -124,21 +124,21 @@ class DLL_EXPORT PacketBuffer : private pbuf * The maximum size buffer an application can allocate with no protocol header reserve. */ #if CHIP_SYSTEM_CONFIG_USE_LWIP - static constexpr uint16_t kMaxSizeWithoutReserve = LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE); + static constexpr uint32_t kMaxSizeWithoutReserve = LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE); #else - static constexpr uint16_t kMaxSizeWithoutReserve = CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX; + static constexpr uint32_t kMaxSizeWithoutReserve = CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX; #endif /** * The number of bytes to reserve in a network packet buffer to contain all the possible protocol encapsulation headers * before the application data. */ - static constexpr uint16_t kDefaultHeaderReserve = CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE; + static constexpr uint32_t kDefaultHeaderReserve = CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE; /** * The maximum size buffer an application can allocate with the default protocol header reserve. */ - static constexpr uint16_t kMaxSize = kMaxSizeWithoutReserve - kDefaultHeaderReserve; + static constexpr uint32_t kMaxSize = kMaxSizeWithoutReserve - kDefaultHeaderReserve; /** * Return the size of the allocation including the reserved and payload data spaces but not including space @@ -148,7 +148,7 @@ class DLL_EXPORT PacketBuffer : private pbuf * * @return size of the allocation */ - uint16_t AllocSize() const + uint32_t AllocSize() const { #if CHIP_SYSTEM_PACKETBUFFER_FROM_LWIP_STANDARD_POOL || CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_POOL return kMaxSizeWithoutReserve; @@ -191,7 +191,7 @@ class DLL_EXPORT PacketBuffer : private pbuf * * @return length, in bytes (current payload length). */ - uint16_t DataLength() const { return this->len; } + uint32_t DataLength() const { return this->len; } /** * Set the length, in bytes, of data in a packet buffer, adjusting total length accordingly. @@ -206,29 +206,29 @@ class DLL_EXPORT PacketBuffer : private pbuf * @param[in,out] aChainHead - the head of the buffer chain the current buffer belongs to. May be \c nullptr if the current * buffer is the head of the buffer chain. */ - void SetDataLength(uint16_t aNewLen, const PacketBufferHandle & aChainHead); - void SetDataLength(uint16_t aNewLen) { SetDataLength(aNewLen, nullptr); } + void SetDataLength(uint32_t aNewLen, const PacketBufferHandle & aChainHead); + void SetDataLength(uint32_t aNewLen) { SetDataLength(aNewLen, nullptr); } /** * Get the total length of packet data in the buffer chain. * * @return total length, in octets. */ - uint16_t TotalLength() const { return this->tot_len; } + uint32_t TotalLength() const { return this->tot_len; } /** * Get the maximum amount, in bytes, of data that will fit in the buffer given the current start position and buffer size. * * @return number of bytes that fits in the buffer given the current start position. */ - uint16_t MaxDataLength() const; + uint32_t MaxDataLength() const; /** * Get the number of bytes of data that can be added to the current buffer given the current start position and data length. * * @return the length, in bytes, of data that will fit in the current buffer given the current start position and data length. */ - uint16_t AvailableDataLength() const; + uint32_t AvailableDataLength() const; /** * Get the number of bytes within the current buffer between the start of the buffer and the current data start position. @@ -274,7 +274,7 @@ class DLL_EXPORT PacketBuffer : private pbuf * * @param[in] aConsumeLength - number of bytes to consume from the current buffer. */ - void ConsumeHead(uint16_t aConsumeLength); + void ConsumeHead(uint32_t aConsumeLength); /** * Ensure the buffer has at least the specified amount of reserved space. @@ -355,7 +355,7 @@ class DLL_EXPORT PacketBuffer : private pbuf private: // Memory required for a maximum-size PacketBuffer. - static constexpr uint16_t kBlockSize = PacketBuffer::kStructureSize + PacketBuffer::kMaxSizeWithoutReserve; + static constexpr uint32_t kBlockSize = PacketBuffer::kStructureSize + PacketBuffer::kMaxSizeWithoutReserve; // Note: this condition includes DOXYGEN to work around a Doxygen error. DOXYGEN is never defined in any actual build. #if CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_POOL || defined(DOXYGEN) @@ -379,9 +379,9 @@ class DLL_EXPORT PacketBuffer : private pbuf static PacketBuffer * FreeHead(PacketBuffer * aPacket); PacketBuffer * ChainedBuffer() const { return static_cast(this->next); } - PacketBuffer * Consume(uint16_t aConsumeLength); + PacketBuffer * Consume(uint32_t aConsumeLength); void Clear(); - void SetDataLength(uint16_t aNewLen, PacketBuffer * aChainHead); + void SetDataLength(uint32_t aNewLen, PacketBuffer * aChainHead); /** * Get a pointer to the start of the reserved space (which comes before the @@ -542,7 +542,7 @@ class DLL_EXPORT PacketBufferHandle * * @param[in] aConsumeLength - number of bytes to consume from the current chain. */ - void Consume(uint16_t aConsumeLength) { mBuffer = mBuffer->Consume(aConsumeLength); } + void Consume(uint32_t aConsumeLength) { mBuffer = mBuffer->Consume(aConsumeLength); } /** * Copy the given buffer to a right-sized buffer if applicable. @@ -630,7 +630,7 @@ class DLL_EXPORT PacketBufferHandle * * @return On success, a PacketBufferHandle to the allocated buffer. On fail, \c nullptr. */ - static PacketBufferHandle NewWithData(const void * aData, size_t aDataSize, uint16_t aAdditionalSize = 0, + static PacketBufferHandle NewWithData(const void * aData, size_t aDataSize, uint32_t aAdditionalSize = 0, uint16_t aReservedSize = PacketBuffer::kDefaultHeaderReserve); /** @@ -699,7 +699,7 @@ class DLL_EXPORT PacketBufferHandle friend class ::PacketBufferTest; }; -inline void PacketBuffer::SetDataLength(uint16_t aNewLen, const PacketBufferHandle & aChainHead) +inline void PacketBuffer::SetDataLength(uint32_t aNewLen, const PacketBufferHandle & aChainHead) { SetDataLength(aNewLen, aChainHead.mBuffer); } diff --git a/src/system/tests/TestSystemPacketBuffer.cpp b/src/system/tests/TestSystemPacketBuffer.cpp index 9bf2597d7a92b5..f3475a55537306 100644 --- a/src/system/tests/TestSystemPacketBuffer.cpp +++ b/src/system/tests/TestSystemPacketBuffer.cpp @@ -70,9 +70,9 @@ using ::chip::System::pbuf; #define OF_LWIP_PBUF(x) (reinterpret_cast(reinterpret_cast(x))) namespace { -void ScrambleData(uint8_t * start, uint16_t length) +void ScrambleData(uint8_t * start, uint32_t length) { - for (uint16_t i = 0; i < length; ++i) + for (uint32_t i = 0; i < length; ++i) ++start[i]; } } // namespace @@ -584,8 +584,8 @@ void PacketBufferTest::CheckSetDataLength(nlTestSuite * inSuite, void * inContex NL_TEST_ASSERT(inSuite, config_1.handle->tot_len == - (config_1.init_len + static_cast(config_2.end_buffer - config_2.payload_ptr) - - static_cast(config_2.init_len))); + (config_1.init_len + static_cast(config_2.end_buffer - config_2.payload_ptr) - + static_cast(config_2.init_len))); } else { @@ -596,7 +596,7 @@ void PacketBufferTest::CheckSetDataLength(nlTestSuite * inSuite, void * inContex NL_TEST_ASSERT( inSuite, config_1.handle->tot_len == - (config_1.init_len + static_cast(length) - static_cast(config_2.init_len))); + (config_1.init_len + static_cast(length) - static_cast(config_2.init_len))); } } } @@ -764,7 +764,9 @@ void PacketBufferTest::CheckAddToEnd(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, config_2.handle->ref == 2); // config_2.handle and config_1.handle->next NL_TEST_ASSERT(inSuite, config_3.handle->ref == 2); // config_3.handle and config_2.handle->next - NL_TEST_ASSERT(inSuite, config_1.handle->tot_len == (config_1.init_len + config_2.init_len + config_3.init_len)); + NL_TEST_ASSERT(inSuite, + config_1.handle->tot_len == + static_cast(config_1.init_len + config_2.init_len + config_3.init_len)); NL_TEST_ASSERT(inSuite, config_1.handle->next == config_2.handle.Get()); NL_TEST_ASSERT(inSuite, config_2.handle->next == config_3.handle.Get()); NL_TEST_ASSERT(inSuite, config_3.handle->next == nullptr); @@ -857,11 +859,11 @@ void PacketBufferTest::CheckCompactHead(nlTestSuite * inSuite, void * inContext) { for (size_t i = 0; i < theContext->length_count; ++i) { - const uint16_t length = theContext->lengths[i]; + const uint32_t length = theContext->lengths[i]; test->PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); config.handle->SetDataLength(length, config.handle); - const uint16_t data_length = config.handle->DataLength(); + const uint32_t data_length = config.handle->DataLength(); config.handle->CompactHead(); @@ -886,12 +888,12 @@ void PacketBufferTest::CheckCompactHead(nlTestSuite * inSuite, void * inContext) // start with various initial length for the first buffer for (size_t i = 0; i < theContext->length_count; ++i) { - const uint16_t length_1 = theContext->lengths[i]; + const uint32_t length_1 = theContext->lengths[i]; // start with various initial length for the second buffer for (size_t j = 0; j < theContext->length_count; ++j) { - const uint16_t length_2 = theContext->lengths[j]; + const uint32_t length_2 = theContext->lengths[j]; test->PrepareTestBuffer(&config_1, kRecordHandle | kAllowHandleReuse); NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); @@ -904,14 +906,14 @@ void PacketBufferTest::CheckCompactHead(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, config_2.handle.IsNull()); config_1.handle->SetDataLength(length_1, config_1.handle); - const uint16_t data_length_1 = config_1.handle->DataLength(); + const uint32_t data_length_1 = config_1.handle->DataLength(); // This chain will cause buffer_2 to be freed. config_1.handle->next = buffer_2; // Add various lengths to the second buffer buffer_2->SetDataLength(length_2, config_1.handle); - const uint16_t data_length_2 = buffer_2->DataLength(); + const uint32_t data_length_2 = buffer_2->DataLength(); config_1.handle->CompactHead(); @@ -1045,8 +1047,8 @@ void PacketBufferTest::CheckConsume(nlTestSuite * inSuite, void * inContext) config_1.handle->SetDataLength(len_1, config_1.handle); config_2.handle->SetDataLength(len_2, config_1.handle); - const uint16_t buf_1_len = config_1.handle->len; - const uint16_t buf_2_len = config_2.handle->len; + const uint32_t buf_1_len = config_1.handle->len; + const uint32_t buf_2_len = config_2.handle->len; PacketBufferHandle original_handle_1 = config_1.handle.Retain(); NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); // config_1.handle and original_handle_1 @@ -1117,7 +1119,7 @@ void PacketBufferTest::CheckEnsureReservedSize(nlTestSuite * inSuite, void * inC const uint16_t length = theContext->lengths[i]; test->PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); - const uint16_t kAllocSize = config.handle->AllocSize(); + const uint32_t kAllocSize = config.handle->AllocSize(); uint16_t reserved_size = config.reserved_size; if (PacketBuffer::kStructureSize + config.reserved_size > kAllocSize) @@ -1163,7 +1165,7 @@ void PacketBufferTest::CheckAlignPayload(nlTestSuite * inSuite, void * inContext for (size_t n = 0; n < theContext->length_count; ++n) { test->PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); - const uint16_t kAllocSize = config.handle->AllocSize(); + const uint32_t kAllocSize = config.handle->AllocSize(); if (theContext->lengths[n] == 0) { @@ -1171,7 +1173,7 @@ void PacketBufferTest::CheckAlignPayload(nlTestSuite * inSuite, void * inContext continue; } - uint16_t reserved_size = config.reserved_size; + uint32_t reserved_size = config.reserved_size; if (config.reserved_size > kAllocSize) { reserved_size = kAllocSize; @@ -1307,10 +1309,10 @@ void PacketBufferTest::CheckRead(nlTestSuite * inSuite, void * inContext) test->PrepareTestBuffer(&config_1, kAllowHandleReuse); test->PrepareTestBuffer(&config_2, kAllowHandleReuse); - const uint16_t length_1 = config_1.handle->MaxDataLength(); - const uint16_t length_2 = config_2.handle->MaxDataLength(); + const uint32_t length_1 = config_1.handle->MaxDataLength(); + const uint32_t length_2 = config_2.handle->MaxDataLength(); const size_t length_sum = length_1 + length_2; - const uint16_t length_total = static_cast(length_sum); + const uint32_t length_total = static_cast(length_sum); NL_TEST_ASSERT(inSuite, length_total == length_sum); memcpy(config_1.handle->Start(), payloads, length_1); diff --git a/src/transport/SecureMessageCodec.cpp b/src/transport/SecureMessageCodec.cpp index 9c08d535ff57a3..0d3e5aa765a738 100644 --- a/src/transport/SecureMessageCodec.cpp +++ b/src/transport/SecureMessageCodec.cpp @@ -43,13 +43,13 @@ CHIP_ERROR Encrypt(const CryptoContext & context, CryptoContext::ConstNonceView VerifyOrReturnError(!msgBuf->HasChainedBuffer(), CHIP_ERROR_INVALID_MESSAGE_LENGTH); VerifyOrReturnError(msgBuf->TotalLength() <= kMaxAppMessageLen, CHIP_ERROR_MESSAGE_TOO_LONG); - static_assert(std::is_sameTotalLength()), uint16_t>::value, + static_assert(std::is_sameTotalLength()), uint32_t>::value, "Addition to generate payloadLength might overflow"); ReturnErrorOnFailure(payloadHeader.EncodeBeforeData(msgBuf)); uint8_t * data = msgBuf->Start(); - uint16_t totalLen = msgBuf->TotalLength(); + uint32_t totalLen = msgBuf->TotalLength(); MessageAuthenticationCode mac; ReturnErrorOnFailure(context.Encrypt(data, totalLen, data, nonce, packetHeader, mac)); @@ -57,8 +57,8 @@ CHIP_ERROR Encrypt(const CryptoContext & context, CryptoContext::ConstNonceView uint16_t taglen = 0; ReturnErrorOnFailure(mac.Encode(packetHeader, &data[totalLen], msgBuf->AvailableDataLength(), &taglen)); - VerifyOrReturnError(CanCastTo(totalLen + taglen), CHIP_ERROR_INTERNAL); - msgBuf->SetDataLength(static_cast(totalLen + taglen)); + VerifyOrReturnError(CanCastTo(totalLen + taglen), CHIP_ERROR_INTERNAL); + msgBuf->SetDataLength(static_cast(totalLen + taglen)); return CHIP_NO_ERROR; } @@ -69,7 +69,7 @@ CHIP_ERROR Decrypt(const CryptoContext & context, CryptoContext::ConstNonceView ReturnErrorCodeIf(msg.IsNull(), CHIP_ERROR_INVALID_ARGUMENT); uint8_t * data = msg->Start(); - uint16_t len = msg->DataLength(); + uint32_t len = msg->DataLength(); PacketBufferHandle origMsg; #if CHIP_SYSTEM_CONFIG_USE_LWIP @@ -89,7 +89,7 @@ CHIP_ERROR Decrypt(const CryptoContext & context, CryptoContext::ConstNonceView ReturnErrorOnFailure(mac.Decode(packetHeader, &data[len - footerLen], footerLen, &taglen)); VerifyOrReturnError(taglen == footerLen, CHIP_ERROR_INTERNAL); - len = static_cast(len - taglen); + len = static_cast(len - taglen); msg->SetDataLength(len); uint8_t * plainText = msg->Start(); diff --git a/src/transport/SessionManager.cpp b/src/transport/SessionManager.cpp index 085fda948629c7..67ce762fd27692 100644 --- a/src/transport/SessionManager.cpp +++ b/src/transport/SessionManager.cpp @@ -877,7 +877,7 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack // Extract MIC from the end of the message. uint8_t * data = msg->Start(); - uint16_t len = msg->DataLength(); + uint32_t len = msg->DataLength(); uint16_t footerLen = partialPacketHeader.MICTagLength(); VerifyOrReturn(footerLen <= len); diff --git a/src/transport/raw/MessageHeader.cpp b/src/transport/raw/MessageHeader.cpp index 2947756b644d1a..041e32fddd9bd7 100644 --- a/src/transport/raw/MessageHeader.cpp +++ b/src/transport/raw/MessageHeader.cpp @@ -156,12 +156,12 @@ CHIP_ERROR PacketHeader::DecodeFixedCommon(Encoding::LittleEndian::Reader & read CHIP_ERROR PacketHeader::DecodeFixed(const System::PacketBufferHandle & buf) { const uint8_t * const data = buf->Start(); - uint16_t size = buf->DataLength(); + uint32_t size = buf->DataLength(); LittleEndian::Reader reader(data, size); return DecodeFixedCommon(reader); } -CHIP_ERROR PacketHeader::Decode(const uint8_t * const data, uint16_t size, uint16_t * decode_len) +CHIP_ERROR PacketHeader::Decode(const uint8_t * const data, uint32_t size, uint16_t * decode_len) { CHIP_ERROR err = CHIP_NO_ERROR; LittleEndian::Reader reader(data, size); @@ -247,7 +247,7 @@ CHIP_ERROR PacketHeader::DecodeAndConsume(const System::PacketBufferHandle & buf return CHIP_NO_ERROR; } -CHIP_ERROR PayloadHeader::Decode(const uint8_t * const data, uint16_t size, uint16_t * decode_len) +CHIP_ERROR PayloadHeader::Decode(const uint8_t * const data, uint32_t size, uint16_t * decode_len) { CHIP_ERROR err = CHIP_NO_ERROR; LittleEndian::Reader reader(data, size); @@ -312,7 +312,7 @@ CHIP_ERROR PayloadHeader::DecodeAndConsume(const System::PacketBufferHandle & bu return CHIP_NO_ERROR; } -CHIP_ERROR PacketHeader::Encode(uint8_t * data, uint16_t size, uint16_t * encode_size) const +CHIP_ERROR PacketHeader::Encode(uint8_t * data, uint32_t size, uint16_t * encode_size) const { VerifyOrReturnError(size >= EncodeSizeBytes(), CHIP_ERROR_INVALID_ARGUMENT); VerifyOrReturnError(!(mDestinationNodeId.HasValue() && mDestinationGroupId.HasValue()), CHIP_ERROR_INTERNAL); @@ -364,7 +364,7 @@ CHIP_ERROR PacketHeader::EncodeBeforeData(const System::PacketBufferHandle & buf return CHIP_NO_ERROR; } -CHIP_ERROR PayloadHeader::Encode(uint8_t * data, uint16_t size, uint16_t * encode_size) const +CHIP_ERROR PayloadHeader::Encode(uint8_t * data, uint32_t size, uint16_t * encode_size) const { VerifyOrReturnError(size >= EncodeSizeBytes(), CHIP_ERROR_INVALID_ARGUMENT); @@ -404,7 +404,7 @@ CHIP_ERROR PayloadHeader::EncodeBeforeData(const System::PacketBufferHandle & bu return CHIP_NO_ERROR; } -CHIP_ERROR MessageAuthenticationCode::Decode(const PacketHeader & packetHeader, const uint8_t * const data, uint16_t size, +CHIP_ERROR MessageAuthenticationCode::Decode(const PacketHeader & packetHeader, const uint8_t * const data, uint32_t size, uint16_t * decode_len) { const uint16_t taglen = packetHeader.MICTagLength(); @@ -419,7 +419,7 @@ CHIP_ERROR MessageAuthenticationCode::Decode(const PacketHeader & packetHeader, return CHIP_NO_ERROR; } -CHIP_ERROR MessageAuthenticationCode::Encode(const PacketHeader & packetHeader, uint8_t * data, uint16_t size, +CHIP_ERROR MessageAuthenticationCode::Encode(const PacketHeader & packetHeader, uint8_t * data, uint32_t size, uint16_t * encode_size) const { uint8_t * p = data; diff --git a/src/transport/raw/MessageHeader.h b/src/transport/raw/MessageHeader.h index 7c637fa782d957..bad7e63c72fbda 100644 --- a/src/transport/raw/MessageHeader.h +++ b/src/transport/raw/MessageHeader.h @@ -418,7 +418,7 @@ class PacketHeader * CHIP_ERROR_INVALID_ARGUMENT on insufficient buffer size * CHIP_ERROR_VERSION_MISMATCH if header version is not supported. */ - CHIP_ERROR Decode(const uint8_t * data, uint16_t size, uint16_t * decode_size); + CHIP_ERROR Decode(const uint8_t * data, uint32_t size, uint16_t * decode_size); /** * A version of Decode that uses the type system to determine available @@ -448,7 +448,7 @@ class PacketHeader * Possible failures: * CHIP_ERROR_INVALID_ARGUMENT on insufficient buffer size */ - CHIP_ERROR Encode(uint8_t * data, uint16_t size, uint16_t * encode_size) const; + CHIP_ERROR Encode(uint8_t * data, uint32_t size, uint16_t * encode_size) const; /** * A version of Encode that uses the type system to determine available @@ -662,7 +662,7 @@ class PayloadHeader * CHIP_ERROR_INVALID_ARGUMENT on insufficient buffer size * CHIP_ERROR_VERSION_MISMATCH if header version is not supported. */ - CHIP_ERROR Decode(const uint8_t * data, uint16_t size, uint16_t * decode_size); + CHIP_ERROR Decode(const uint8_t * data, uint32_t size, uint16_t * decode_size); /** * A version of Decode that uses the type system to determine available @@ -692,7 +692,7 @@ class PayloadHeader * Possible failures: * CHIP_ERROR_INVALID_ARGUMENT on insufficient buffer size */ - CHIP_ERROR Encode(uint8_t * data, uint16_t size, uint16_t * encode_size) const; + CHIP_ERROR Encode(uint8_t * data, uint32_t size, uint16_t * encode_size) const; /** * A version of Encode that uses the type system to determine available @@ -779,7 +779,7 @@ class MessageAuthenticationCode * CHIP_ERROR_INVALID_ARGUMENT on insufficient buffer size * CHIP_ERROR_VERSION_MISMATCH if header version is not supported. */ - CHIP_ERROR Decode(const PacketHeader & packetHeader, const uint8_t * data, uint16_t size, uint16_t * decode_size); + CHIP_ERROR Decode(const PacketHeader & packetHeader, const uint8_t * data, uint32_t size, uint16_t * decode_size); /** * Encodes the Messae Authentication Tag into the given buffer. @@ -794,7 +794,7 @@ class MessageAuthenticationCode * Possible failures: * CHIP_ERROR_INVALID_ARGUMENT on insufficient buffer size */ - CHIP_ERROR Encode(const PacketHeader & packetHeader, uint8_t * data, uint16_t size, uint16_t * encode_size) const; + CHIP_ERROR Encode(const PacketHeader & packetHeader, uint8_t * data, uint32_t size, uint16_t * encode_size) const; private: /// Message authentication tag generated at encryption of the message. diff --git a/src/transport/raw/TCP.cpp b/src/transport/raw/TCP.cpp index a1b1df1fe45a2d..306b8e056e5699 100644 --- a/src/transport/raw/TCP.cpp +++ b/src/transport/raw/TCP.cpp @@ -39,11 +39,11 @@ namespace { using namespace chip::Encoding; -// Packets start with a 16-bit size -constexpr size_t kPacketSizeBytes = 2; +// Packets start with a 32-bit size +constexpr size_t kPacketSizeBytes = 4; // TODO: Actual limit may be lower (spec issue #2119) -constexpr uint16_t kMaxMessageSize = static_cast(System::PacketBuffer::kMaxSizeWithoutReserve - kPacketSizeBytes); +constexpr uint32_t kMaxMessageSize = static_cast(System::PacketBuffer::kMaxSizeWithoutReserve - kPacketSizeBytes); constexpr int kListenBacklogSize = 2; @@ -173,16 +173,16 @@ CHIP_ERROR TCPBase::SendMessage(const Transport::PeerAddress & address, System:: VerifyOrReturnError(address.GetTransportType() == Type::kTcp, CHIP_ERROR_INVALID_ARGUMENT); VerifyOrReturnError(mState == State::kInitialized, CHIP_ERROR_INCORRECT_STATE); - VerifyOrReturnError(kPacketSizeBytes + msgBuf->DataLength() <= std::numeric_limits::max(), + VerifyOrReturnError(kPacketSizeBytes + msgBuf->DataLength() <= std::numeric_limits::max(), CHIP_ERROR_INVALID_ARGUMENT); - // The check above about kPacketSizeBytes + msgBuf->DataLength() means it definitely fits in uint16_t. + // The check above about kPacketSizeBytes + msgBuf->DataLength() means it definitely fits in uint32_t. VerifyOrReturnError(msgBuf->EnsureReservedSize(static_cast(kPacketSizeBytes)), CHIP_ERROR_NO_MEMORY); msgBuf->SetStart(msgBuf->Start() - kPacketSizeBytes); uint8_t * output = msgBuf->Start(); - LittleEndian::Write16(output, static_cast(msgBuf->DataLength() - kPacketSizeBytes)); + LittleEndian::Write32(output, static_cast(msgBuf->DataLength() - kPacketSizeBytes)); // Reuse existing connection if one exists, otherwise a new one // will be established @@ -272,8 +272,10 @@ CHIP_ERROR TCPBase::ProcessReceivedBuffer(Inet::TCPEndPoint * endPoint, const Pe { return err; } - uint16_t messageSize = LittleEndian::Get16(messageSizeBuf); - if (messageSize >= kMaxMessageSize) + uint32_t messageSize = LittleEndian::Get32(messageSizeBuf); + // TODO: Needs to change to the right max size limit for TCP + // based on a configured definition. + if (messageSize > kMaxMessageSize) { // This message is too long for upper layers. return CHIP_ERROR_MESSAGE_TOO_LONG; @@ -291,7 +293,7 @@ CHIP_ERROR TCPBase::ProcessReceivedBuffer(Inet::TCPEndPoint * endPoint, const Pe return CHIP_NO_ERROR; } -CHIP_ERROR TCPBase::ProcessSingleMessage(const PeerAddress & peerAddress, ActiveConnectionState * state, uint16_t messageSize) +CHIP_ERROR TCPBase::ProcessSingleMessage(const PeerAddress & peerAddress, ActiveConnectionState * state, uint32_t messageSize) { // We enter with `state->mReceived` containing at least one full message, perhaps in a chain. // `state->mReceived->Start()` currently points to the message data. diff --git a/src/transport/raw/TCP.h b/src/transport/raw/TCP.h index d9f78be1771b0f..d30acbf82d566d 100644 --- a/src/transport/raw/TCP.h +++ b/src/transport/raw/TCP.h @@ -223,7 +223,7 @@ class DLL_EXPORT TCPBase : public Base * is no other data). * @param[in] messageSize Size of the single message. */ - CHIP_ERROR ProcessSingleMessage(const PeerAddress & peerAddress, ActiveConnectionState * state, uint16_t messageSize); + CHIP_ERROR ProcessSingleMessage(const PeerAddress & peerAddress, ActiveConnectionState * state, uint32_t messageSize); // Release an active connection (corresponding to the passed TCPEndPoint) // from the pool. diff --git a/src/transport/raw/tests/TestMessageHeader.cpp b/src/transport/raw/tests/TestMessageHeader.cpp index 93b1ef65ad6389..7e45e668bdc4fb 100644 --- a/src/transport/raw/tests/TestMessageHeader.cpp +++ b/src/transport/raw/tests/TestMessageHeader.cpp @@ -224,7 +224,7 @@ void TestPacketHeaderEncodeDecodeBounds(nlTestSuite * inSuite, void * inContext) uint8_t buffer[64] = {}; uint16_t unusedLen = 0; - for (uint16_t shortLen = 0; shortLen < 8; shortLen++) + for (uint32_t shortLen = 0; shortLen < 8; shortLen++) { NL_TEST_ASSERT(inSuite, header.Encode(buffer, shortLen, &unusedLen) != CHIP_NO_ERROR); NL_TEST_ASSERT(inSuite, header.Decode(buffer, shortLen, &unusedLen) != CHIP_NO_ERROR); @@ -279,13 +279,13 @@ void TestPacketHeaderEncodeDecodeBounds(nlTestSuite * inSuite, void * inContext) header.ClearDestinationNodeId(); header.SetDestinationGroupId(25); header.SetSessionType(Header::SessionType::kGroupSession); - for (uint16_t shortLen = minLen; shortLen < minLen + 10; shortLen++) + for (uint32_t shortLen = minLen; shortLen < minLen + 10; shortLen++) { NL_TEST_ASSERT(inSuite, header.Encode(buffer, shortLen, &unusedLen) != CHIP_NO_ERROR); } NL_TEST_ASSERT(inSuite, header.Encode(buffer, minLen + 10, &encoded_len) == CHIP_NO_ERROR); NL_TEST_ASSERT(inSuite, encoded_len == minLen + 10); - for (uint16_t shortLen = 0; shortLen < encoded_len; shortLen++) + for (uint32_t shortLen = 0; shortLen < encoded_len; shortLen++) { NL_TEST_ASSERT(inSuite, header.Decode(buffer, shortLen, &unusedLen) != CHIP_NO_ERROR); } diff --git a/src/transport/raw/tests/TestTCP.cpp b/src/transport/raw/tests/TestTCP.cpp index 62b03f4e3feb03..105b342e709a52 100644 --- a/src/transport/raw/tests/TestTCP.cpp +++ b/src/transport/raw/tests/TestTCP.cpp @@ -62,7 +62,7 @@ namespace { constexpr size_t kMaxTcpActiveConnectionCount = 4; constexpr size_t kMaxTcpPendingPackets = 4; -constexpr uint16_t kPacketSizeBytes = static_cast(sizeof(uint16_t)); +constexpr uint16_t kPacketSizeBytes = static_cast(sizeof(uint32_t)); using TCPImpl = Transport::TCP; @@ -240,7 +240,7 @@ struct TestData // the last buffer will be made larger. TestData() : mPayload(nullptr), mTotalLength(0), mMessageLength(0), mMessageOffset(0) {} ~TestData() { Free(); } - bool Init(const uint16_t sizes[]); + bool Init(const uint32_t sizes[]); void Free(); bool IsValid() { return !mHandle.IsNull() && (mPayload != nullptr); } @@ -251,7 +251,7 @@ struct TestData size_t mMessageOffset; }; -bool TestData::Init(const uint16_t sizes[]) +bool TestData::Init(const uint32_t sizes[]) { Free(); @@ -267,17 +267,17 @@ bool TestData::Init(const uint16_t sizes[]) mTotalLength += sizes[bufferCount]; } --bufferCount; - uint16_t additionalLength = 0; + uint32_t additionalLength = 0; if (headerLength + kPacketSizeBytes > mTotalLength) { - additionalLength = static_cast((headerLength + kPacketSizeBytes) - mTotalLength); + additionalLength = static_cast((headerLength + kPacketSizeBytes) - mTotalLength); mTotalLength += additionalLength; } - if (mTotalLength > UINT16_MAX) + if (mTotalLength > UINT32_MAX) { return false; } - uint16_t messageLength = static_cast(mTotalLength - kPacketSizeBytes); + uint32_t messageLength = static_cast(mTotalLength - kPacketSizeBytes); // Build the test payload. uint8_t * payload = static_cast(chip::Platform::MemoryCalloc(1, mTotalLength)); @@ -285,7 +285,7 @@ bool TestData::Init(const uint16_t sizes[]) { return false; } - chip::Encoding::LittleEndian::Put16(payload, messageLength); + chip::Encoding::LittleEndian::Put32(payload, messageLength); uint16_t headerSize; CHIP_ERROR err = header.Encode(payload + kPacketSizeBytes, messageLength, &headerSize); if (err != CHIP_NO_ERROR) @@ -305,10 +305,10 @@ bool TestData::Init(const uint16_t sizes[]) System::PacketBufferHandle head = chip::System::PacketBufferHandle::New(sizes[0], 0 /* reserve */); for (int i = 1; i <= bufferCount; ++i) { - uint16_t size = sizes[i]; + uint32_t size = sizes[i]; if (i == bufferCount) { - size = static_cast(size + additionalLength); + size = static_cast(size + additionalLength); } chip::System::PacketBufferHandle buffer = chip::System::PacketBufferHandle::New(size, 0 /* reserve */); if (buffer.IsNull()) @@ -337,7 +337,7 @@ bool TestData::Init(const uint16_t sizes[]) if (lToWriteToCurrentBuf != 0) { memcpy(iterator->Start(), writePayload, lToWriteToCurrentBuf); - iterator->SetDataLength(static_cast(iterator->DataLength() + lToWriteToCurrentBuf), head); + iterator->SetDataLength(static_cast(iterator->DataLength() + lToWriteToCurrentBuf), head); writePayload += lToWriteToCurrentBuf; writeLength -= lToWriteToCurrentBuf; } @@ -410,22 +410,22 @@ void chip::Transport::TCPTest::CheckProcessReceivedBuffer(nlTestSuite * inSuite, // Test a single packet buffer. gMockTransportMgrDelegate.mReceiveHandlerCallCount = 0; - NL_TEST_ASSERT(inSuite, testData[0].Init((const uint16_t[]){ 111, 0 })); + NL_TEST_ASSERT(inSuite, testData[0].Init((const uint32_t[]){ 111, 0 })); err = tcp.ProcessReceivedBuffer(lEndPoint, lPeerAddress, std::move(testData[0].mHandle)); NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); NL_TEST_ASSERT(inSuite, gMockTransportMgrDelegate.mReceiveHandlerCallCount == 1); // Test a message in a chain of three packet buffers. The message length is split across buffers. gMockTransportMgrDelegate.mReceiveHandlerCallCount = 0; - NL_TEST_ASSERT(inSuite, testData[0].Init((const uint16_t[]){ 1, 122, 123, 0 })); + NL_TEST_ASSERT(inSuite, testData[0].Init((const uint32_t[]){ 1, 122, 123, 0 })); err = tcp.ProcessReceivedBuffer(lEndPoint, lPeerAddress, std::move(testData[0].mHandle)); NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); NL_TEST_ASSERT(inSuite, gMockTransportMgrDelegate.mReceiveHandlerCallCount == 1); // Test two messages in a chain. gMockTransportMgrDelegate.mReceiveHandlerCallCount = 0; - NL_TEST_ASSERT(inSuite, testData[0].Init((const uint16_t[]){ 131, 0 })); - NL_TEST_ASSERT(inSuite, testData[1].Init((const uint16_t[]){ 132, 0 })); + NL_TEST_ASSERT(inSuite, testData[0].Init((const uint32_t[]){ 131, 0 })); + NL_TEST_ASSERT(inSuite, testData[1].Init((const uint32_t[]){ 132, 0 })); testData[0].mHandle->AddToEnd(std::move(testData[1].mHandle)); err = tcp.ProcessReceivedBuffer(lEndPoint, lPeerAddress, std::move(testData[0].mHandle)); NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); @@ -433,8 +433,8 @@ void chip::Transport::TCPTest::CheckProcessReceivedBuffer(nlTestSuite * inSuite, // Test a chain of two messages, each a chain. gMockTransportMgrDelegate.mReceiveHandlerCallCount = 0; - NL_TEST_ASSERT(inSuite, testData[0].Init((const uint16_t[]){ 141, 142, 0 })); - NL_TEST_ASSERT(inSuite, testData[1].Init((const uint16_t[]){ 143, 144, 0 })); + NL_TEST_ASSERT(inSuite, testData[0].Init((const uint32_t[]){ 141, 142, 0 })); + NL_TEST_ASSERT(inSuite, testData[1].Init((const uint32_t[]){ 143, 144, 0 })); testData[0].mHandle->AddToEnd(std::move(testData[1].mHandle)); err = tcp.ProcessReceivedBuffer(lEndPoint, lPeerAddress, std::move(testData[0].mHandle)); NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); @@ -443,7 +443,7 @@ void chip::Transport::TCPTest::CheckProcessReceivedBuffer(nlTestSuite * inSuite, // Test a message that is too large to coalesce into a single packet buffer. gMockTransportMgrDelegate.mReceiveHandlerCallCount = 0; gMockTransportMgrDelegate.SetCallback(TestDataCallbackCheck, &testData[1]); - NL_TEST_ASSERT(inSuite, testData[0].Init((const uint16_t[]){ 51, System::PacketBuffer::kMaxSizeWithoutReserve, 0 })); + NL_TEST_ASSERT(inSuite, testData[0].Init((const uint32_t[]){ 51, System::PacketBuffer::kMaxSizeWithoutReserve, 0 })); // Sending only the first buffer of the long chain. This should be enough to trigger the error. System::PacketBufferHandle head = testData[0].mHandle.PopHead(); err = tcp.ProcessReceivedBuffer(lEndPoint, lPeerAddress, std::move(head));