Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SkipArea logic and removed the use of memcpy #35075

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Comment on lines +113 to +115
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what it's worth, that last line could just have been:

areaDesc.locationInfo.Value().locationName = areaNameSpan;

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

Expand Down Expand Up @@ -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);
Copy link
Contributor

@bzbarsky-apple bzbarsky-apple Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this has different behavior from the old code, right? The old code copied as much as fit. CopyCharSpanToMutableCharSpan will copy nothing in the too-long case, return error, and mapNameSpan will be holding random data....

I think the code above also has that problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting this @bzbarsky-apple. I did not check the implementation of this method. @cecille, ideally this method does not error as otherwise setting these values by simply using consecutive .Set methods would be cumbersome. Hence, I think we should revert to the old implementation.

name = CharSpan(mapNameSpan.data(), mapNameSpan.size());
}

/**
Expand Down
9 changes: 0 additions & 9 deletions src/app/clusters/service-area-server/service-area-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading