From ef3e5c4582df70896758e775bed5383b7c415689 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Fri, 5 Jun 2020 18:20:21 +0200 Subject: [PATCH] very important bugfix for serial buffer adapter --- serialize/SerialBufferAdapter.cpp | 39 ++++++++++++------------------- serialize/SerialBufferAdapter.h | 9 +++---- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/serialize/SerialBufferAdapter.cpp b/serialize/SerialBufferAdapter.cpp index e13e7eb6..db91a93f 100644 --- a/serialize/SerialBufferAdapter.cpp +++ b/serialize/SerialBufferAdapter.cpp @@ -3,22 +3,17 @@ #include template -SerialBufferAdapter::SerialBufferAdapter(const void* buffer, +SerialBufferAdapter::SerialBufferAdapter(const uint8_t* buffer, count_t bufferLength, bool serializeLength) : serializeLength(serializeLength), - constBuffer(static_cast(buffer)), m_buffer(nullptr), - bufferLength(bufferLength) { - -} + constBuffer(buffer), buffer(nullptr), + bufferLength(bufferLength) {} template -SerialBufferAdapter::SerialBufferAdapter(void* buffer, +SerialBufferAdapter::SerialBufferAdapter(uint8_t* buffer, count_t bufferLength, bool serializeLength) : - serializeLength(serializeLength), bufferLength(bufferLength) { - uint8_t * member_buffer = static_cast(buffer); - m_buffer = member_buffer; - constBuffer = member_buffer; -} + serializeLength(serializeLength), buffer(buffer), constBuffer(buffer), + bufferLength(bufferLength) {} template @@ -42,8 +37,8 @@ ReturnValue_t SerialBufferAdapter::serialize(uint8_t** buffer, } if (constBuffer != nullptr) { memcpy(*buffer, constBuffer, bufferLength); - } else if (m_buffer != nullptr) { - memcpy(*buffer, m_buffer, bufferLength); + } else if (buffer != nullptr) { + memcpy(*buffer, buffer, bufferLength); } else { return HasReturnvaluesIF::RETURN_FAILED; } @@ -66,14 +61,11 @@ template ReturnValue_t SerialBufferAdapter::deSerialize(const uint8_t** buffer, size_t* size, bool bigEndian) { //TODO Ignores Endian flag! - // (Robin) the one of the buffer? wouldn't that be an issue for serialize - // as well? SerialFixedArrayListAdapter implements swapping of buffer - // fields (if buffer type is not uint8_t) if (buffer != nullptr) { if(serializeLength) { count_t serializedSize = AutoSerializeAdapter::getSerializedSize( &bufferLength); - if(bufferLength + serializedSize >= *size) { + if(bufferLength + serializedSize <= *size) { *buffer += serializedSize; *size -= serializedSize; } @@ -84,7 +76,7 @@ ReturnValue_t SerialBufferAdapter::deSerialize(const uint8_t** buffer, //No Else If, go on with buffer if (bufferLength <= *size) { *size -= bufferLength; - memcpy(m_buffer, *buffer, bufferLength); + memcpy(buffer, *buffer, bufferLength); (*buffer) += bufferLength; return HasReturnvaluesIF::RETURN_OK; } @@ -99,28 +91,27 @@ ReturnValue_t SerialBufferAdapter::deSerialize(const uint8_t** buffer, template uint8_t * SerialBufferAdapter::getBuffer() { - if(m_buffer == nullptr) { + if(buffer == nullptr) { sif::error << "Wrong access function for stored type !" " Use getConstBuffer()" << std::endl; return nullptr; } - return m_buffer; + return buffer; } template const uint8_t * SerialBufferAdapter::getConstBuffer() { if(constBuffer == nullptr) { - sif::error << "Wrong access function for stored type !" - " Use getBuffer()" << std::endl; + sif::error << "SerialBufferAdapter: Buffers are unitialized!" << std::endl; return nullptr; } return constBuffer; } template -void SerialBufferAdapter::setBuffer(void * buffer, +void SerialBufferAdapter::setBuffer(uint8_t* buffer, count_t buffer_length) { - m_buffer = static_cast(buffer); + this->buffer = buffer; bufferLength = buffer_length; } diff --git a/serialize/SerialBufferAdapter.h b/serialize/SerialBufferAdapter.h index d414573c..cb799be5 100644 --- a/serialize/SerialBufferAdapter.h +++ b/serialize/SerialBufferAdapter.h @@ -29,7 +29,7 @@ public: * @param bufferLength * @param serializeLength */ - SerialBufferAdapter(const void* buffer, count_t bufferLength, + SerialBufferAdapter(const uint8_t* buffer, count_t bufferLength, bool serializeLength = false); /** @@ -40,7 +40,8 @@ public: * @param bufferLength * @param serializeLength Length field will be serialized with size count_t */ - SerialBufferAdapter(void* buffer, count_t bufferLength, bool serializeLength = false); + SerialBufferAdapter(uint8_t* buffer, count_t bufferLength, + bool serializeLength = false); virtual ~SerialBufferAdapter(); @@ -66,11 +67,11 @@ public: uint8_t * getBuffer(); const uint8_t * getConstBuffer(); - void setBuffer(void* buffer_, count_t bufferLength_); + void setBuffer(uint8_t* buffer_, count_t bufferLength_); private: bool serializeLength = false; const uint8_t *constBuffer = nullptr; - uint8_t *m_buffer = nullptr; + uint8_t *buffer = nullptr; count_t bufferLength = 0; };