Skip to content

Fix: Preventing dereferencing nullptr in case of missing initialisation #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions extras/test/src/test_OTALogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ void simulateOTABinaryReception(OTALogic & ota_logic, ota::OTAData const & ota_t
TEST CODE
**************************************************************************************/

TEST_CASE("No OTA Storage configured", "[OTALogic-01]")
{
/* Perform test */
OTALogic ota_logic;

WHEN("OTALogic::update() is called")
{
ota_logic.update();
THEN("The OTA logic should be in the 'Error' state")
{
REQUIRE(ota_logic.state() == OTAState::Error);
}
THEN("The OTA error should be set to OTAError::NoOTAStorageConfigured")
{
REQUIRE(ota_logic.error() == OTAError::NoOTAStorageConfigured);
}
}
}

/**************************************************************************************/

TEST_CASE("OTAStorage initialisation fails", "[OTAStorage::init() -> returns false]")
{
Mock<OTAStorage> ota_storage;
Expand Down
19 changes: 18 additions & 1 deletion src/utility/ota/OTALogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
******************************************************************************/

OTALogic::OTALogic()
: _ota_storage{nullptr}
: _is_configured{false}
, _ota_storage{nullptr}
, _ota_state{OTAState::Init}
, _ota_error{OTAError::None}
{
Expand All @@ -54,8 +55,24 @@ OTALogic::OTALogic()
* PUBLIC MEMBER FUNCTIONS
******************************************************************************/

void OTALogic::setOTAStorage(OTAStorage & ota_storage)
{
_ota_storage = &ota_storage;
_is_configured = true;
}

OTAError OTALogic::update()
{
/* This if clause should never happen. None the less we
* should insure ourselves against this scenario because
* otherwise we'll have a nullptr dereferencing.
*/
if (!_is_configured) {
_ota_state = OTAState::Error;
_ota_error = OTAError::NoOTAStorageConfigured;
return _ota_error;
}

OTAState prev_ota_state;
/* The purpose of this loop is to allow the transition of
* more than one state per a singular call of 'update'. If
Expand Down
6 changes: 4 additions & 2 deletions src/utility/ota/OTALogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ enum class OTAError : int
StorageWriteFailed = 3,
ChecksumMismatch = 4,
ReceivedDataOverrun = 5,
RenameOfTempFileFailed = 6
RenameOfTempFileFailed = 6,
NoOTAStorageConfigured = 7
};

/******************************************************************************
Expand All @@ -71,7 +72,7 @@ class OTALogic
OTALogic();


inline void setOTAStorage(OTAStorage & ota_storage) { _ota_storage = &ota_storage; }
void setOTAStorage(OTAStorage & ota_storage);


OTAError update();
Expand Down Expand Up @@ -99,6 +100,7 @@ class OTALogic
crc_t crc32;
} sOTABinaryData;

bool _is_configured;
OTAStorage * _ota_storage;
OTAState _ota_state;
OTAError _ota_error;
Expand Down