Skip to content

Commit

Permalink
Initialize scalar members in TLVReader (#32129)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
mwswartwout and restyled-commits authored Feb 21, 2024
1 parent 0c3f012 commit a64773a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/lib/core/TLVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ using namespace chip::Encoding;

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

TLVReader::TLVReader() :
ImplicitProfileId(kProfileIdNotSpecified), AppData(nullptr), mElemLenOrVal(0), mBackingStore(nullptr), mReadPoint(nullptr),
mBufEnd(nullptr), mLenRead(0), mMaxLen(0), mContainerType(kTLVType_NotSpecified), mControlByte(kTLVControlByte_NotSpecified),
mContainerOpen(false)
{}

void TLVReader::Init(const uint8_t * data, size_t dataLen)
{
// TODO: Maybe we can just make mMaxLen and mLenRead size_t instead?
Expand Down
2 changes: 2 additions & 0 deletions src/lib/core/TLVReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class DLL_EXPORT TLVReader
friend class TLVUpdater;

public:
TLVReader();

/**
* Initializes a TLVReader object from another TLVReader object.
*
Expand Down
23 changes: 23 additions & 0 deletions src/lib/core/tests/TestTLV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3832,6 +3832,27 @@ void TestTLVReader_SkipOverContainer(nlTestSuite * inSuite)
ForEachElement(inSuite, reader, nullptr, TestTLVReader_SkipOverContainer_ProcessElement);
}

/**
* Tests using an uninitialized TLVReader.
*/
void TestTLVReaderUninitialized(nlTestSuite * inSuite)
{
TLVReader reader;

NL_TEST_ASSERT(inSuite, reader.GetType() == kTLVType_NotSpecified);
NL_TEST_ASSERT(inSuite, reader.GetLength() == 0);
NL_TEST_ASSERT(inSuite, reader.GetControlByte() == kTLVControlByte_NotSpecified);
NL_TEST_ASSERT(inSuite, reader.GetContainerType() == kTLVType_NotSpecified);
NL_TEST_ASSERT(inSuite, reader.GetLengthRead() == 0);
NL_TEST_ASSERT(inSuite, reader.GetRemainingLength() == 0);
NL_TEST_ASSERT(inSuite, reader.GetTotalLength() == 0);
NL_TEST_ASSERT(inSuite, reader.GetBackingStore() == nullptr);
NL_TEST_ASSERT(inSuite, reader.IsElementDouble() == false);
NL_TEST_ASSERT(inSuite, reader.GetReadPoint() == nullptr);
NL_TEST_ASSERT(inSuite, reader.ImplicitProfileId == kProfileIdNotSpecified);
NL_TEST_ASSERT(inSuite, reader.AppData == nullptr);
}

/**
* Test CHIP TLV Reader
*/
Expand All @@ -3852,6 +3873,8 @@ void CheckTLVReader(nlTestSuite * inSuite, void * inContext)
TestTLVReader_NextOverContainer(inSuite);

TestTLVReader_SkipOverContainer(inSuite);

TestTLVReaderUninitialized(inSuite);
}

/**
Expand Down

0 comments on commit a64773a

Please sign in to comment.