Skip to content

Commit 4121252

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Correctly return RESOURCE_EXHAUSTED when too many bindings are written. (#25549)
Fixes #25538
1 parent 2e559ba commit 4121252

File tree

5 files changed

+255
-14
lines changed

5 files changed

+255
-14
lines changed

src/app/clusters/bindings/bindings.cpp

+24-11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <app/util/attribute-storage.h>
3131
#include <app/util/config.h>
3232
#include <lib/support/logging/CHIPLogging.h>
33+
#include <protocols/interaction_model/StatusCode.h>
34+
3335
using namespace chip;
3436
using namespace chip::app;
3537
using namespace chip::app::Clusters;
@@ -117,7 +119,7 @@ CHIP_ERROR CheckValidBindingList(const EndpointId localEndpoint, const Decodable
117119
return CHIP_NO_ERROR;
118120
}
119121

120-
void CreateBindingEntry(const TargetStructType & entry, EndpointId localEndpoint)
122+
CHIP_ERROR CreateBindingEntry(const TargetStructType & entry, EndpointId localEndpoint)
121123
{
122124
EmberBindingTableEntry bindingEntry;
123125

@@ -131,7 +133,7 @@ void CreateBindingEntry(const TargetStructType & entry, EndpointId localEndpoint
131133
entry.cluster);
132134
}
133135

134-
AddBindingEntry(bindingEntry);
136+
return AddBindingEntry(bindingEntry);
135137
}
136138

137139
CHIP_ERROR BindingTableAccess::Read(const ConcreteReadAttributePath & path, AttributeValueEncoder & encoder)
@@ -225,10 +227,11 @@ CHIP_ERROR BindingTableAccess::WriteBindingTable(const ConcreteDataAttributePath
225227
}
226228

227229
// Add new entries
228-
auto iter = newBindingList.begin();
229-
while (iter.Next())
230+
auto iter = newBindingList.begin();
231+
CHIP_ERROR err = CHIP_NO_ERROR;
232+
while (iter.Next() && err == CHIP_NO_ERROR)
230233
{
231-
CreateBindingEntry(iter.GetValue(), path.mEndpointId);
234+
err = CreateBindingEntry(iter.GetValue(), path.mEndpointId);
232235
}
233236

234237
// If this was not caused by a list operation, OnListWriteEnd is not going to be triggered
@@ -238,7 +241,7 @@ CHIP_ERROR BindingTableAccess::WriteBindingTable(const ConcreteDataAttributePath
238241
// Notify binding table has changed
239242
LogErrorOnFailure(NotifyBindingsChanged());
240243
}
241-
return CHIP_NO_ERROR;
244+
return err;
242245
}
243246
if (path.mListOp == ConcreteDataAttributePath::ListOperation::AppendItem)
244247
{
@@ -248,8 +251,7 @@ CHIP_ERROR BindingTableAccess::WriteBindingTable(const ConcreteDataAttributePath
248251
{
249252
return CHIP_IM_GLOBAL_STATUS(ConstraintError);
250253
}
251-
CreateBindingEntry(target, path.mEndpointId);
252-
return CHIP_NO_ERROR;
254+
return CreateBindingEntry(target, path.mEndpointId);
253255
}
254256
return CHIP_IM_GLOBAL_STATUS(UnsupportedWrite);
255257
}
@@ -269,11 +271,22 @@ void MatterBindingPluginServerInitCallback()
269271
registerAttributeAccessOverride(&gAttrAccess);
270272
}
271273

272-
void AddBindingEntry(const EmberBindingTableEntry & entry)
274+
CHIP_ERROR AddBindingEntry(const EmberBindingTableEntry & entry)
273275
{
276+
CHIP_ERROR err = BindingTable::GetInstance().Add(entry);
277+
if (err == CHIP_ERROR_NO_MEMORY)
278+
{
279+
return CHIP_IM_GLOBAL_STATUS(ResourceExhausted);
280+
}
281+
282+
if (err != CHIP_NO_ERROR)
283+
{
284+
return err;
285+
}
286+
274287
if (entry.type == EMBER_UNICAST_BINDING)
275288
{
276-
CHIP_ERROR err = BindingManager::GetInstance().UnicastBindingCreated(entry.fabricIndex, entry.nodeId);
289+
err = BindingManager::GetInstance().UnicastBindingCreated(entry.fabricIndex, entry.nodeId);
277290
if (err != CHIP_NO_ERROR)
278291
{
279292
// Unicast connection failure can happen if peer is offline. We'll retry connection on-demand.
@@ -283,5 +296,5 @@ void AddBindingEntry(const EmberBindingTableEntry & entry)
283296
}
284297
}
285298

286-
BindingTable::GetInstance().Add(entry);
299+
return CHIP_NO_ERROR;
287300
}

src/app/clusters/bindings/bindings.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
*
2929
* @param entry binding to add
3030
*/
31-
void AddBindingEntry(const EmberBindingTableEntry & entry);
31+
CHIP_ERROR AddBindingEntry(const EmberBindingTableEntry & entry);

src/app/tests/suites/TestBinding.yaml

+21
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,24 @@ tests:
104104
{ FabricIndex: 1, Node: 1, Endpoint: 1, Cluster: 6 },
105105
{ FabricIndex: 1, Node: 2, Endpoint: 1 },
106106
]
107+
108+
- label: "Write over-long binding table on endpoint 1"
109+
command: "writeAttribute"
110+
attribute: "Binding"
111+
arguments:
112+
value:
113+
[
114+
{ FabricIndex: 0, Node: 1, Endpoint: 1, Cluster: 6 },
115+
{ FabricIndex: 0, Node: 2, Endpoint: 2, Cluster: 6 },
116+
{ FabricIndex: 0, Node: 3, Endpoint: 3, Cluster: 6 },
117+
{ FabricIndex: 0, Node: 4, Endpoint: 4, Cluster: 6 },
118+
{ FabricIndex: 0, Node: 5, Endpoint: 5, Cluster: 6 },
119+
{ FabricIndex: 0, Node: 6, Endpoint: 6, Cluster: 6 },
120+
{ FabricIndex: 0, Node: 7, Endpoint: 7, Cluster: 6 },
121+
{ FabricIndex: 0, Node: 8, Endpoint: 8, Cluster: 6 },
122+
{ FabricIndex: 0, Node: 9, Endpoint: 9, Cluster: 6 },
123+
{ FabricIndex: 0, Node: 10, Endpoint: 10, Cluster: 6 },
124+
{ FabricIndex: 0, Node: 11, Endpoint: 11, Cluster: 6 },
125+
]
126+
response:
127+
error: RESOURCE_EXHAUSTED

zzz_generated/chip-tool/zap-generated/test/Commands.h

+107-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zzz_generated/darwin-framework-tool/zap-generated/test/Commands.h

+102-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)