diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cfcc5681..f83c5078f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). PR: https://egit.irs.uni-stuttgart.de/fsfw/fsfw/pulls/572 - HAL Devicehandlers: Periodic printout is run-time configurable now - `oneShotAction` flag in the `TestTask` class is not static anymore +- `SimpleRingBuffer::writeData` now checks if the amount is larger than the total size of the + Buffer and rejects such writeData calls with `HasReturnvaluesIF::RETURN_FAILED` + PR: https://egit.irs.uni-stuttgart.de/fsfw/fsfw/pulls/586 - Major update for version handling, using `git describe` to fetch version information with git. PR: https://egit.irs.uni-stuttgart.de/fsfw/fsfw/pulls/601 - Add helper functions provided by [`cmake-modules`](https://github.com/bilke/cmake-modules) diff --git a/src/fsfw/container/SimpleRingBuffer.cpp b/src/fsfw/container/SimpleRingBuffer.cpp index bcf3cf20d..437e72ea1 100644 --- a/src/fsfw/container/SimpleRingBuffer.cpp +++ b/src/fsfw/container/SimpleRingBuffer.cpp @@ -1,4 +1,7 @@ #include "fsfw/container/SimpleRingBuffer.h" +#include "fsfw/FSFW.h" + +#include "fsfw/serviceinterface.h" #include @@ -48,6 +51,19 @@ void SimpleRingBuffer::confirmBytesWritten(size_t amount) { } ReturnValue_t SimpleRingBuffer::writeData(const uint8_t* data, size_t amount) { + if(data == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + if(amount > getMaxSize()) { +#if FSFW_VERBOSE_LEVEL >= 1 +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::error << "SimpleRingBuffer::writeData: Amount of data too large" << std::endl; +#else + sif::printError("SimpleRingBuffer::writeData: Amount of data too large\n"); +#endif +#endif + return HasReturnvaluesIF::RETURN_FAILED; + } if (availableWriteSpace() >= amount or overwriteOld) { size_t amountTillWrap = writeTillWrap(); if (amountTillWrap >= amount) { diff --git a/unittests/container/RingBufferTest.cpp b/unittests/container/RingBufferTest.cpp index ff5db76b4..9ae58388f 100644 --- a/unittests/container/RingBufferTest.cpp +++ b/unittests/container/RingBufferTest.cpp @@ -31,6 +31,8 @@ TEST_CASE("Ring Buffer Test", "[RingBufferTest]") { for (uint8_t i = 0; i < 9; i++) { CHECK(readBuffer[i] == i); } + REQUIRE(ringBuffer.writeData(testData, 1024) == retval::CATCH_FAILED); + REQUIRE(ringBuffer.writeData(nullptr, 5) == retval::CATCH_FAILED); } SECTION("Get Free Element Test") { @@ -144,12 +146,13 @@ TEST_CASE("Ring Buffer Test2", "[RingBufferTest2]") { SECTION("Overflow") { REQUIRE(ringBuffer.availableWriteSpace() == 9); - // Writing more than the buffer is large, technically thats allowed - // But it is senseless and has undesired impact on read call - REQUIRE(ringBuffer.writeData(testData, 13) == retval::CATCH_OK); - REQUIRE(ringBuffer.getAvailableReadData() == 3); + // We don't allow writing of Data that is larger than the ring buffer in total + REQUIRE(ringBuffer.getMaxSize() == 9); + REQUIRE(ringBuffer.writeData(testData, 13) == retval::CATCH_FAILED); + REQUIRE(ringBuffer.getAvailableReadData() == 0); ringBuffer.clear(); uint8_t *ptr = nullptr; + // With excess Bytes 13 Bytes can be written to this Buffer REQUIRE(ringBuffer.getFreeElement(&ptr, 13) == retval::CATCH_OK); REQUIRE(ptr != nullptr); memcpy(ptr, testData, 13); @@ -234,11 +237,13 @@ TEST_CASE("Ring Buffer Test3", "[RingBufferTest3]") { SECTION("Overflow") { REQUIRE(ringBuffer.availableWriteSpace() == 9); - // Writing more than the buffer is large, technically thats allowed - // But it is senseless and has undesired impact on read call - REQUIRE(ringBuffer.writeData(testData, 13) == retval::CATCH_OK); - REQUIRE(ringBuffer.getAvailableReadData() == 3); + // Writing more than the buffer is large. + // This write will be rejected and is seen as a configuration mistake + REQUIRE(ringBuffer.writeData(testData, 13) == retval::CATCH_FAILED); + REQUIRE(ringBuffer.getAvailableReadData() == 0); ringBuffer.clear(); + // Using FreeElement allows the usage of excessBytes but + // should be used with caution uint8_t *ptr = nullptr; REQUIRE(ringBuffer.getFreeElement(&ptr, 13) == retval::CATCH_OK); REQUIRE(ptr != nullptr);