From 1033414e0e8708266f7a7707b496e95850141440 Mon Sep 17 00:00:00 2001 From: William Date: Tue, 20 Aug 2024 18:26:53 +0100 Subject: [PATCH] Fix SkipArea logic and removed the use of memcpy (#35075) * Updated the SkipArea serever handle to not error if the CurrentArea is null, according to the spec updates. * Replaced the use of memcpy with CopyCharSpanToMutableCharSpan. * Restyled by clang-format --------- Co-authored-by: Restyled.io --- .../service-area-cluster-objects.h | 28 +++++-------------- .../service-area-server.cpp | 9 ------ 2 files changed, 7 insertions(+), 30 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 1b040c53cdc1bf..66f20720701587 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 @@ -110,9 +110,9 @@ struct AreaStructureWrapper : public chip::app::Clusters::ServiceArea::Structs:: { areaDesc.locationInfo.SetNonNull(); // Copy the name - auto sizeToCopy = std::min(sizeof(mAreaNameBuffer), locationName.size()); - memcpy(mAreaNameBuffer, locationName.data(), sizeToCopy); - areaDesc.locationInfo.Value().locationName = CharSpan(mAreaNameBuffer, sizeToCopy); + 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; @@ -320,24 +320,10 @@ struct MapStructureWrapper : public chip::app::Clusters::ServiceArea::Structs::M */ void Set(uint32_t aMapId, const CharSpan & aMapName) { - mapID = aMapId; - - if (aMapName.empty()) - { - name = CharSpan(mMapNameBuffer, 0); - } - else if (aMapName.size() > sizeof(mMapNameBuffer)) - { - // Save the truncated name that fits into available size. - memcpy(mMapNameBuffer, aMapName.data(), sizeof(mMapNameBuffer)); - name = CharSpan(mMapNameBuffer, sizeof(mMapNameBuffer)); - } - else - { - // Save full name. - memcpy(mMapNameBuffer, aMapName.data(), aMapName.size()); - name = CharSpan(mMapNameBuffer, aMapName.size()); - } + mapID = aMapId; + auto mapNameSpan = MutableCharSpan(mMapNameBuffer, kMapNameMaxSize); + CopyCharSpanToMutableCharSpan(aMapName, mapNameSpan); + name = CharSpan(mapNameSpan.data(), mapNameSpan.size()); } /** diff --git a/src/app/clusters/service-area-server/service-area-server.cpp b/src/app/clusters/service-area-server/service-area-server.cpp index a4c132df8a6e4d..baf56cd4fc84be 100644 --- a/src/app/clusters/service-area-server/service-area-server.cpp +++ b/src/app/clusters/service-area-server/service-area-server.cpp @@ -378,15 +378,6 @@ void Instance::HandleSkipAreaCmd(HandlerContext & ctx, const Commands::SkipArea: return; } - // If the CurrentArea attribute is null, the status should be set to InvalidInMode. - // If the Status field is not set to Success, or InvalidAreaList, the StatusText field SHALL include a vendor defined error - // description. - if (mCurrentArea.IsNull()) - { - exitResponse(SkipAreaStatus::kInvalidInMode, "Current Area attribute is null"_span); - return; - } - // have the device attempt to skip // If the Status field is not set to Success, or InvalidAreaList, the StatusText field SHALL include a vendor defined error // description. InvalidInMode | The received request cannot be handled due to the current mode of the device. (skipStatusText to