Skip to content

Commit

Permalink
Enforce a maximum entry limit during append operations (#36843)
Browse files Browse the repository at this point in the history
* Enforce a maximum entry limit during append operations

* Add test for this change
  • Loading branch information
yufengwangca authored Dec 16, 2024
1 parent 33dda32 commit e3277eb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/app/clusters/user-label-server/user-label-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ CHIP_ERROR UserLabelAttrAccess::WriteLabelList(const ConcreteDataAttributePath &

return provider->SetUserLabelList(endpoint, labelList);
}

if (aPath.mListOp == ConcreteDataAttributePath::ListOperation::AppendItem)
{
Structs::LabelStruct::DecodableType entry;
Expand Down
32 changes: 32 additions & 0 deletions src/app/tests/suites/TestUserLabelClusterConstraints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,35 @@ tests:
]
response:
error: CONSTRAINT_ERROR

- label: "Attempt to write a large label list"
command: "writeAttribute"
attribute: "LabelList"
arguments:
value: [
# Example repeated user labels to blow up the maximum allowed
{ Label: "roomName", Value: "master bedroom 1" },
{ Label: "orientation", Value: "east" },
{ Label: "floor", Value: "2" },
{ Label: "roomType", Value: "bedroom" },
{ Label: "someKey5", Value: "someVal5" },
{ Label: "someKey6", Value: "someVal6" },
{ Label: "someKey7", Value: "someVal7" },
{ Label: "someKey8", Value: "someVal8" },
{ Label: "someKey9", Value: "someVal9" },
{ Label: "someKey10", Value: "someVal10" },
{ Label: "someKey11", Value: "someVal11" },
{ Label: "someKey12", Value: "someVal12" },
{ Label: "someKey13", Value: "someVal13" },
{ Label: "someKey14", Value: "someVal14" },
{ Label: "someKey15", Value: "someVal15" },
{ Label: "someKey16", Value: "someVal16" },
{ Label: "someKey17", Value: "someVal17" },
{ Label: "someKey18", Value: "someVal18" },
{ Label: "someKey19", Value: "someVal19" },
{ Label: "someKey20", Value: "someVal20" },
]
response:
# When the cluster runs out of capacity to store these entries,
# we expect a FAILURE get returned.
error: FAILURE
11 changes: 8 additions & 3 deletions src/platform/DeviceInfoProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,16 @@ CHIP_ERROR DeviceInfoProvider::AppendUserLabel(EndpointId endpoint, const UserLa
{
size_t length;

// Increase the size of UserLabelList by 1
// Fetch current list length
ReturnErrorOnFailure(GetUserLabelLength(endpoint, length));
ReturnErrorOnFailure(SetUserLabelLength(endpoint, length + 1));

// Append the user label at the end of UserLabelList
if (length >= kMaxUserLabelListLength)
{
return CHIP_ERROR_NO_MEMORY;
}

// Add the new entry to the list
ReturnErrorOnFailure(SetUserLabelLength(endpoint, length + 1));
ReturnErrorOnFailure(SetUserLabelAt(endpoint, length, label));

return CHIP_NO_ERROR;
Expand Down

0 comments on commit e3277eb

Please sign in to comment.