From cd080b159f10ba60312834efc8a8c7e326aaa83d Mon Sep 17 00:00:00 2001 From: rusefillc Date: Wed, 13 Apr 2022 18:22:40 -0400 Subject: [PATCH] logging of live data structs was: data points #3614 support for null fragments --- firmware/console/binary/FragmentEntry.cpp | 7 ++++++- unit_tests/tests/test_scattered_outputs.cpp | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/firmware/console/binary/FragmentEntry.cpp b/firmware/console/binary/FragmentEntry.cpp index 3953a34bfa..0887adda35 100644 --- a/firmware/console/binary/FragmentEntry.cpp +++ b/firmware/console/binary/FragmentEntry.cpp @@ -24,7 +24,12 @@ void copyRange(uint8_t *destination, FragmentEntry *fragments, size_t dataOffset while (dataLength > 0) { int copyNowSize = minI(dataLength, fragments[fragmentIndex].size - dataOffset); - memcpy(destination + destinationIndex, fragments[fragmentIndex].data + dataOffset, copyNowSize); + const uint8_t *fromBase = fragments[fragmentIndex].data; + if (fromBase == nullptr) { + memset(destination + destinationIndex, 0, copyNowSize); + } else { + memcpy(destination + destinationIndex, fromBase + dataOffset, copyNowSize); + } destinationIndex += copyNowSize; dataOffset = 0; dataLength -= copyNowSize; diff --git a/unit_tests/tests/test_scattered_outputs.cpp b/unit_tests/tests/test_scattered_outputs.cpp index c181a4be95..43d5a75c79 100644 --- a/unit_tests/tests/test_scattered_outputs.cpp +++ b/unit_tests/tests/test_scattered_outputs.cpp @@ -7,6 +7,7 @@ static uint8_t buffer5[] = {11, 12, 13, 14, 15}; static FragmentEntry fragments[] = { FragmentEntry(buffer10, sizeof(buffer10)), FragmentEntry(buffer5, sizeof(buffer5)), + FragmentEntry(nullptr, sizeof(5)), }; TEST(outputs, fragments) { @@ -22,4 +23,10 @@ TEST(outputs, fragments) { copyRange(buffer, fragments, 12, 3); EXPECT_TRUE( 0 == std::memcmp(buffer, expected, sizeof(expected))); } + + { + uint8_t expected[] = {15, 0, 0}; + copyRange(buffer, fragments, 14, 3); + EXPECT_TRUE( 0 == std::memcmp(buffer, expected, sizeof(expected))); + } }