Skip to content

Commit bdbd6de

Browse files
tcarmelveilleuxtennessee-googlerestyled-commitskpschoedel
authored
Remove several direct uses of TLVWriter::mWritePoint (#31770)
* Remove several direct uses of TLVWriter::mWritePoint - Remove all writing to mWritePoint that occur outside of WriteData - Remove unsafe usage of CONFIG_HAVE_VSNPRINTF_EX - Does not impact public API as it was one underlying possible optimization, and the remaining code would work. Also, not used in SDK at all. - Removed secondary paths that were potentially unsafe in TLVWriter::WriteElementHead Fixes #31769 Testing done: - All existing unit tests still pass. - Only removed and non-functionally-refactored code. * Restyled by clang-format * Update src/lib/core/TLVWriter.cpp Co-authored-by: Kevin Schoedel <kpschoedel@google.com> --------- Co-authored-by: tennessee.carmelveilleux@gmail.com <tennessee@google.com> Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Kevin Schoedel <kpschoedel@google.com>
1 parent 5f955f4 commit bdbd6de

File tree

2 files changed

+11
-59
lines changed

2 files changed

+11
-59
lines changed

src/lib/core/TLVWriter.cpp

+11-58
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ CHIP_ERROR TLVWriter::Init(TLVBackingStore & backingStore, uint32_t maxLen /* =
101101
if (err != CHIP_NO_ERROR)
102102
return err;
103103

104+
VerifyOrReturnError(mBufStart != nullptr, CHIP_ERROR_INTERNAL);
104105
mWritePoint = mBufStart;
105106
mInitializationCookie = kExpectedInitializationCookie;
106107
return CHIP_NO_ERROR;
@@ -363,11 +364,7 @@ CHIP_ERROR TLVWriter::VPutStringF(Tag tag, const char * fmt, va_list ap)
363364
size_t dataLen;
364365
CHIP_ERROR err = CHIP_NO_ERROR;
365366
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
371368
char * tmpBuf;
372369
#endif
373370
va_copy(aq, ap);
@@ -396,47 +393,14 @@ CHIP_ERROR TLVWriter::VPutStringF(Tag tag, const char * fmt, va_list ap)
396393
VerifyOrExit((mLenWritten + dataLen) <= mMaxLen, err = CHIP_ERROR_BUFFER_TOO_SMALL);
397394

398395
// 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
432397

433398
va_copy(aq, ap);
434399

435400
vcbprintf(TLVWriterPutcharCB, this, dataLen, fmt, aq);
436401

437402
va_end(aq);
438-
439-
#else // CONFIG_HAVE_VSNPRINTF_EX
403+
#else // CONFIG_HAVE_VCBPRINTF
440404

441405
tmpBuf = static_cast<char *>(chip::Platform::MemoryAlloc(dataLen + 1));
442406
VerifyOrExit(tmpBuf != nullptr, err = CHIP_ERROR_NO_MEMORY);
@@ -450,7 +414,7 @@ CHIP_ERROR TLVWriter::VPutStringF(Tag tag, const char * fmt, va_list ap)
450414
err = WriteData(reinterpret_cast<uint8_t *>(tmpBuf), static_cast<uint32_t>(dataLen));
451415
chip::Platform::MemoryFree(tmpBuf);
452416

453-
#endif // CONFIG_HAVE_VSNPRINTF_EX
417+
#endif // CONFIG_HAVE_VCBPRINTF
454418

455419
exit:
456420

@@ -694,19 +658,13 @@ CHIP_ERROR TLVWriter::WriteElementHead(TLVElementType elemType, Tag tag, uint64_
694658
ABORT_ON_UNINITIALIZED_IF_ENABLED();
695659

696660
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-
700661
if (IsContainerOpen())
701662
return CHIP_ERROR_TLV_CONTAINER_OPEN;
702663

664+
uint8_t stagingBuf[17]; // 17 = 1 control byte + 8 tag bytes + 8 length/value bytes
665+
uint8_t * p = stagingBuf;
703666
uint32_t tagNum = TagNumFromTag(tag);
704667

705-
if ((mRemainingLen >= sizeof(stagingBuf)) && (mMaxLen >= sizeof(stagingBuf)))
706-
p = mWritePoint;
707-
else
708-
p = stagingBuf;
709-
710668
if (IsSpecialTag(tag))
711669
{
712670
if (tagNum <= Tag::kContextTagMaxNum)
@@ -799,15 +757,9 @@ CHIP_ERROR TLVWriter::WriteElementHead(TLVElementType elemType, Tag tag, uint64_
799757
break;
800758
}
801759

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);
811763
}
812764

813765
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)
855807
ReturnErrorOnFailure(mBackingStore->FinalizeBuffer(*this, mBufStart, static_cast<uint32_t>(mWritePoint - mBufStart)));
856808

857809
ReturnErrorOnFailure(mBackingStore->GetNewBuffer(*this, mBufStart, mRemainingLen));
810+
VerifyOrReturnError(mRemainingLen > 0, CHIP_ERROR_NO_MEMORY);
858811

859812
mWritePoint = mBufStart;
860813

src/system/BUILD.gn

-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ buildconfig_header("system_buildconfig") {
118118
"HAVE_NETINET_ICMP6_H=true",
119119
"HAVE_ICMP6_FILTER=true",
120120
"CONFIG_HAVE_VCBPRINTF=false",
121-
"CONFIG_HAVE_VSNPRINTF_EX=false",
122121
"HAVE_SYS_SOCKET_H=${chip_system_config_use_sockets}",
123122
]
124123

0 commit comments

Comments
 (0)