@@ -515,6 +515,21 @@ PacketBufferHandle PacketBufferHandle::New(size_t aAvailableSize, uint16_t aRese
515
515
// Setting a static upper bound on the maximum buffer size allocation for regular sized messages (not large).
516
516
static_assert (PacketBuffer::kMaxSizeWithoutReserve <= UINT16_MAX, " kMaxSizeWithoutReserve should not exceed UINT16_MAX." );
517
517
518
+ #if INET_CONFIG_ENABLE_TCP_ENDPOINT
519
+ // Setting a static upper bound on the maximum buffer size allocation for
520
+ // large messages.
521
+ #if CHIP_SYSTEM_CONFIG_USE_LWIP
522
+ static_assert (PacketBuffer::kLargeBufMaxSizeWithoutReserve <= UINT16_MAX, " In LwIP, max size for Large payload buffers cannot exceed UINT16_MAX!" );
523
+ #else
524
+ static_assert (PacketBuffer::kLargeBufMaxSizeWithoutReserve <= UINT32_MAX, " Max size for Large payload buffers cannot exceed UINT32_MAX" );
525
+ #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
526
+
527
+ // Ensure that the definition of the max buffer allocation for regular
528
+ // messages is less than that for large ones.
529
+ static_assert (PacketBuffer::kMaxSizeWithoutReserve < PacketBuffer::kLargeBufMaxSizeWithoutReserve ,
530
+ " Large buffer configuration should be greater than the conventional buffer limit" );
531
+ #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
532
+
518
533
// Ensure that aAvailableSize is bound within a max and is not big enough to cause overflow during
519
534
// subsequent addition of all the sizes.
520
535
if (aAvailableSize > UINT32_MAX)
@@ -557,7 +572,7 @@ PacketBufferHandle PacketBufferHandle::New(size_t aAvailableSize, uint16_t aRese
557
572
558
573
CHIP_SYSTEM_FAULT_INJECT (FaultInjection::kFault_PacketBufferNew , return PacketBufferHandle ());
559
574
560
- if (lAllocSize > PacketBuffer::kMaxSizeWithoutReserve )
575
+ if (lAllocSize > PacketBuffer::kMaxAllocSize )
561
576
{
562
577
ChipLogError (chipSystemLayer, " PacketBuffer: allocation exceeding buffer capacity limits." );
563
578
return PacketBufferHandle ();
@@ -621,18 +636,15 @@ PacketBufferHandle PacketBufferHandle::New(size_t aAvailableSize, uint16_t aRese
621
636
PacketBufferHandle PacketBufferHandle::NewWithData (const void * aData, size_t aDataSize, size_t aAdditionalSize,
622
637
uint16_t aReservedSize)
623
638
{
624
- if (aDataSize > UINT16_MAX)
625
- {
626
- ChipLogError (chipSystemLayer, " PacketBuffer: allocation too large." );
627
- return PacketBufferHandle ();
628
- }
629
639
// Since `aDataSize` fits in uint16_t, the sum `aDataSize + aAdditionalSize` will not overflow.
630
640
// `New()` will only return a non-null buffer if the total allocation size does not overflow.
631
641
PacketBufferHandle buffer = New (aDataSize + aAdditionalSize, aReservedSize);
632
642
if (buffer.mBuffer != nullptr )
633
643
{
634
644
memcpy (buffer.mBuffer ->payload , aData, aDataSize);
635
645
#if CHIP_SYSTEM_CONFIG_USE_LWIP
646
+ // The VerifyOrDie() in the New() call catches buffer allocations greater
647
+ // than UINT16_MAX for LwIP based platforms.
636
648
buffer.mBuffer ->len = buffer.mBuffer ->tot_len = static_cast <uint16_t >(aDataSize);
637
649
#else
638
650
buffer.mBuffer ->len = buffer.mBuffer ->tot_len = aDataSize;
@@ -755,18 +767,20 @@ PacketBufferHandle PacketBufferHandle::CloneData() const
755
767
size_t originalDataSize = original->MaxDataLength ();
756
768
uint16_t originalReservedSize = original->ReservedSize ();
757
769
758
- if (originalDataSize + originalReservedSize > PacketBuffer::kMaxSizeWithoutReserve )
770
+ uint32_t maxSize = PacketBuffer::kMaxAllocSize ;
771
+
772
+ if (originalDataSize + originalReservedSize > maxSize)
759
773
{
760
774
// The original memory allocation may have provided a larger block than requested (e.g. when using a shared pool),
761
775
// and in particular may have provided a larger block than we are able to request from PackBufferHandle::New().
762
776
// It is a genuine error if that extra space has been used.
763
- if (originalReservedSize + original->DataLength () > PacketBuffer:: kMaxSizeWithoutReserve )
777
+ if (originalReservedSize + original->DataLength () > maxSize )
764
778
{
765
779
return PacketBufferHandle ();
766
780
}
767
781
// Otherwise, reduce the requested data size. This subtraction can not underflow because the above test
768
- // guarantees originalReservedSize <= PacketBuffer::kMaxSizeWithoutReserve .
769
- originalDataSize = PacketBuffer:: kMaxSizeWithoutReserve - originalReservedSize;
782
+ // guarantees originalReservedSize <= maxSize .
783
+ originalDataSize = maxSize - originalReservedSize;
770
784
}
771
785
772
786
PacketBufferHandle clone = PacketBufferHandle::New (originalDataSize, originalReservedSize);
0 commit comments