Skip to content

Commit

Permalink
[darwin-framework-tool] Sending a command or issuing a write using th…
Browse files Browse the repository at this point in the history
…e any cluster leaks (project-chip#36215)

* [darwin-framework-tool] ClusterCommandBridge.h does not release the custom argument data, making the leak detector complaining

* [darwin-framework-tool] WriteAttributeCommandBridge.h does not release the custom argument data, making the leak detector complaining
  • Loading branch information
vivien-apple authored Oct 24, 2024
1 parent 197f698 commit 92fa1ad
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,8 @@ class ClusterCommand : public ModelCommand {

CHIP_ERROR SendCommand(MTRBaseDevice * _Nonnull device, chip::EndpointId endpointId) override
{
chip::TLV::TLVWriter writer;
chip::TLV::TLVReader reader;

mData = static_cast<uint8_t *>(chip::Platform::MemoryCalloc(sizeof(uint8_t), mDataMaxLen));
VerifyOrReturnError(mData != nullptr, CHIP_ERROR_NO_MEMORY);

writer.Init(mData, mDataMaxLen);

ReturnErrorOnFailure(mPayload.Encode(writer, chip::TLV::AnonymousTag()));
reader.Init(mData, writer.GetLengthWritten());
ReturnErrorOnFailure(reader.Next());

id commandFields = NSObjectFromCHIPTLV(&reader);
if (commandFields == nil) {
return CHIP_ERROR_INTERNAL;
}
id commandFields;
ReturnErrorOnFailure(GetCommandFields(&commandFields));
return ClusterCommand::SendCommand(device, endpointId, mClusterId, mCommandId, commandFields);
}

Expand Down Expand Up @@ -136,6 +122,35 @@ class ClusterCommand : public ModelCommand {
NSError * _Nullable mError = nil;

private:
CHIP_ERROR GetCommandFields(id _Nonnull * _Nonnull outCommandFields)
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::TLV::TLVWriter writer;
chip::TLV::TLVReader reader;

mData = static_cast<uint8_t *>(chip::Platform::MemoryCalloc(sizeof(uint8_t), mDataMaxLen));
VerifyOrExit(mData != nullptr, err = CHIP_ERROR_NO_MEMORY);

writer.Init(mData, mDataMaxLen);

err = mPayload.Encode(writer, chip::TLV::AnonymousTag());
SuccessOrExit(err);

reader.Init(mData, writer.GetLengthWritten());
err = reader.Next();
SuccessOrExit(err);

*outCommandFields = NSObjectFromCHIPTLV(&reader);
VerifyOrDo(nil != *outCommandFields, err = CHIP_ERROR_INTERNAL);

exit:
if (nullptr != mData) {
chip::Platform::MemoryFree(mData);
mData = nullptr;
}
return err;
}

chip::ClusterId mClusterId;
chip::CommandId mCommandId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,8 @@ class WriteAttribute : public ModelCommand {

CHIP_ERROR SendCommand(MTRBaseDevice * _Nonnull device, chip::EndpointId endpointId) override
{
chip::TLV::TLVWriter writer;
chip::TLV::TLVReader reader;

mData = static_cast<uint8_t *>(chip::Platform::MemoryCalloc(sizeof(uint8_t), mDataMaxLen));
VerifyOrReturnError(mData != nullptr, CHIP_ERROR_NO_MEMORY);

writer.Init(mData, mDataMaxLen);

ReturnErrorOnFailure(mAttributeValue.Encode(writer, chip::TLV::AnonymousTag()));
reader.Init(mData, writer.GetLengthWritten());
ReturnErrorOnFailure(reader.Next());

id value = NSObjectFromCHIPTLV(&reader);
if (value == nil) {
return CHIP_ERROR_INTERNAL;
}

id value;
ReturnErrorOnFailure(GetValue(&value));
return WriteAttribute::SendCommand(device, endpointId, mClusterId, mAttributeId, value);
}

Expand Down Expand Up @@ -122,6 +107,35 @@ class WriteAttribute : public ModelCommand {
chip::Optional<uint32_t> mDataVersion;

private:
CHIP_ERROR GetValue(id _Nonnull * _Nonnull outValue)
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::TLV::TLVWriter writer;
chip::TLV::TLVReader reader;

mData = static_cast<uint8_t *>(chip::Platform::MemoryCalloc(sizeof(uint8_t), mDataMaxLen));
VerifyOrExit(mData != nullptr, err = CHIP_ERROR_NO_MEMORY);

writer.Init(mData, mDataMaxLen);

err = mAttributeValue.Encode(writer, chip::TLV::AnonymousTag());
SuccessOrExit(err);

reader.Init(mData, writer.GetLengthWritten());
err = reader.Next();
SuccessOrExit(err);

*outValue = NSObjectFromCHIPTLV(&reader);
VerifyOrDo(nil != *outValue, err = CHIP_ERROR_INTERNAL);

exit:
if (nullptr != mData) {
chip::Platform::MemoryFree(mData);
mData = nullptr;
}
return err;
}

chip::ClusterId mClusterId;
chip::AttributeId mAttributeId;
CHIP_ERROR mError = CHIP_NO_ERROR;
Expand Down

0 comments on commit 92fa1ad

Please sign in to comment.