mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-12-02 06:10:45 +08:00
fix(vectornav): replace discard swap buffer with in-place memmove
Shifting the remaining bytes with memmove and zeroing the tail is functionally equivalent to the copy-out/zero/copy-back sequence and removes the dynamic allocation entirely, along with the unchecked malloc return. Suggested-by: Beat Küng <beat@auterion.com> Assisted-by: Claude:claude-fable-5 Signed-off-by: Ramon Roche <mrpollo@gmail.com>
This commit is contained in:
1 parent
e312313f77
commit
e7b1d486e5
1 file changed
+3
-11
@@ -74,17 +74,9 @@ void VnSearcher_discardData(char inputData[], size_t dataLength, size_t discardL
|
||||
else
|
||||
{
|
||||
/* Move the data to the front of the array and zero out everything else */
|
||||
/* Size of the data to move */
|
||||
size_t tmpSize = dataLength - discardLength;
|
||||
/* Temporary swap buffer */
|
||||
char* tmp = (char*)malloc(sizeof(char) * tmpSize);
|
||||
/* And the swap operations */
|
||||
/*memcpy_s(tmp, tmpSize, inputData + discardLength, tmpSize);*/
|
||||
memcpy(tmp, inputData + discardLength, tmpSize);
|
||||
memset(inputData, 0, dataLength);
|
||||
/*memcpy_s(inputData, tmpSize, tmp, tmpSize);*/
|
||||
memcpy(inputData, tmp, tmpSize);
|
||||
free(tmp);
|
||||
const size_t newSize = dataLength - discardLength;
|
||||
memmove(inputData, inputData + discardLength, newSize);
|
||||
memset(inputData + newSize, 0, discardLength);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user