From 1235002cf5b0a65a13aab6cc3f00fec4b57be150 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 22 Aug 2024 09:56:19 +0100 Subject: [PATCH] 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 --- .../service-area-cluster-objects.h | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/app/clusters/service-area-server/service-area-cluster-objects.h b/src/app/clusters/service-area-server/service-area-cluster-objects.h index 66f20720701587..69a392a874f49d 100644 --- a/src/app/clusters/service-area-server/service-area-cluster-objects.h +++ b/src/app/clusters/service-area-server/service-area-cluster-objects.h @@ -109,12 +109,14 @@ struct AreaStructureWrapper : public chip::app::Clusters::ServiceArea::Structs:: const DataModel::Nullable & 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; } @@ -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); } /**