Skip to content

Commit

Permalink
Truncate map and area names (#35127)
Browse files Browse the repository at this point in the history
* Updated the setting of the area and map names so that if the name given is greater than the buffer size, it's truncated rater to set to empty.

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Dec 16, 2024
1 parent 697bafb commit 1235002
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/app/clusters/service-area-server/service-area-cluster-objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,14 @@ struct AreaStructureWrapper : public chip::app::Clusters::ServiceArea::Structs::
const DataModel::Nullable<Globals::AreaTypeTag> & areaType)
{
areaDesc.locationInfo.SetNonNull();
// Copy the name
auto areaNameSpan = MutableCharSpan(mAreaNameBuffer, kAreaNameMaxSize);
CopyCharSpanToMutableCharSpan(locationName, areaNameSpan);
areaDesc.locationInfo.Value().locationName = CharSpan(areaNameSpan.data(), areaNameSpan.size());
areaDesc.locationInfo.Value().floorNumber = floorNumber;
areaDesc.locationInfo.Value().areaType = areaType;

// Copy the name. If the name is larger than kAreaNameMaxSize, truncate it to fit.
auto sizeToCopy = std::min(kAreaNameMaxSize, locationName.size());
memcpy(mAreaNameBuffer, locationName.data(), sizeToCopy);
areaDesc.locationInfo.Value().locationName = CharSpan(mAreaNameBuffer, sizeToCopy);

areaDesc.locationInfo.Value().floorNumber = floorNumber;
areaDesc.locationInfo.Value().areaType = areaType;

return *this;
}
Expand Down Expand Up @@ -320,10 +322,11 @@ struct MapStructureWrapper : public chip::app::Clusters::ServiceArea::Structs::M
*/
void Set(uint32_t aMapId, const CharSpan & aMapName)
{
mapID = aMapId;
auto mapNameSpan = MutableCharSpan(mMapNameBuffer, kMapNameMaxSize);
CopyCharSpanToMutableCharSpan(aMapName, mapNameSpan);
name = CharSpan(mapNameSpan.data(), mapNameSpan.size());
mapID = aMapId;
// Copy the name. If the name is larger than kMapNameMaxSize, truncate it to fit.
auto sizeToCopy = std::min(kMapNameMaxSize, aMapName.size());
memcpy(mMapNameBuffer, aMapName.data(), sizeToCopy);
name = CharSpan(mMapNameBuffer, sizeToCopy);
}

/**
Expand Down

0 comments on commit 1235002

Please sign in to comment.