@@ -101,6 +101,7 @@ CHIP_ERROR TLVWriter::Init(TLVBackingStore & backingStore, uint32_t maxLen /* =
101
101
if (err != CHIP_NO_ERROR)
102
102
return err;
103
103
104
+ VerifyOrReturnError (mBufStart != nullptr , CHIP_ERROR_INTERNAL);
104
105
mWritePoint = mBufStart ;
105
106
mInitializationCookie = kExpectedInitializationCookie ;
106
107
return CHIP_NO_ERROR;
@@ -363,11 +364,7 @@ CHIP_ERROR TLVWriter::VPutStringF(Tag tag, const char * fmt, va_list ap)
363
364
size_t dataLen;
364
365
CHIP_ERROR err = CHIP_NO_ERROR;
365
366
TLVFieldSize lenFieldSize;
366
- #if CONFIG_HAVE_VSNPRINTF_EX
367
- size_t skipLen;
368
- size_t writtenBytes;
369
- #elif CONFIG_HAVE_VCBPRINTF
370
- #else
367
+ #if !CONFIG_HAVE_VCBPRINTF
371
368
char * tmpBuf;
372
369
#endif
373
370
va_copy (aq, ap);
@@ -396,47 +393,14 @@ CHIP_ERROR TLVWriter::VPutStringF(Tag tag, const char * fmt, va_list ap)
396
393
VerifyOrExit ((mLenWritten + dataLen) <= mMaxLen , err = CHIP_ERROR_BUFFER_TOO_SMALL);
397
394
398
395
// write data
399
- #if CONFIG_HAVE_VSNPRINTF_EX
400
-
401
- skipLen = 0 ;
402
-
403
- do
404
- {
405
- va_copy (aq, ap);
406
-
407
- vsnprintf_ex (reinterpret_cast <char *>(mWritePoint ), mRemainingLen , skipLen, fmt, aq);
408
-
409
- va_end (aq);
410
-
411
- writtenBytes = (mRemainingLen >= (dataLen - skipLen)) ? dataLen - skipLen : mRemainingLen ;
412
- skipLen += writtenBytes;
413
- mWritePoint += writtenBytes;
414
- mRemainingLen -= writtenBytes;
415
- mLenWritten += writtenBytes;
416
- if (skipLen < dataLen)
417
- {
418
- VerifyOrExit (mBackingStore != NULL , err = CHIP_ERROR_NO_MEMORY);
419
-
420
- err = mBackingStore ->FinalizeBuffer (*this , mBufHandle , mBufStart , mWritePoint - mBufStart );
421
- SuccessOrExit (err);
422
-
423
- err = mBackingStore ->GetNewBuffer (*this , mBufHandle , mBufStart , mRemainingLen );
424
- SuccessOrExit (err);
425
-
426
- mWritePoint = mBufStart ;
427
- }
428
-
429
- } while (skipLen < dataLen);
430
-
431
- #elif CONFIG_HAVE_VCBPRINTF
396
+ #if CONFIG_HAVE_VCBPRINTF
432
397
433
398
va_copy (aq, ap);
434
399
435
400
vcbprintf (TLVWriterPutcharCB, this , dataLen, fmt, aq);
436
401
437
402
va_end (aq);
438
-
439
- #else // CONFIG_HAVE_VSNPRINTF_EX
403
+ #else // CONFIG_HAVE_VCBPRINTF
440
404
441
405
tmpBuf = static_cast <char *>(chip::Platform::MemoryAlloc (dataLen + 1 ));
442
406
VerifyOrExit (tmpBuf != nullptr , err = CHIP_ERROR_NO_MEMORY);
@@ -450,7 +414,7 @@ CHIP_ERROR TLVWriter::VPutStringF(Tag tag, const char * fmt, va_list ap)
450
414
err = WriteData (reinterpret_cast <uint8_t *>(tmpBuf), static_cast <uint32_t >(dataLen));
451
415
chip::Platform::MemoryFree (tmpBuf);
452
416
453
- #endif // CONFIG_HAVE_VSNPRINTF_EX
417
+ #endif // CONFIG_HAVE_VCBPRINTF
454
418
455
419
exit :
456
420
@@ -694,19 +658,13 @@ CHIP_ERROR TLVWriter::WriteElementHead(TLVElementType elemType, Tag tag, uint64_
694
658
ABORT_ON_UNINITIALIZED_IF_ENABLED ();
695
659
696
660
VerifyOrReturnError (IsInitialized (), CHIP_ERROR_INCORRECT_STATE);
697
- uint8_t * p;
698
- uint8_t stagingBuf[17 ]; // 17 = 1 control byte + 8 tag bytes + 8 length/value bytes
699
-
700
661
if (IsContainerOpen ())
701
662
return CHIP_ERROR_TLV_CONTAINER_OPEN;
702
663
664
+ uint8_t stagingBuf[17 ]; // 17 = 1 control byte + 8 tag bytes + 8 length/value bytes
665
+ uint8_t * p = stagingBuf;
703
666
uint32_t tagNum = TagNumFromTag (tag);
704
667
705
- if ((mRemainingLen >= sizeof (stagingBuf)) && (mMaxLen >= sizeof (stagingBuf)))
706
- p = mWritePoint ;
707
- else
708
- p = stagingBuf;
709
-
710
668
if (IsSpecialTag (tag))
711
669
{
712
670
if (tagNum <= Tag::kContextTagMaxNum )
@@ -799,15 +757,9 @@ CHIP_ERROR TLVWriter::WriteElementHead(TLVElementType elemType, Tag tag, uint64_
799
757
break ;
800
758
}
801
759
802
- if ((mRemainingLen >= sizeof (stagingBuf)) && (mMaxLen >= sizeof (stagingBuf)))
803
- {
804
- uint32_t len = static_cast <uint32_t >(p - mWritePoint );
805
- mWritePoint = p;
806
- mRemainingLen -= len;
807
- mLenWritten += len;
808
- return CHIP_NO_ERROR;
809
- }
810
- return WriteData (stagingBuf, static_cast <uint32_t >(p - stagingBuf));
760
+ uint32_t bytesStaged = static_cast <uint32_t >(p - stagingBuf);
761
+ VerifyOrDie (bytesStaged <= sizeof (stagingBuf));
762
+ return WriteData (stagingBuf, bytesStaged);
811
763
}
812
764
813
765
CHIP_ERROR TLVWriter::WriteElementWithData (TLVType type, Tag tag, const uint8_t * data, uint32_t dataLen)
@@ -855,6 +807,7 @@ CHIP_ERROR TLVWriter::WriteData(const uint8_t * p, uint32_t len)
855
807
ReturnErrorOnFailure (mBackingStore ->FinalizeBuffer (*this , mBufStart , static_cast <uint32_t >(mWritePoint - mBufStart )));
856
808
857
809
ReturnErrorOnFailure (mBackingStore ->GetNewBuffer (*this , mBufStart , mRemainingLen ));
810
+ VerifyOrReturnError (mRemainingLen > 0 , CHIP_ERROR_NO_MEMORY);
858
811
859
812
mWritePoint = mBufStart ;
860
813
0 commit comments