[chip-tool[darwin-framework-tool] Using ComplexArgument leaks on syntax failure and even on success (#36227)
* [chip-tool[darwin-framework-tool] Using a malformed ComplexArgument may result into a leak
* [chip-tool[darwin-framework-tool] Even on success the ComplexArgument may not be cleared properly
---------
Co-authored-by: Andrei Litvin <andy314@gmail.com>
diff --git a/examples/chip-tool/commands/clusters/ComplexArgument.h b/examples/chip-tool/commands/clusters/ComplexArgument.h
index 6f92256..695af2f 100644
--- a/examples/chip-tool/commands/clusters/ComplexArgument.h
+++ b/examples/chip-tool/commands/clusters/ComplexArgument.h
@@ -181,7 +181,12 @@
// - 11 is the maximum length of a %d (-2147483648, 2147483647)
// - 2 is the length for the "[" and "]" characters.
snprintf(labelWithIndex, sizeof(labelWithIndex), "%.241s[%d]", label, i);
- ReturnErrorOnFailure(ComplexArgumentParser::Setup(labelWithIndex, content[i], value[i]));
+ auto error = ComplexArgumentParser::Setup(labelWithIndex, content[i], value[i]);
+ if (CHIP_NO_ERROR != error)
+ {
+ chip::Platform::MemoryFree(content);
+ return error;
+ }
}
request = chip::app::DataModel::List<T>(content, value.size());
@@ -415,7 +420,14 @@
return ComplexArgumentParser::Setup(label, *mRequest, value);
}
- void Reset() { *mRequest = T(); }
+ void Reset()
+ {
+ if (mRequest != nullptr)
+ {
+ ComplexArgumentParser::Finalize(*mRequest);
+ *mRequest = T();
+ }
+ }
private:
T * mRequest;