Skip to content

Commit 07789d4

Browse files
Truncate map and area names (#35127)
* 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>
1 parent 72ee6eb commit 07789d4

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/app/clusters/service-area-server/service-area-cluster-objects.h

+13-10
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,14 @@ struct AreaStructureWrapper : public chip::app::Clusters::ServiceArea::Structs::
109109
const DataModel::Nullable<Globals::AreaTypeTag> & areaType)
110110
{
111111
areaDesc.locationInfo.SetNonNull();
112-
// Copy the name
113-
auto areaNameSpan = MutableCharSpan(mAreaNameBuffer, kAreaNameMaxSize);
114-
CopyCharSpanToMutableCharSpan(locationName, areaNameSpan);
115-
areaDesc.locationInfo.Value().locationName = CharSpan(areaNameSpan.data(), areaNameSpan.size());
116-
areaDesc.locationInfo.Value().floorNumber = floorNumber;
117-
areaDesc.locationInfo.Value().areaType = areaType;
112+
113+
// Copy the name. If the name is larger than kAreaNameMaxSize, truncate it to fit.
114+
auto sizeToCopy = std::min(kAreaNameMaxSize, locationName.size());
115+
memcpy(mAreaNameBuffer, locationName.data(), sizeToCopy);
116+
areaDesc.locationInfo.Value().locationName = CharSpan(mAreaNameBuffer, sizeToCopy);
117+
118+
areaDesc.locationInfo.Value().floorNumber = floorNumber;
119+
areaDesc.locationInfo.Value().areaType = areaType;
118120

119121
return *this;
120122
}
@@ -320,10 +322,11 @@ struct MapStructureWrapper : public chip::app::Clusters::ServiceArea::Structs::M
320322
*/
321323
void Set(uint32_t aMapId, const CharSpan & aMapName)
322324
{
323-
mapID = aMapId;
324-
auto mapNameSpan = MutableCharSpan(mMapNameBuffer, kMapNameMaxSize);
325-
CopyCharSpanToMutableCharSpan(aMapName, mapNameSpan);
326-
name = CharSpan(mapNameSpan.data(), mapNameSpan.size());
325+
mapID = aMapId;
326+
// Copy the name. If the name is larger than kMapNameMaxSize, truncate it to fit.
327+
auto sizeToCopy = std::min(kMapNameMaxSize, aMapName.size());
328+
memcpy(mMapNameBuffer, aMapName.data(), sizeToCopy);
329+
name = CharSpan(mMapNameBuffer, sizeToCopy);
327330
}
328331

329332
/**

0 commit comments

Comments
 (0)