Skip to content

Commit 8e32ce7

Browse files
Fix leak in chip-tool when using "any" commands. (project-chip#34461)
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
1 parent b68df5a commit 8e32ce7

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

examples/chip-tool/commands/clusters/CustomArgument.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,7 @@ class CustomArgumentParser
230230
class CustomArgument
231231
{
232232
public:
233-
~CustomArgument()
234-
{
235-
if (mData != nullptr)
236-
{
237-
chip::Platform::MemoryFree(mData);
238-
}
239-
}
233+
~CustomArgument() { Reset(); }
240234

241235
CHIP_ERROR Parse(const char * label, const char * json)
242236
{
@@ -286,6 +280,15 @@ class CustomArgument
286280
return writer.CopyElement(tag, reader);
287281
}
288282

283+
void Reset()
284+
{
285+
if (mData != nullptr)
286+
{
287+
chip::Platform::MemoryFree(mData);
288+
mData = nullptr;
289+
}
290+
}
291+
289292
// We trust our consumers to do the encoding of our data correctly, so don't
290293
// need to know whether we are being encoded for a write.
291294
static constexpr bool kIsFabricScoped = false;

examples/chip-tool/commands/common/Command.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,11 @@ void Command::ResetArguments()
10691069
auto vectorArgument = static_cast<std::vector<uint32_t> *>(arg.value);
10701070
vectorArgument->clear();
10711071
}
1072+
else if (type == ArgumentType::Custom)
1073+
{
1074+
auto argument = static_cast<CustomArgument *>(arg.value);
1075+
argument->Reset();
1076+
}
10721077
else if (type == ArgumentType::VectorCustom)
10731078
{
10741079
auto vectorArgument = static_cast<std::vector<CustomArgument *> *>(arg.value);

0 commit comments

Comments
 (0)