Skip to content
Snippets Groups Projects
Commit a7a8f6a5 authored by Valerio Pastore's avatar Valerio Pastore
Browse files

fix structure dangling pointer

parent 0687e980
No related branches found
No related tags found
No related merge requests found
......@@ -21,25 +21,26 @@ namespace inaf::oasbo::PacketLib {
class BasePacketStructure {
protected:
using Structure = std::vector<std::tuple<uint, std::string, uint>>;
std::string source;
std::vector<std::tuple<uint, std::string, uint>> structure;
Structure structure;
uint byteSize;
std::vector<uint> fieldSizes;
std::unordered_map<uint, uint> indexToOffsetsMap;
std::unordered_map<std::string, uint> fieldNameToIndexMap;
std::unordered_map<uint, std::string> indexToFieldNameMap;
std::function<Structure(std::string)> sourceReadingFunc;
virtual std::vector<std::tuple<uint, std::string, uint>> readStructureFromSource(
std::string source) = 0;
void updateFieldSizes(
const std::vector<std::tuple<uint, std::string, uint>> &paramsTuple) {
const Structure &paramsTuple) {
std::for_each(paramsTuple.begin(), paramsTuple.end(),
[&](const std::tuple<uint, std::string, uint> &tup) {
this->fieldSizes.push_back(std::get<2>(tup));
});
}
void updateFieldOffsets(
const std::vector<std::tuple<uint, std::string, uint>> &paramsTuple) {
const Structure &paramsTuple) {
uint offset = 0;
for (size_t i = 0; i < paramsTuple.size(); i++) {
indexToOffsetsMap[i] = offset;
......@@ -47,7 +48,7 @@ protected:
}
}
void updateFieldNameAndIndexMap(
const std::vector<std::tuple<uint, std::string, uint>> &paramsTuple) {
const Structure &paramsTuple) {
std::for_each(paramsTuple.begin(), paramsTuple.end(),
[&](const std::tuple<uint, std::string, uint> &tup) {
this->fieldNameToIndexMap[std::get<1>(tup)] = std::get<0>(
......@@ -58,7 +59,7 @@ protected:
}
void updateStructure(std::string source) {
this->source = source;
this->structure = readStructureFromSource(source);
this->structure = sourceReadingFunc(source);
updateFieldSizes(structure);
updateFieldOffsets(structure);
updateFieldNameAndIndexMap(structure);
......@@ -69,7 +70,9 @@ protected:
public:
virtual ~BasePacketStructure() = default;
BasePacketStructure(std::string source) : source(source){};
BasePacketStructure(std::string source, std::function<Structure(std::string)> sourceReadingFunc) : source(source), sourceReadingFunc(sourceReadingFunc){
this->updateStructure(source);
};
BasePacketStructure(const BasePacketStructure &other){
this->source = other.source;
this->byteSize = other.byteSize;
......@@ -77,6 +80,7 @@ public:
this->indexToOffsetsMap = other.indexToOffsetsMap;
this->fieldNameToIndexMap = other.fieldNameToIndexMap;
this->indexToFieldNameMap = other.indexToFieldNameMap;
this->sourceReadingFunc = other.sourceReadingFunc;
}
void changeSource(std::string source) {
......@@ -262,7 +266,7 @@ protected:
public:
BasePacketTempl(BasePacketStructure &structure) {
this->structure = &structure;
this->structure = new BasePacketStructure(structure);
this->binaryPointer = new uint8_t[structure.getByteSize()];
}
......@@ -312,7 +316,7 @@ public:
uint max_writable = this->structure->getByteSize();
if (size > max_writable) {
std::cerr << "Error: you are trying to copy " << size
<< "byte where the max size is: " << max_writable
<< " byte where the max size is: " << max_writable
<< std::endl;
std::cerr << "\tI copy only until " << max_writable << " byte"
<< std::endl;
......@@ -328,13 +332,13 @@ public:
uint max_writable = this->structure->getByteSize();
if (offset > max_writable) {
std::cerr << "Error: you are trying to copy starting from "
<< offset << "byte where the max size is: " << max_writable
<< offset << " byte where the max size is: " << max_writable
<< std::endl;
return -1;
}
if (size + offset > max_writable) {
std::cerr << "Error: you are trying to copy " << size + offset
<< "byte where the max size is: " << max_writable
<< " byte where the max size is: " << max_writable
<< std::endl;
std::cerr << "\tI copy only until " << max_writable << " byte"
<< std::endl;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment