Skip to content

Commit

Permalink
Remove usages of variable length arrays
Browse files Browse the repository at this point in the history
Variable length arrays are not part of the C++ standard and present a risk of
stack overflows in the case of an codeing error. Replace variable length arrays
with a statically sized array large enough to handle the maximum possible input.
  • Loading branch information
Joshua-Anderson committed Jun 24, 2021
1 parent 7b85edc commit 3aa4a12
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Fw/FilePacket/PathName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Fw {

{
const U8* addrLeft = serialBuffer.getBuffAddrLeft();
U8 bytes[this->length];
U8 bytes[MAX_LENGTH];
const SerializeStatus status =
serialBuffer.popBytes(bytes, this->length);
if (status != FW_SERIALIZE_OK)
Expand Down
5 changes: 3 additions & 2 deletions Svc/BufferLogger/BufferLoggerFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,16 @@ namespace Svc {
bool BufferLogger::File ::
writeSize(const U32 size)
{
FW_ASSERT(this->sizeOfSize <= sizeof(U32));
U8 sizeBuffer[sizeof(U32)];
U32 sizeRegister = size;
U8 sizeBuffer[this->sizeOfSize];
for (U8 i = 0; i < this->sizeOfSize; ++i) {
sizeBuffer[this->sizeOfSize - i - 1] = sizeRegister & 0xFF;
sizeRegister >>= 8;
}
const bool status = this->writeBytes(
sizeBuffer,
sizeof(sizeBuffer)
this->sizeOfSize
);
return status;
}
Expand Down
2 changes: 1 addition & 1 deletion Svc/FileDownlink/FileDownlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ namespace Svc {
FW_ASSERT(byteOffset < this->endOffset);
const U32 maxDataSize = FILEDOWNLINK_INTERNAL_BUFFER_SIZE - Fw::FilePacket::DataPacket::HEADERSIZE;
const U32 dataSize = (byteOffset + maxDataSize > this->endOffset) ? (this->endOffset - byteOffset) : maxDataSize;
U8 buffer[dataSize];
U8 buffer[FILEDOWNLINK_INTERNAL_BUFFER_SIZE - Fw::FilePacket::DataPacket::HEADERSIZE];
//This will be last data packet sent
if (dataSize + byteOffset == this->endOffset) {
this->lastCompletedType = Fw::FilePacket::T_DATA;
Expand Down
14 changes: 5 additions & 9 deletions Svc/FileUplink/File.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ======================================================================
// ======================================================================
// \title File.cpp
// \author bocchino
// \brief cpp file for FileUplink::File
Expand All @@ -7,8 +7,8 @@
// Copyright 2009-2016, by the California Institute of Technology.
// ALL RIGHTS RESERVED. United States Government Sponsorship
// acknowledged.
//
// ======================================================================
//
// ======================================================================

#include <Svc/FileUplink/FileUplink.hpp>
#include <Fw/Types/Assert.hpp>
Expand All @@ -18,16 +18,12 @@ namespace Svc {
Os::File::Status FileUplink::File ::
open(const Fw::FilePacket::StartPacket& startPacket)
{
const U32 length = startPacket.destinationPath.length;
char path[length + 1];
memcpy(path, startPacket.destinationPath.value, length);
path[length] = 0;
this->size = startPacket.fileSize;
Fw::LogStringArg logStringArg(path);
Fw::LogStringArg logStringArg(startPacket.destinationPath.value);
this->name = logStringArg;
CFDP::Checksum checksum;
this->checksum = checksum;
return this->osFile.open(path, Os::File::OPEN_WRITE);
return this->osFile.open(startPacket.destinationPath.value, Os::File::OPEN_WRITE);
}

Os::File::Status FileUplink::File ::
Expand Down
10 changes: 5 additions & 5 deletions Svc/GroundInterface/GroundInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ======================================================================
// ======================================================================
// \title GroundInterface.cpp
// \author lestarch
// \brief cpp file for GroundInterface component implementation class
// ======================================================================
// ======================================================================

#include <Fw/Com/ComPacket.hpp>
#include <Svc/GroundInterface/GroundInterface.hpp>
Expand All @@ -16,7 +16,7 @@ namespace Svc {
const U32 GroundInterfaceComponentImpl::END_WORD = static_cast<U32>(0xcafecafe);

// ----------------------------------------------------------------------
// Construction, initialization, and destruction
// Construction, initialization, and destruction
// ----------------------------------------------------------------------

GroundInterfaceComponentImpl ::
Expand All @@ -33,7 +33,7 @@ namespace Svc {
void GroundInterfaceComponentImpl ::
init(
const NATIVE_INT_TYPE instance
)
)
{
GroundInterfaceComponentBase::init(instance);
}
Expand Down Expand Up @@ -129,7 +129,7 @@ namespace Svc {

//read packet descriptor in size agnostic way
U8 packet_descriptor_size = sizeof(FwPacketDescriptorType);
U8 packet_type_bytes[packet_descriptor_size];
U8 packet_type_bytes[sizeof(FwPacketDescriptorType)];
Fw::SerializeStatus stat = m_in_ring.peek(packet_type_bytes, packet_descriptor_size, HEADER_SIZE);
//m_in_ring.peek(packet_type, HEADER_SIZE); // this way is only valid for 4byte packet descriptors
if(stat == Fw::FW_SERIALIZE_OK)
Expand Down

0 comments on commit 3aa4a12

Please sign in to comment.