Skip to content

Commit 4f25d84

Browse files
encourage the use of CopyCharSpanToMutableCharSpanWithTruncation (project-chip#35444)
* Updated the service area documentation to encourage the use of CopyCharSpanToMutableCharSpanWithTruncation for memory safety. Updated the rvc-app example to use this method. * Restyled by whitespace --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent edf1f65 commit 4f25d84

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

examples/rvc-app/rvc-common/src/rvc-device.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ bool RvcDevice::SaIsSetSelectedAreasAllowed(MutableCharSpan & statusText)
168168
{
169169
if (mOperationalStateInstance.GetCurrentOperationalState() == to_underlying(OperationalState::OperationalStateEnum::kRunning))
170170
{
171-
CopyCharSpanToMutableCharSpan("cannot set the Selected Areas while the device is running"_span, statusText);
171+
CopyCharSpanToMutableCharSpanWithTruncation("cannot set the Selected Areas while the device is running"_span, statusText);
172172
return false;
173173
}
174174
return true;
@@ -179,14 +179,14 @@ bool RvcDevice::SaHandleSkipArea(uint32_t skippedArea, MutableCharSpan & skipSta
179179
if (mServiceAreaInstance.GetCurrentArea() != skippedArea)
180180
{
181181
// This device only supports skipping the current location.
182-
CopyCharSpanToMutableCharSpan("the skipped area does not match the current area"_span, skipStatusText);
182+
CopyCharSpanToMutableCharSpanWithTruncation("the skipped area does not match the current area"_span, skipStatusText);
183183
return false;
184184
}
185185

186186
if (mOperationalStateInstance.GetCurrentOperationalState() != to_underlying(OperationalState::OperationalStateEnum::kRunning))
187187
{
188188
// This device only accepts the skip are command while in the running state
189-
CopyCharSpanToMutableCharSpan("skip area is only accepted when the device is running"_span, skipStatusText);
189+
CopyCharSpanToMutableCharSpanWithTruncation("skip area is only accepted when the device is running"_span, skipStatusText);
190190
return false;
191191
}
192192

examples/rvc-app/rvc-common/src/rvc-service-area-delegate.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ bool RvcServiceAreaDelegate::IsValidSelectAreasSet(const Span<const uint32_t> &
134134
if (!GetInstance()->GetSupportedAreaById(selectedAreas[0], ignoredIndex, tempArea))
135135
{
136136
areaStatus = SelectAreasStatus::kUnsupportedArea;
137-
CopyCharSpanToMutableCharSpan("unable to find selected area in supported areas"_span, statusText);
137+
CopyCharSpanToMutableCharSpanWithTruncation("unable to find selected area in supported areas"_span, statusText);
138138
return false;
139139
}
140140

@@ -145,14 +145,14 @@ bool RvcServiceAreaDelegate::IsValidSelectAreasSet(const Span<const uint32_t> &
145145
if (!GetInstance()->GetSupportedAreaById(areaId, ignoredIndex, tempArea))
146146
{
147147
areaStatus = SelectAreasStatus::kUnsupportedArea;
148-
CopyCharSpanToMutableCharSpan("unable to find selected area in supported areas"_span, statusText);
148+
CopyCharSpanToMutableCharSpanWithTruncation("unable to find selected area in supported areas"_span, statusText);
149149
return false;
150150
}
151151

152152
if (tempArea.mapID.Value() != mapId)
153153
{
154154
areaStatus = SelectAreasStatus::kInvalidSet;
155-
CopyCharSpanToMutableCharSpan("all selected areas must be in the same map"_span, statusText);
155+
CopyCharSpanToMutableCharSpanWithTruncation("all selected areas must be in the same map"_span, statusText);
156156
return false;
157157
}
158158
}

src/app/clusters/service-area-server/service-area-delegate.h

+13-10
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ class Delegate
5555
* @brief Can the selected locations be set by the client in the current operating mode?
5656
* @param[out] statusText text describing why the selected locations cannot be set (if return is false).
5757
* Max size kMaxSizeStatusText.
58-
* Note: If the return is false and statusText is not successfully set, for example due to a string that can be longer than
59-
* kMaxSizeStatusText, the size of this value should be set to 0 with .reduce_size(0) to avoid callers using un-initialized
60-
* memory.
58+
* Note: statusText must be successfully set if the return is false. Use CopyCharSpanToMutableCharSpanWithTruncation to
59+
* ensure that a message is copied successfully. Otherwise, ensure that if setting the statusText can fail (e.g., due
60+
* to exceeding kMaxSizeStatusText) the size of this value is set to 0 with .reduce_size(0) to avoid callers using
61+
* un-initialized memory.
6162
* @return true if the current device state allows selected locations to be set by user.
6263
*
6364
* @note The statusText field SHOULD indicate why the request is not allowed, given the current mode
@@ -77,9 +78,10 @@ class Delegate
7778
* @param[in] selectedAreas List of new selected locations.
7879
* @param[out] locationStatus Success if all checks pass, error code if failure.
7980
* @param[out] statusText text describing failure (see description above). Max size kMaxSizeStatusText.
80-
* Note: If the return is false and statusText is not successfully set, for example due to a string that can be longer than
81-
* kMaxSizeStatusText, the size of this value should be set to 0 with .reduce_size(0) to avoid callers using un-initialized
82-
* memory.
81+
* Note: statusText must be successfully set if the return is false. Use CopyCharSpanToMutableCharSpanWithTruncation to
82+
* ensure that a message is copied successfully. Otherwise, ensure that if setting the statusText can fail (e.g., due
83+
* to exceeding kMaxSizeStatusText) the size of this value is set to 0 with .reduce_size(0) to avoid callers using
84+
* un-initialized memory.
8385
* @return true if success.
8486
*
8587
* @note If the SelectAreas command is allowed when the device is operating and the selected locations change to none, the
@@ -93,9 +95,10 @@ class Delegate
9395
* calling this method.
9496
* @param[in] skippedArea the area ID to skip.
9597
* @param[out] skipStatusText text describing why the current location cannot be skipped. Max size kMaxSizeStatusText.
96-
* Note: If the return is false and skipStatusText is not successfully set, for example due to a string that can be longer than
97-
* kMaxSizeStatusText, the size of this value should be set to 0 with .reduce_size(0) to avoid callers using un-initialized
98-
* memory.
98+
* Note: skipStatusText must be successfully set if the return is false. Use CopyCharSpanToMutableCharSpanWithTruncation to
99+
* ensure that a message is copied successfully. Otherwise, ensure that if setting the skipStatusText can fail (e.g., due
100+
* to exceeding kMaxSizeStatusText) the size of this value is set to 0 with .reduce_size(0) to avoid callers using
101+
* un-initialized memory.
99102
* @return true if command is successful, false if the received skip request cannot be handled due to the current mode of the
100103
* device.
101104
*
@@ -120,7 +123,7 @@ class Delegate
120123
virtual bool HandleSkipArea(uint32_t skippedArea, MutableCharSpan & skipStatusText)
121124
{
122125
// device support of this command is optional
123-
CopyCharSpanToMutableCharSpan("Skip Current Area command not supported by device"_span, skipStatusText);
126+
CopyCharSpanToMutableCharSpanWithTruncation("Skip Current Area command not supported by device"_span, skipStatusText);
124127
return false;
125128
}
126129

0 commit comments

Comments
 (0)