From 8e32ce7ea5e6250aee0f1e04443ccb3cfdb824be Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 23 Jul 2024 18:26:06 -0400 Subject: [PATCH] Fix leak in chip-tool when using "any" commands. (#34461) We were not properly cleaning up the buffer in a CustomArgument when a command using it finished. Addresses the main part of https://github.com/project-chip/connectedhomeip/issues/34221 --- .../commands/clusters/CustomArgument.h | 17 ++++++++++------- examples/chip-tool/commands/common/Command.cpp | 5 +++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/examples/chip-tool/commands/clusters/CustomArgument.h b/examples/chip-tool/commands/clusters/CustomArgument.h index c7d81414659755..05da3a37936119 100644 --- a/examples/chip-tool/commands/clusters/CustomArgument.h +++ b/examples/chip-tool/commands/clusters/CustomArgument.h @@ -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) { @@ -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; diff --git a/examples/chip-tool/commands/common/Command.cpp b/examples/chip-tool/commands/common/Command.cpp index 2c4f1eb3540e9f..82e9eedc461dd5 100644 --- a/examples/chip-tool/commands/common/Command.cpp +++ b/examples/chip-tool/commands/common/Command.cpp @@ -1069,6 +1069,11 @@ void Command::ResetArguments() auto vectorArgument = static_cast *>(arg.value); vectorArgument->clear(); } + else if (type == ArgumentType::Custom) + { + auto argument = static_cast(arg.value); + argument->Reset(); + } else if (type == ArgumentType::VectorCustom) { auto vectorArgument = static_cast *>(arg.value);