Skip to content

Commit 81be848

Browse files
committed
Adjust storage and processing in SystemPacketBuffer (project-chip#31776)
and associated code for large payloads. Extend uint16_t type variables to size_t for the APIs and all applicable places.
1 parent a28965b commit 81be848

35 files changed

+252
-196
lines changed

src/app/BufferedReadCallback.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ CHIP_ERROR BufferedReadCallback::GenerateListTLV(TLV::ScopedBufferTLVReader & aR
6565
//
6666
// To avoid that, a single contiguous buffer is the best likely approach for now.
6767
//
68-
uint32_t totalBufSize = 0;
68+
size_t totalBufSize = 0;
6969
for (const auto & packetBuffer : mBufferedList)
7070
{
7171
totalBufSize += packetBuffer->TotalLength();

src/app/server/EchoHandler.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ chip::Protocols::Echo::EchoServer gEchoServer;
4040
*/
4141
void HandleEchoRequestReceived(chip::Messaging::ExchangeContext * ec, chip::System::PacketBufferHandle && payload)
4242
{
43-
ChipLogProgress(AppServer, "Echo Request, len=%u ... sending response.\n", payload->DataLength());
43+
ChipLogProgress(AppServer, "Echo Request, len=%" PRIu32 "... sending response.\n",
44+
static_cast<uint32_t>(payload->DataLength()));
4445
}
4546

4647
} // namespace

src/ble/BtpEngine.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ CHIP_ERROR BtpEngine::HandleCharacteristicReceived(System::PacketBufferHandle &&
282282
// mRxFragmentSize may be smaller than the characteristic size. Make sure
283283
// we're not truncating to a data length smaller than what we have already consumed.
284284
VerifyOrExit(reader.OctetsRead() <= mRxFragmentSize, err = BLE_ERROR_REASSEMBLER_INCORRECT_STATE);
285-
data->SetDataLength(chip::min(data->DataLength(), mRxFragmentSize));
285+
data->SetDataLength(chip::min(data->DataLength(), static_cast<size_t>(mRxFragmentSize)));
286286

287287
// Now mark the bytes we consumed as consumed.
288288
data->ConsumeHead(static_cast<uint16_t>(reader.OctetsRead()));
@@ -346,11 +346,11 @@ CHIP_ERROR BtpEngine::HandleCharacteristicReceived(System::PacketBufferHandle &&
346346
if (rx_flags.Has(HeaderFlags::kEndMessage))
347347
{
348348
// Trim remainder, if any, of the received packet buffer based on sender-specified length of reassembled message.
349-
int padding = mRxBuf->DataLength() - mRxLength;
349+
int padding = static_cast<uint16_t>(mRxBuf->DataLength()) - mRxLength;
350350

351351
if (padding > 0)
352352
{
353-
mRxBuf->SetDataLength(mRxLength);
353+
mRxBuf->SetDataLength(static_cast<size_t>(mRxLength));
354354
}
355355

356356
// Ensure all received fragments add up to sender-specified total message size.
@@ -375,7 +375,7 @@ CHIP_ERROR BtpEngine::HandleCharacteristicReceived(System::PacketBufferHandle &&
375375
}
376376
if (!mRxBuf.IsNull())
377377
{
378-
ChipLogError(Ble, "With rx buf data length = %u", mRxBuf->DataLength());
378+
ChipLogError(Ble, "With rx buf data length = %u", static_cast<unsigned>(mRxBuf->DataLength()));
379379
}
380380
LogState();
381381

@@ -428,7 +428,7 @@ bool BtpEngine::HandleCharacteristicSend(System::PacketBufferHandle data, bool s
428428

429429
mTxBuf = std::move(data);
430430
mTxState = kState_InProgress;
431-
mTxLength = mTxBuf->DataLength();
431+
mTxLength = static_cast<uint16_t>(mTxBuf->DataLength());
432432

433433
ChipLogDebugBtpEngine(Ble, ">>> CHIPoBle preparing to send whole message:");
434434
PrintBufDebug(mTxBuf);

src/inet/TCPEndPoint.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ CHIP_ERROR TCPEndPoint::SetReceivedDataForTesting(System::PacketBufferHandle &&
126126
return CHIP_NO_ERROR;
127127
}
128128

129-
uint32_t TCPEndPoint::PendingSendLength()
129+
size_t TCPEndPoint::PendingSendLength()
130130
{
131131
if (!mSendQueue.IsNull())
132132
{
@@ -135,7 +135,7 @@ uint32_t TCPEndPoint::PendingSendLength()
135135
return 0;
136136
}
137137

138-
uint32_t TCPEndPoint::PendingReceiveLength()
138+
size_t TCPEndPoint::PendingReceiveLength()
139139
{
140140
if (!mRcvQueue.IsNull())
141141
{
@@ -333,8 +333,8 @@ void TCPEndPoint::DriveReceiving()
333333
{
334334
// Acknowledgement is done after handling the buffers to allow the
335335
// application processing to throttle flow.
336-
uint16_t ackLength = mRcvQueue->TotalLength();
337-
CHIP_ERROR err = OnDataReceived(this, std::move(mRcvQueue));
336+
size_t ackLength = mRcvQueue->TotalLength();
337+
CHIP_ERROR err = OnDataReceived(this, std::move(mRcvQueue));
338338
if (err != CHIP_NO_ERROR)
339339
{
340340
DoClose(err, false);

src/inet/TCPEndPoint.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis<TCPEndPoint>
274274
* received. The operational semantics are undefined if \c len is larger
275275
* than the total outstanding unacknowledged received data.
276276
*/
277-
virtual CHIP_ERROR AckReceive(uint16_t len) = 0;
277+
virtual CHIP_ERROR AckReceive(size_t len) = 0;
278278

279279
/**
280280
* @brief Set the receive queue, for testing.
@@ -295,15 +295,15 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis<TCPEndPoint>
295295
*
296296
* @return Number of untransmitted bytes in the transmit queue.
297297
*/
298-
uint32_t PendingSendLength();
298+
size_t PendingSendLength();
299299

300300
/**
301301
* @brief Extract the length of the unacknowledged receive data.
302302
*
303303
* @return Number of bytes in the receive queue that have not yet been
304304
* acknowledged with <tt>AckReceive(uint16_t len)</tt>.
305305
*/
306-
uint32_t PendingReceiveLength();
306+
size_t PendingReceiveLength();
307307

308308
/**
309309
* @brief Initiate TCP half close, in other words, finished with sending.
@@ -447,7 +447,7 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis<TCPEndPoint>
447447
* is the length of the message text added to the TCP transmit window,
448448
* which are eligible for sending by the underlying network stack.
449449
*/
450-
typedef void (*OnDataSentFunct)(TCPEndPoint * endPoint, uint16_t len);
450+
typedef void (*OnDataSentFunct)(TCPEndPoint * endPoint, size_t len);
451451

452452
/**
453453
* The endpoint's message text transmission event handling function

src/inet/TCPEndPointImplLwIP.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ CHIP_ERROR TCPEndPointImplLwIP::DriveSendingImpl()
351351
{
352352
VerifyOrDie(!startOfUnsent.buffer.IsNull());
353353

354-
uint16_t bufDataLen = startOfUnsent.buffer->DataLength();
354+
uint16_t bufDataLen = static_cast<uint16_t>(startOfUnsent.buffer->DataLength());
355355

356356
// Get a pointer to the start of unsent data within the first buffer on the unsent queue.
357357
const uint8_t * sendData = startOfUnsent.buffer->Start() + startOfUnsent.offset;
@@ -503,16 +503,18 @@ void TCPEndPointImplLwIP::DoCloseImpl(CHIP_ERROR err, State oldState)
503503
}
504504
}
505505

506-
CHIP_ERROR TCPEndPointImplLwIP::AckReceive(uint16_t len)
506+
CHIP_ERROR TCPEndPointImplLwIP::AckReceive(size_t len)
507507
{
508508
VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE);
509509
CHIP_ERROR res = CHIP_NO_ERROR;
510510

511+
VerifyOrReturnError(len < UINT16_MAX, CHIP_ERROR_INVALID_ARGUMENT);
512+
511513
// Lock LwIP stack
512514
LOCK_TCPIP_CORE();
513515

514516
if (mTCP != nullptr)
515-
tcp_recved(mTCP, len);
517+
tcp_recved(mTCP, static_cast<uint16_t>(len));
516518
else
517519
res = CHIP_ERROR_CONNECTION_ABORTED;
518520

@@ -570,7 +572,7 @@ TCPEndPointImplLwIP::BufferOffset TCPEndPointImplLwIP::FindStartOfUnsent()
570572
while (leftToSkip > 0)
571573
{
572574
VerifyOrDie(!startOfUnsent.buffer.IsNull());
573-
uint16_t bufDataLen = startOfUnsent.buffer->DataLength();
575+
uint16_t bufDataLen = static_cast<uint16_t>(startOfUnsent.buffer->DataLength());
574576
if (leftToSkip >= bufDataLen)
575577
{
576578
// We have more to skip than current packet buffer size.

src/inet/TCPEndPointImplLwIP.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class TCPEndPointImplLwIP : public TCPEndPoint, public EndPointStateLwIP
5151
CHIP_ERROR EnableNoDelay() override;
5252
CHIP_ERROR EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) override;
5353
CHIP_ERROR DisableKeepAlive() override;
54-
CHIP_ERROR AckReceive(uint16_t len) override;
54+
CHIP_ERROR AckReceive(size_t len) override;
5555
#if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
5656
void TCPUserTimeoutHandler() override;
5757
#endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT

src/inet/TCPEndPointImplOpenThread.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ CHIP_ERROR TCPEndPointImplOT::DisableKeepAlive()
5454
{
5555
return CHIP_ERROR_NOT_IMPLEMENTED;
5656
}
57-
CHIP_ERROR TCPEndPointImplOT::AckReceive(uint16_t len)
57+
CHIP_ERROR TCPEndPointImplOT::AckReceive(size_t len)
5858
{
5959
return CHIP_ERROR_NOT_IMPLEMENTED;
6060
}

src/inet/TCPEndPointImplOpenThread.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class TCPEndPointImplOT : public TCPEndPoint, public EndPointStateOpenThread
4646
CHIP_ERROR EnableNoDelay() override;
4747
CHIP_ERROR EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) override;
4848
CHIP_ERROR DisableKeepAlive() override;
49-
CHIP_ERROR AckReceive(uint16_t len) override;
49+
CHIP_ERROR AckReceive(size_t len) override;
5050
#if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
5151
void TCPUserTimeoutHandler() override;
5252
#endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT

src/inet/TCPEndPointImplSockets.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ CHIP_ERROR TCPEndPointImplSockets::DisableKeepAlive()
441441
return CHIP_NO_ERROR;
442442
}
443443

444-
CHIP_ERROR TCPEndPointImplSockets::AckReceive(uint16_t len)
444+
CHIP_ERROR TCPEndPointImplSockets::AckReceive(size_t len)
445445
{
446446
VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE);
447447

@@ -483,7 +483,7 @@ CHIP_ERROR TCPEndPointImplSockets::DriveSendingImpl()
483483

484484
while (!mSendQueue.IsNull())
485485
{
486-
uint16_t bufLen = mSendQueue->DataLength();
486+
uint32_t bufLen = static_cast<uint32_t>(mSendQueue->DataLength());
487487

488488
ssize_t lenSentRaw = send(mSocket, mSendQueue->Start(), bufLen, sendFlags);
489489

@@ -496,14 +496,14 @@ CHIP_ERROR TCPEndPointImplSockets::DriveSendingImpl()
496496
break;
497497
}
498498

499-
if (lenSentRaw < 0 || lenSentRaw > bufLen)
499+
if (lenSentRaw < 0 || bufLen < static_cast<uint32_t>(lenSentRaw))
500500
{
501501
err = CHIP_ERROR_INCORRECT_STATE;
502502
break;
503503
}
504504

505-
// Cast is safe because bufLen is uint16_t.
506-
uint16_t lenSent = static_cast<uint16_t>(lenSentRaw);
505+
// Cast is safe because bufLen is uint32_t.
506+
uint32_t lenSent = static_cast<uint32_t>(lenSentRaw);
507507

508508
// Mark the connection as being active.
509509
MarkActive();

src/inet/TCPEndPointImplSockets.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class TCPEndPointImplSockets : public TCPEndPoint, public EndPointStateSockets
4646
CHIP_ERROR EnableNoDelay() override;
4747
CHIP_ERROR EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) override;
4848
CHIP_ERROR DisableKeepAlive() override;
49-
CHIP_ERROR AckReceive(uint16_t len) override;
49+
CHIP_ERROR AckReceive(size_t len) override;
5050
#if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
5151
void TCPUserTimeoutHandler() override;
5252
#endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
@@ -72,7 +72,7 @@ class TCPEndPointImplSockets : public TCPEndPoint, public EndPointStateSockets
7272

7373
#if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
7474
/// This counts the number of bytes written on the TCP socket since thelast probe into the TCP outqueue was made.
75-
uint32_t mBytesWrittenSinceLastProbe;
75+
size_t mBytesWrittenSinceLastProbe;
7676

7777
/// This is the measured size(in bytes) of the kernel TCP send queue at the end of the last user timeout window.
7878
uint32_t mLastTCPKernelSendQueueLen;

src/inet/UDPEndPointImplLwIP.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ void UDPEndPointImplLwIP::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb
352352
if (buf->HasChainedBuffer())
353353
{
354354
// Have to allocate a new big-enough buffer and copy.
355-
uint16_t messageSize = buf->TotalLength();
355+
size_t messageSize = buf->TotalLength();
356356
System::PacketBufferHandle copy = System::PacketBufferHandle::New(messageSize, 0);
357357
if (copy.IsNull() || buf->Read(copy->Start(), messageSize) != CHIP_NO_ERROR)
358358
{

src/inet/UDPEndPointImplOpenThread.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ CHIP_ERROR UDPEndPointImplOT::SendMsgImpl(const IPPacketInfo * aPktInfo, System:
237237
message = otUdpNewMessage(mOTInstance, NULL);
238238
VerifyOrExit(message != NULL, error = OT_ERROR_NO_BUFS);
239239

240-
error = otMessageAppend(message, msg->Start(), msg->DataLength());
240+
error = otMessageAppend(message, msg->Start(), static_cast<uint16_t>(msg->DataLength()));
241241

242242
if (error == OT_ERROR_NONE)
243243
{

src/inet/UDPEndPointImplSockets.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,10 @@ CHIP_ERROR UDPEndPointImplSockets::SendMsgImpl(const IPPacketInfo * aPktInfo, Sy
415415
{
416416
return CHIP_ERROR_POSIX(errno);
417417
}
418-
if (lenSent != msg->DataLength())
418+
419+
size_t len = static_cast<size_t>(lenSent);
420+
421+
if (len != msg->DataLength())
419422
{
420423
return CHIP_ERROR_OUTBOUND_MESSAGE_TOO_BIG;
421424
}
@@ -611,7 +614,7 @@ void UDPEndPointImplSockets::HandlePendingIO(System::SocketEvents events)
611614
{
612615
lStatus = CHIP_ERROR_POSIX(errno);
613616
}
614-
else if (rcvLen > lBuffer->AvailableDataLength())
617+
else if (lBuffer->AvailableDataLength() < static_cast<size_t>(rcvLen))
615618
{
616619
lStatus = CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG;
617620
}

src/inet/tests/TestInetLayer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ static void HandleTCPConnectionClosed(TCPEndPoint * aEndPoint, CHIP_ERROR aError
565565
}
566566
}
567567

568-
static void HandleTCPDataSent(TCPEndPoint * aEndPoint, uint16_t len) {}
568+
static void HandleTCPDataSent(TCPEndPoint * aEndPoint, size_t len) {}
569569

570570
static CHIP_ERROR HandleTCPDataReceived(TCPEndPoint * aEndPoint, PacketBufferHandle && aBuffer)
571571
{

0 commit comments

Comments
 (0)