Skip to content

Commit

Permalink
Be more consistent about construction/destruction of ChipCertificateD…
Browse files Browse the repository at this point in the history
…ata.

We are mixing placement new with Clear() calls and no destructor
calls, and in some cases (ChipCertificateSet::Release) doing neither
clearing nor destruction.  Instead, try to consistently use
~ChipCertificateData when we are no longer keeping track of the
relevant object.

The addition of `Clear()` in `~ChipCertificateData` is both to match
existing behavior and on the assumption that we don't want that data
lying around in memory if not needed.
  • Loading branch information
bzbarsky-apple committed Feb 5, 2021
1 parent 775e37f commit f0889aa
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/credentials/CHIPCert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ void ChipCertificateSet::Release()
{
if (mCerts != nullptr)
{
Clear();
chip::Platform::MemoryFree(mCerts);
mCerts = nullptr;
}
Expand All @@ -136,7 +137,7 @@ void ChipCertificateSet::Clear()
{
for (int i = 0; i < mCertCount; i++)
{
mCerts[i].Clear();
mCerts[i].~ChipCertificateData();
}

mCertCount = 0;
Expand Down Expand Up @@ -240,7 +241,7 @@ CHIP_ERROR ChipCertificateSet::LoadCert(TLVReader & reader, BitFlags<uint8_t, Ce
{
if (cert != nullptr)
{
cert->Clear();
cert->~ChipCertificateData();
}
}

Expand Down Expand Up @@ -316,7 +317,7 @@ CHIP_ERROR ChipCertificateSet::LoadCerts(TLVReader & reader, BitFlags<uint8_t, C
{
for (uint8_t i = initialCertCount; i < mCertCount; i++)
{
mCerts[i].Clear();
mCerts[i].~ChipCertificateData();
}
mCertCount = initialCertCount;
}
Expand Down Expand Up @@ -629,7 +630,10 @@ ChipCertificateData::ChipCertificateData()
Clear();
}

ChipCertificateData::~ChipCertificateData() {}
ChipCertificateData::~ChipCertificateData()
{
Clear();
}

void ChipCertificateData::Clear()
{
Expand Down

0 comments on commit f0889aa

Please sign in to comment.