very important bugfix for serial buffer adapter

This commit is contained in:
Robin Müller 2020-06-05 18:20:21 +02:00
parent dbeb04895b
commit ef3e5c4582
2 changed files with 20 additions and 28 deletions

View File

@ -3,22 +3,17 @@
#include <cstring> #include <cstring>
template<typename count_t> template<typename count_t>
SerialBufferAdapter<count_t>::SerialBufferAdapter(const void* buffer, SerialBufferAdapter<count_t>::SerialBufferAdapter(const uint8_t* buffer,
count_t bufferLength, bool serializeLength) : count_t bufferLength, bool serializeLength) :
serializeLength(serializeLength), serializeLength(serializeLength),
constBuffer(static_cast<const uint8_t *>(buffer)), m_buffer(nullptr), constBuffer(buffer), buffer(nullptr),
bufferLength(bufferLength) { bufferLength(bufferLength) {}
}
template<typename count_t> template<typename count_t>
SerialBufferAdapter<count_t>::SerialBufferAdapter(void* buffer, SerialBufferAdapter<count_t>::SerialBufferAdapter(uint8_t* buffer,
count_t bufferLength, bool serializeLength) : count_t bufferLength, bool serializeLength) :
serializeLength(serializeLength), bufferLength(bufferLength) { serializeLength(serializeLength), buffer(buffer), constBuffer(buffer),
uint8_t * member_buffer = static_cast<uint8_t *>(buffer); bufferLength(bufferLength) {}
m_buffer = member_buffer;
constBuffer = member_buffer;
}
template<typename count_t> template<typename count_t>
@ -42,8 +37,8 @@ ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer,
} }
if (constBuffer != nullptr) { if (constBuffer != nullptr) {
memcpy(*buffer, constBuffer, bufferLength); memcpy(*buffer, constBuffer, bufferLength);
} else if (m_buffer != nullptr) { } else if (buffer != nullptr) {
memcpy(*buffer, m_buffer, bufferLength); memcpy(*buffer, buffer, bufferLength);
} else { } else {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
@ -66,14 +61,11 @@ template<typename count_t>
ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer, ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
size_t* size, bool bigEndian) { size_t* size, bool bigEndian) {
//TODO Ignores Endian flag! //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 (buffer != nullptr) {
if(serializeLength) { if(serializeLength) {
count_t serializedSize = AutoSerializeAdapter::getSerializedSize( count_t serializedSize = AutoSerializeAdapter::getSerializedSize(
&bufferLength); &bufferLength);
if(bufferLength + serializedSize >= *size) { if(bufferLength + serializedSize <= *size) {
*buffer += serializedSize; *buffer += serializedSize;
*size -= serializedSize; *size -= serializedSize;
} }
@ -84,7 +76,7 @@ ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
//No Else If, go on with buffer //No Else If, go on with buffer
if (bufferLength <= *size) { if (bufferLength <= *size) {
*size -= bufferLength; *size -= bufferLength;
memcpy(m_buffer, *buffer, bufferLength); memcpy(buffer, *buffer, bufferLength);
(*buffer) += bufferLength; (*buffer) += bufferLength;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
@ -99,28 +91,27 @@ ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
template<typename count_t> template<typename count_t>
uint8_t * SerialBufferAdapter<count_t>::getBuffer() { uint8_t * SerialBufferAdapter<count_t>::getBuffer() {
if(m_buffer == nullptr) { if(buffer == nullptr) {
sif::error << "Wrong access function for stored type !" sif::error << "Wrong access function for stored type !"
" Use getConstBuffer()" << std::endl; " Use getConstBuffer()" << std::endl;
return nullptr; return nullptr;
} }
return m_buffer; return buffer;
} }
template<typename count_t> template<typename count_t>
const uint8_t * SerialBufferAdapter<count_t>::getConstBuffer() { const uint8_t * SerialBufferAdapter<count_t>::getConstBuffer() {
if(constBuffer == nullptr) { if(constBuffer == nullptr) {
sif::error << "Wrong access function for stored type !" sif::error << "SerialBufferAdapter: Buffers are unitialized!" << std::endl;
" Use getBuffer()" << std::endl;
return nullptr; return nullptr;
} }
return constBuffer; return constBuffer;
} }
template<typename count_t> template<typename count_t>
void SerialBufferAdapter<count_t>::setBuffer(void * buffer, void SerialBufferAdapter<count_t>::setBuffer(uint8_t* buffer,
count_t buffer_length) { count_t buffer_length) {
m_buffer = static_cast<uint8_t *>(buffer); this->buffer = buffer;
bufferLength = buffer_length; bufferLength = buffer_length;
} }

View File

@ -29,7 +29,7 @@ public:
* @param bufferLength * @param bufferLength
* @param serializeLength * @param serializeLength
*/ */
SerialBufferAdapter(const void* buffer, count_t bufferLength, SerialBufferAdapter(const uint8_t* buffer, count_t bufferLength,
bool serializeLength = false); bool serializeLength = false);
/** /**
@ -40,7 +40,8 @@ public:
* @param bufferLength * @param bufferLength
* @param serializeLength Length field will be serialized with size count_t * @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(); virtual ~SerialBufferAdapter();
@ -66,11 +67,11 @@ public:
uint8_t * getBuffer(); uint8_t * getBuffer();
const uint8_t * getConstBuffer(); const uint8_t * getConstBuffer();
void setBuffer(void* buffer_, count_t bufferLength_); void setBuffer(uint8_t* buffer_, count_t bufferLength_);
private: private:
bool serializeLength = false; bool serializeLength = false;
const uint8_t *constBuffer = nullptr; const uint8_t *constBuffer = nullptr;
uint8_t *m_buffer = nullptr; uint8_t *buffer = nullptr;
count_t bufferLength = 0; count_t bufferLength = 0;
}; };