better size checks
All checks were successful
EIVE/eive-obsw/pipeline/pr-develop This commit looks good

This commit is contained in:
Robin Müller 2022-08-16 18:37:51 +02:00
parent 5c32114bdb
commit e649d45e3e
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
2 changed files with 54 additions and 18 deletions

View File

@ -237,7 +237,7 @@ class TcMemRead : public TcBase {
uint16_t memLen = 0; uint16_t memLen = 0;
ReturnValue_t lengthCheck(size_t commandDataLen) { ReturnValue_t lengthCheck(size_t commandDataLen) {
if (commandDataLen != COMMAND_LENGTH) { if (commandDataLen != COMMAND_LENGTH or checkPayloadLen() != HasReturnvaluesIF::RETURN_OK) {
return INVALID_LENGTH; return INVALID_LENGTH;
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
@ -264,10 +264,14 @@ class TcMemWrite : public TcBase {
if (result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
std::memcpy(payloadStart, commandData, commandDataLen);
uint16_t memLen = uint16_t memLen =
*(commandData + MEM_ADDRESS_SIZE) << 8 | *(commandData + MEM_ADDRESS_SIZE + 1); *(commandData + MEM_ADDRESS_SIZE) << 8 | *(commandData + MEM_ADDRESS_SIZE + 1);
spParams.setPayloadLen(MIN_FIXED_PAYLOAD_LENGTH + memLen * 4); spParams.setPayloadLen(MIN_FIXED_PAYLOAD_LENGTH + memLen * 4);
result = checkPayloadLen();
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
std::memcpy(payloadStart, commandData, commandDataLen);
return result; return result;
} }
@ -279,7 +283,6 @@ class TcMemWrite : public TcBase {
static const size_t MIN_COMMAND_DATA_LENGTH = MIN_FIXED_PAYLOAD_LENGTH + 4; static const size_t MIN_COMMAND_DATA_LENGTH = MIN_FIXED_PAYLOAD_LENGTH + 4;
ReturnValue_t lengthCheck(size_t commandDataLen) { ReturnValue_t lengthCheck(size_t commandDataLen) {
if (commandDataLen < MIN_COMMAND_DATA_LENGTH) { if (commandDataLen < MIN_COMMAND_DATA_LENGTH) {
sif::warning << "TcMemWrite: Length " << commandDataLen << " smaller than minimum " << sif::warning << "TcMemWrite: Length " << commandDataLen << " smaller than minimum " <<
@ -310,10 +313,14 @@ class FlashFopen : public ploc::SpTcBase {
ReturnValue_t createPacket(std::string filename, char accessMode_) { ReturnValue_t createPacket(std::string filename, char accessMode_) {
accessMode = accessMode_; accessMode = accessMode_;
size_t nameSize = filename.size(); size_t nameSize = filename.size();
spParams.setPayloadLen(nameSize + sizeof(NULL_TERMINATOR) + sizeof(accessMode));
ReturnValue_t result = checkPayloadLen();
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
std::memcpy(payloadStart, filename.c_str(), nameSize); std::memcpy(payloadStart, filename.c_str(), nameSize);
*(spParams.buf + nameSize) = NULL_TERMINATOR; *(spParams.buf + nameSize) = NULL_TERMINATOR;
std::memcpy(payloadStart + nameSize + sizeof(NULL_TERMINATOR), &accessMode, sizeof(accessMode)); std::memcpy(payloadStart + nameSize + sizeof(NULL_TERMINATOR), &accessMode, sizeof(accessMode));
spParams.setPayloadLen(nameSize + sizeof(NULL_TERMINATOR) + sizeof(accessMode));
updateSpFields(); updateSpFields();
return calcCrc(); return calcCrc();
} }
@ -325,16 +332,20 @@ class FlashFopen : public ploc::SpTcBase {
/** /**
* @brief Class to help creation of flash fclose command. * @brief Class to help creation of flash fclose command.
*/ */
class FlashFclose : public TcBase { class FlashFclose : public ploc::SpTcBase {
public: public:
FlashFclose(ploc::SpTcParams params, uint16_t sequenceCount) FlashFclose(ploc::SpTcParams params, uint16_t sequenceCount)
: TcBase(params, apid::TC_FLASHFCLOSE, sequenceCount) {} : ploc::SpTcBase(params, apid::TC_FLASHFCLOSE, sequenceCount) {}
ReturnValue_t createPacket(std::string filename) { ReturnValue_t createPacket(std::string filename) {
size_t nameSize = filename.size(); size_t nameSize = filename.size();
spParams.setPayloadLen(nameSize + sizeof(NULL_TERMINATOR));
ReturnValue_t result = checkPayloadLen();
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
std::memcpy(payloadStart, filename.c_str(), nameSize); std::memcpy(payloadStart, filename.c_str(), nameSize);
*(payloadStart + nameSize) = NULL_TERMINATOR; *(payloadStart + nameSize) = NULL_TERMINATOR;
spParams.setPayloadLen(nameSize + sizeof(NULL_TERMINATOR));
return calcCrc(); return calcCrc();
} }
}; };
@ -354,6 +365,11 @@ class TcFlashWrite : public ploc::SpTcBase {
sif::debug << "FlashWrite::createPacket: Command data too big" << std::endl; sif::debug << "FlashWrite::createPacket: Command data too big" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
spParams.setPayloadLen(static_cast<uint16_t>(writeLen) + 4);
result = checkPayloadLen();
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
size_t serializedSize = 0; size_t serializedSize = 0;
result = SerializeAdapter::serialize(&writeLen, payloadStart, &serializedSize, sizeof(writeLen), result = SerializeAdapter::serialize(&writeLen, payloadStart, &serializedSize, sizeof(writeLen),
SerializeIF::Endianness::BIG); SerializeIF::Endianness::BIG);
@ -361,7 +377,6 @@ class TcFlashWrite : public ploc::SpTcBase {
return result; return result;
} }
std::memcpy(payloadStart + sizeof(writeLen), writeData, writeLen); std::memcpy(payloadStart + sizeof(writeLen), writeData, writeLen);
spParams.setPayloadLen(static_cast<uint16_t>(writeLen) + 4);
updateSpFields(); updateSpFields();
auto res = checkSizeAndSerializeHeader(); auto res = checkSizeAndSerializeHeader();
if (res != result::OK) { if (res != result::OK) {
@ -384,11 +399,16 @@ class TcFlashDelete : public ploc::SpTcBase {
ReturnValue_t buildPacket(std::string filename) { ReturnValue_t buildPacket(std::string filename) {
size_t nameSize = filename.size(); size_t nameSize = filename.size();
spParams.setPayloadLen(nameSize + sizeof(NULL_TERMINATOR));
auto res = checkPayloadLen();
if(res != HasReturnvaluesIF::RETURN_OK) {
return res;
}
std::memcpy(payloadStart, filename.c_str(), nameSize); std::memcpy(payloadStart, filename.c_str(), nameSize);
*(payloadStart + nameSize) = NULL_TERMINATOR; *(payloadStart + nameSize) = NULL_TERMINATOR;
spParams.setPayloadLen(nameSize + sizeof(NULL_TERMINATOR));
updateSpFields(); updateSpFields();
auto res = checkSizeAndSerializeHeader(); res = checkSizeAndSerializeHeader();
if (res != result::OK) { if (res != result::OK) {
return res; return res;
} }
@ -419,6 +439,7 @@ class TcReplayStart : public TcBase {
protected: protected:
ReturnValue_t initPacket(const uint8_t* commandData, size_t commandDataLen) override { ReturnValue_t initPacket(const uint8_t* commandData, size_t commandDataLen) override {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
spParams.setPayloadLen(commandDataLen);
result = lengthCheck(commandDataLen); result = lengthCheck(commandDataLen);
if (result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
@ -428,7 +449,6 @@ class TcReplayStart : public TcBase {
return result; return result;
} }
std::memcpy(payloadStart, commandData, commandDataLen); std::memcpy(payloadStart, commandData, commandDataLen);
spParams.setPayloadLen(commandDataLen);
return result; return result;
} }
@ -438,7 +458,7 @@ class TcReplayStart : public TcBase {
static const uint8_t ONCE = 1; static const uint8_t ONCE = 1;
ReturnValue_t lengthCheck(size_t commandDataLen) { ReturnValue_t lengthCheck(size_t commandDataLen) {
if (commandDataLen != COMMAND_DATA_LENGTH) { if (commandDataLen != COMMAND_DATA_LENGTH or checkPayloadLen() != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "TcReplayStart: Command has invalid length " << commandDataLen << std::endl; sif::warning << "TcReplayStart: Command has invalid length " << commandDataLen << std::endl;
return INVALID_LENGTH; return INVALID_LENGTH;
} }
@ -480,9 +500,13 @@ class TcDownlinkPwrOn : public TcBase {
if (result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
spParams.setPayloadLen(commandDataLen + sizeof(MAX_AMPLITUDE));
result = checkPayloadLen();
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
std::memcpy(payloadStart, commandData, commandDataLen); std::memcpy(payloadStart, commandData, commandDataLen);
std::memcpy(payloadStart + commandDataLen, &MAX_AMPLITUDE, sizeof(MAX_AMPLITUDE)); std::memcpy(payloadStart + commandDataLen, &MAX_AMPLITUDE, sizeof(MAX_AMPLITUDE));
spParams.setPayloadLen(commandDataLen + sizeof(MAX_AMPLITUDE));
return result; return result;
} }
@ -547,13 +571,13 @@ class TcReplayWriteSeq : public TcBase {
protected: protected:
ReturnValue_t initPacket(const uint8_t* commandData, size_t commandDataLen) override { ReturnValue_t initPacket(const uint8_t* commandData, size_t commandDataLen) override {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
spParams.setPayloadLen(commandDataLen + sizeof(NULL_TERMINATOR));
result = lengthCheck(commandDataLen); result = lengthCheck(commandDataLen);
if (result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
std::memcpy(payloadStart, commandData, commandDataLen); std::memcpy(payloadStart, commandData, commandDataLen);
*(payloadStart + commandDataLen) = NULL_TERMINATOR; *(payloadStart + commandDataLen) = NULL_TERMINATOR;
spParams.setPayloadLen(commandDataLen + sizeof(NULL_TERMINATOR));
return result; return result;
} }
@ -561,7 +585,8 @@ class TcReplayWriteSeq : public TcBase {
static const size_t USE_DECODING_LENGTH = 1; static const size_t USE_DECODING_LENGTH = 1;
ReturnValue_t lengthCheck(size_t commandDataLen) { ReturnValue_t lengthCheck(size_t commandDataLen) {
if (commandDataLen > USE_DECODING_LENGTH + MAX_FILENAME_SIZE) { if (commandDataLen > USE_DECODING_LENGTH + MAX_FILENAME_SIZE or
checkPayloadLen() != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "TcReplayWriteSeq: Command has invalid length " << commandDataLen sif::warning << "TcReplayWriteSeq: Command has invalid length " << commandDataLen
<< std::endl; << std::endl;
return INVALID_LENGTH; return INVALID_LENGTH;
@ -632,12 +657,17 @@ class TcCamcmdSend : public TcBase {
return INVALID_LENGTH; return INVALID_LENGTH;
} }
uint16_t dataLen = static_cast<uint16_t>(commandDataLen + sizeof(CARRIAGE_RETURN)); uint16_t dataLen = static_cast<uint16_t>(commandDataLen + sizeof(CARRIAGE_RETURN));
spParams.setPayloadLen(sizeof(dataLen) + commandDataLen + sizeof(CARRIAGE_RETURN));
auto res = checkPayloadLen();
if(res != HasReturnvaluesIF::RETURN_OK) {
return res;
}
size_t size = sizeof(dataLen); size_t size = sizeof(dataLen);
SerializeAdapter::serialize(&dataLen, payloadStart, &size, sizeof(dataLen), SerializeAdapter::serialize(&dataLen, payloadStart, &size, sizeof(dataLen),
SerializeIF::Endianness::BIG); SerializeIF::Endianness::BIG);
std::memcpy(payloadStart + sizeof(dataLen), commandData, commandDataLen); std::memcpy(payloadStart + sizeof(dataLen), commandData, commandDataLen);
*(payloadStart + sizeof(dataLen) + commandDataLen) = CARRIAGE_RETURN; *(payloadStart + sizeof(dataLen) + commandDataLen) = CARRIAGE_RETURN;
spParams.setPayloadLen(sizeof(dataLen) + commandDataLen + sizeof(CARRIAGE_RETURN));
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }

View File

@ -1101,7 +1101,7 @@ class WriteMemory : public ploc::SpTcBase {
static const uint16_t META_DATA_LENGTH = 8; static const uint16_t META_DATA_LENGTH = 8;
uint8_t n = 1; uint8_t n = 1;
void initPacket(uint8_t memoryId, uint32_t startAddr, uint16_t updateDataLen, ReturnValue_t initPacket(uint8_t memoryId, uint32_t startAddr, uint16_t updateDataLen,
uint8_t* updateData) { uint8_t* updateData) {
size_t serializedSize = 0; size_t serializedSize = 0;
uint8_t* data = payloadStart; uint8_t* data = payloadStart;
@ -1113,7 +1113,6 @@ class WriteMemory : public ploc::SpTcBase {
SerializeIF::Endianness::BIG); SerializeIF::Endianness::BIG);
SerializeAdapter::serialize(&updateDataLen, &data, &serializedSize, sizeof(updateDataLen), SerializeAdapter::serialize(&updateDataLen, &data, &serializedSize, sizeof(updateDataLen),
SerializeIF::Endianness::BIG); SerializeIF::Endianness::BIG);
std::memcpy(data, updateData, updateDataLen);
if (updateDataLen % 2 != 0) { if (updateDataLen % 2 != 0) {
spParams.setPayloadLen(META_DATA_LENGTH + updateDataLen + 1); spParams.setPayloadLen(META_DATA_LENGTH + updateDataLen + 1);
// The data field must be two bytes aligned. Thus, in case the number of bytes to write is odd // The data field must be two bytes aligned. Thus, in case the number of bytes to write is odd
@ -1122,6 +1121,13 @@ class WriteMemory : public ploc::SpTcBase {
} else { } else {
spParams.setPayloadLen(META_DATA_LENGTH + updateDataLen); spParams.setPayloadLen(META_DATA_LENGTH + updateDataLen);
} }
// To avoid crashes in this unexpected case
ReturnValue_t result = checkPayloadLen();
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
std::memcpy(data, updateData, updateDataLen);
return HasReturnvaluesIF::RETURN_OK;
} }
}; };