Skip to content

Commit

Permalink
Fix leak in chip-tool when using "any" commands. (project-chip#34461)
Browse files Browse the repository at this point in the history
We were not properly cleaning up the buffer in a CustomArgument when a command
using it finished.

Addresses the main part of project-chip#34221
  • Loading branch information
bzbarsky-apple authored Jul 23, 2024
1 parent b68df5a commit 8e32ce7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
17 changes: 10 additions & 7 deletions examples/chip-tool/commands/clusters/CustomArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,7 @@ class CustomArgumentParser
class CustomArgument
{
public:
~CustomArgument()
{
if (mData != nullptr)
{
chip::Platform::MemoryFree(mData);
}
}
~CustomArgument() { Reset(); }

CHIP_ERROR Parse(const char * label, const char * json)
{
Expand Down Expand Up @@ -286,6 +280,15 @@ class CustomArgument
return writer.CopyElement(tag, reader);
}

void Reset()
{
if (mData != nullptr)
{
chip::Platform::MemoryFree(mData);
mData = nullptr;
}
}

// We trust our consumers to do the encoding of our data correctly, so don't
// need to know whether we are being encoded for a write.
static constexpr bool kIsFabricScoped = false;
Expand Down
5 changes: 5 additions & 0 deletions examples/chip-tool/commands/common/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,11 @@ void Command::ResetArguments()
auto vectorArgument = static_cast<std::vector<uint32_t> *>(arg.value);
vectorArgument->clear();
}
else if (type == ArgumentType::Custom)
{
auto argument = static_cast<CustomArgument *>(arg.value);
argument->Reset();
}
else if (type == ArgumentType::VectorCustom)
{
auto vectorArgument = static_cast<std::vector<CustomArgument *> *>(arg.value);
Expand Down

0 comments on commit 8e32ce7

Please sign in to comment.