Skip to content

Commit a64773a

Browse files
Initialize scalar members in TLVReader (project-chip#32129)
* Initialize scalar members in TLVReader The documented return values for multiple TLVReader functions do match the actual return value for the uninitialize case. Explicitly initialize members with null/0 values to match the documentation. * Restyled by clang-format * Don't inline constructor * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 0c3f012 commit a64773a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/lib/core/TLVReader.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ using namespace chip::Encoding;
4949

5050
static const uint8_t sTagSizes[] = { 0, 1, 2, 4, 2, 4, 6, 8 };
5151

52+
TLVReader::TLVReader() :
53+
ImplicitProfileId(kProfileIdNotSpecified), AppData(nullptr), mElemLenOrVal(0), mBackingStore(nullptr), mReadPoint(nullptr),
54+
mBufEnd(nullptr), mLenRead(0), mMaxLen(0), mContainerType(kTLVType_NotSpecified), mControlByte(kTLVControlByte_NotSpecified),
55+
mContainerOpen(false)
56+
{}
57+
5258
void TLVReader::Init(const uint8_t * data, size_t dataLen)
5359
{
5460
// TODO: Maybe we can just make mMaxLen and mLenRead size_t instead?

src/lib/core/TLVReader.h

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class DLL_EXPORT TLVReader
8686
friend class TLVUpdater;
8787

8888
public:
89+
TLVReader();
90+
8991
/**
9092
* Initializes a TLVReader object from another TLVReader object.
9193
*

src/lib/core/tests/TestTLV.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -3832,6 +3832,27 @@ void TestTLVReader_SkipOverContainer(nlTestSuite * inSuite)
38323832
ForEachElement(inSuite, reader, nullptr, TestTLVReader_SkipOverContainer_ProcessElement);
38333833
}
38343834

3835+
/**
3836+
* Tests using an uninitialized TLVReader.
3837+
*/
3838+
void TestTLVReaderUninitialized(nlTestSuite * inSuite)
3839+
{
3840+
TLVReader reader;
3841+
3842+
NL_TEST_ASSERT(inSuite, reader.GetType() == kTLVType_NotSpecified);
3843+
NL_TEST_ASSERT(inSuite, reader.GetLength() == 0);
3844+
NL_TEST_ASSERT(inSuite, reader.GetControlByte() == kTLVControlByte_NotSpecified);
3845+
NL_TEST_ASSERT(inSuite, reader.GetContainerType() == kTLVType_NotSpecified);
3846+
NL_TEST_ASSERT(inSuite, reader.GetLengthRead() == 0);
3847+
NL_TEST_ASSERT(inSuite, reader.GetRemainingLength() == 0);
3848+
NL_TEST_ASSERT(inSuite, reader.GetTotalLength() == 0);
3849+
NL_TEST_ASSERT(inSuite, reader.GetBackingStore() == nullptr);
3850+
NL_TEST_ASSERT(inSuite, reader.IsElementDouble() == false);
3851+
NL_TEST_ASSERT(inSuite, reader.GetReadPoint() == nullptr);
3852+
NL_TEST_ASSERT(inSuite, reader.ImplicitProfileId == kProfileIdNotSpecified);
3853+
NL_TEST_ASSERT(inSuite, reader.AppData == nullptr);
3854+
}
3855+
38353856
/**
38363857
* Test CHIP TLV Reader
38373858
*/
@@ -3852,6 +3873,8 @@ void CheckTLVReader(nlTestSuite * inSuite, void * inContext)
38523873
TestTLVReader_NextOverContainer(inSuite);
38533874

38543875
TestTLVReader_SkipOverContainer(inSuite);
3876+
3877+
TestTLVReaderUninitialized(inSuite);
38553878
}
38563879

38573880
/**

0 commit comments

Comments
 (0)