Skip to content

Commit a6e3a9e

Browse files
committed
Memory allocation removed
1 parent 0b5b2c9 commit a6e3a9e

File tree

3 files changed

+60
-57
lines changed

3 files changed

+60
-57
lines changed

src/app/clusters/meter-identification-server/MeterIdentificationTestEventTriggerHandler.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace chip {
4444
enum class MeterIdentificationTrigger : uint64_t
4545
{
4646
// Scenarios
47-
// Attributes Value Update Test Event | Increment all attributes values
47+
// Attributes Value Update Test Event | Increment all attributes values (strings are incremented lexicographically)
4848
kAttributesValueUpdate = 0x0b06000000000000,
4949
// Attributes Value Test Event Clear | Return the device to pre-test status
5050
kAttributesValueUpdateClear = 0x0b06000000000001,

src/app/clusters/meter-identification-server/meter-identification-server.cpp

+55-56
Original file line numberDiff line numberDiff line change
@@ -50,38 +50,7 @@ bool NullableCharSpanCompare(const DataModel::Nullable<CharSpan> & a, const Data
5050

5151
return false;
5252
}
53-
54-
CHIP_ERROR NullableCharSpanCopy(DataModel::Nullable<CharSpan> & dst, const DataModel::Nullable<CharSpan> & src)
55-
{
56-
const size_t len = src.IsNull() ? 0 : src.Value().size();
57-
if (64 < len)
58-
{
59-
return CHIP_ERROR_INVALID_STRING_LENGTH;
60-
}
61-
62-
if (!dst.IsNull())
63-
{
64-
chip::Platform::MemoryFree(const_cast<char *>(dst.Value().data()));
65-
dst.SetNull();
66-
}
67-
68-
if (!src.IsNull())
69-
{
70-
if (auto * str = static_cast<char *>(chip::Platform::MemoryAlloc(1 + len)))
71-
{
72-
memcpy(str, src.Value().data(), len);
73-
str[len] = 0;
74-
dst = MakeNullable(CharSpan(str, len));
75-
}
76-
else
77-
{
78-
return CHIP_ERROR_NO_MEMORY;
79-
}
80-
}
81-
82-
return CHIP_NO_ERROR;
83-
}
84-
} // namespace
53+
} // namespace
8554

8655
namespace chip {
8756
namespace app {
@@ -91,18 +60,6 @@ namespace MeterIdentification {
9160
Instance::~Instance()
9261
{
9362
Shutdown();
94-
if (!mPointOfDelivery.IsNull())
95-
{
96-
chip::Platform::MemoryFree(const_cast<char *>(mPointOfDelivery.Value().data()));
97-
}
98-
if (!mMeterSerialNumber.IsNull())
99-
{
100-
chip::Platform::MemoryFree(const_cast<char *>(mMeterSerialNumber.Value().data()));
101-
}
102-
if (!mProtocolVersion.IsNull())
103-
{
104-
chip::Platform::MemoryFree(const_cast<char *>(mProtocolVersion.Value().data()));
105-
}
10663
}
10764

10865
CHIP_ERROR Instance::Init()
@@ -165,12 +122,26 @@ CHIP_ERROR Instance::SetPointOfDelivery(const DataModel::Nullable<CharSpan> & ne
165122
return CHIP_NO_ERROR;
166123
}
167124

168-
const CHIP_ERROR ret = NullableCharSpanCopy(mPointOfDelivery, newValue);
169-
if (CHIP_NO_ERROR == ret)
125+
const size_t len = newValue.IsNull() ? 0 : newValue.Value().size();
126+
if (kMaximumStringBufferSize <= len)
127+
{
128+
return CHIP_ERROR_INVALID_STRING_LENGTH;
129+
}
130+
131+
if (!mPointOfDelivery.IsNull())
132+
{
133+
mPointOfDelivery.SetNull();
134+
}
135+
136+
if (!newValue.IsNull())
170137
{
171-
MatterReportingAttributeChangeCallback(mEndpointId, MeterIdentification::Id, PointOfDelivery::Id);
138+
memcpy(mPointOfDeliveryBuf, newValue.Value().data(), len);
139+
mPointOfDeliveryBuf[len] = 0;
140+
mPointOfDelivery = MakeNullable(CharSpan(mPointOfDeliveryBuf, len));
172141
}
173-
return ret;
142+
143+
MatterReportingAttributeChangeCallback(mEndpointId, MeterIdentification::Id, PointOfDelivery::Id);
144+
return CHIP_NO_ERROR;
174145
}
175146

176147
CHIP_ERROR Instance::SetMeterSerialNumber(const DataModel::Nullable<CharSpan> & newValue)
@@ -180,12 +151,26 @@ CHIP_ERROR Instance::SetMeterSerialNumber(const DataModel::Nullable<CharSpan> &
180151
return CHIP_NO_ERROR;
181152
}
182153

183-
const CHIP_ERROR ret = NullableCharSpanCopy(mMeterSerialNumber, newValue);
184-
if (CHIP_NO_ERROR == ret)
154+
const size_t len = newValue.IsNull() ? 0 : newValue.Value().size();
155+
if (kMaximumStringBufferSize <= len)
185156
{
186-
MatterReportingAttributeChangeCallback(mEndpointId, MeterIdentification::Id, MeterSerialNumber::Id);
157+
return CHIP_ERROR_INVALID_STRING_LENGTH;
187158
}
188-
return ret;
159+
160+
if (!mMeterSerialNumber.IsNull())
161+
{
162+
mMeterSerialNumber.SetNull();
163+
}
164+
165+
if (!newValue.IsNull())
166+
{
167+
memcpy(mMeterSerialNumberBuf, newValue.Value().data(), len);
168+
mMeterSerialNumberBuf[len] = 0;
169+
mMeterSerialNumber = MakeNullable(CharSpan(mMeterSerialNumberBuf, len));
170+
}
171+
172+
MatterReportingAttributeChangeCallback(mEndpointId, MeterIdentification::Id, MeterSerialNumber::Id);
173+
return CHIP_NO_ERROR;
189174
}
190175

191176
CHIP_ERROR Instance::SetProtocolVersion(const DataModel::Nullable<CharSpan> & newValue)
@@ -195,12 +180,26 @@ CHIP_ERROR Instance::SetProtocolVersion(const DataModel::Nullable<CharSpan> & ne
195180
return CHIP_NO_ERROR;
196181
}
197182

198-
const CHIP_ERROR ret = NullableCharSpanCopy(mProtocolVersion, newValue);
199-
if (CHIP_NO_ERROR == ret)
183+
const size_t len = newValue.IsNull() ? 0 : newValue.Value().size();
184+
if (kMaximumStringBufferSize <= len)
185+
{
186+
return CHIP_ERROR_INVALID_STRING_LENGTH;
187+
}
188+
189+
if (!mProtocolVersion.IsNull())
200190
{
201-
MatterReportingAttributeChangeCallback(mEndpointId, MeterIdentification::Id, ProtocolVersion::Id);
191+
mProtocolVersion.SetNull();
202192
}
203-
return ret;
193+
194+
if (!newValue.IsNull())
195+
{
196+
memcpy(mProtocolVersionBuf, newValue.Value().data(), len);
197+
mProtocolVersionBuf[len] = 0;
198+
mProtocolVersion = MakeNullable(CharSpan(mProtocolVersionBuf, len));
199+
}
200+
201+
MatterReportingAttributeChangeCallback(mEndpointId, MeterIdentification::Id, ProtocolVersion::Id);
202+
return CHIP_NO_ERROR;
204203
}
205204

206205
CHIP_ERROR Instance::SetPowerThreshold(const DataModel::Nullable<Globals::Structs::PowerThresholdStruct::Type> & newValue)

src/app/clusters/meter-identification-server/meter-identification-server.h

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class Instance : public AttributeAccessInterface
6060

6161
private:
6262
// Attribute storage
63+
static constexpr size_t kMaximumStringBufferSize = 65;
64+
char mPointOfDeliveryBuf[kMaximumStringBufferSize] = {};
65+
char mMeterSerialNumberBuf[kMaximumStringBufferSize] = {};
66+
char mProtocolVersionBuf[kMaximumStringBufferSize] = {};
6367
DataModel::Nullable<MeterTypeEnum> mMeterType;
6468
DataModel::Nullable<CharSpan> mPointOfDelivery;
6569
DataModel::Nullable<CharSpan> mMeterSerialNumber;

0 commit comments

Comments
 (0)