Skip to content

Commit 33bde4b

Browse files
authored
Preventing dereferencing nullptr in case of missing initialisation (#161)
1 parent 1c1e33d commit 33bde4b

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

Diff for: extras/test/src/test_OTALogic.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ void simulateOTABinaryReception(OTALogic & ota_logic, ota::OTAData const & ota_t
4949
TEST CODE
5050
**************************************************************************************/
5151

52+
TEST_CASE("No OTA Storage configured", "[OTALogic-01]")
53+
{
54+
/* Perform test */
55+
OTALogic ota_logic;
56+
57+
WHEN("OTALogic::update() is called")
58+
{
59+
ota_logic.update();
60+
THEN("The OTA logic should be in the 'Error' state")
61+
{
62+
REQUIRE(ota_logic.state() == OTAState::Error);
63+
}
64+
THEN("The OTA error should be set to OTAError::NoOTAStorageConfigured")
65+
{
66+
REQUIRE(ota_logic.error() == OTAError::NoOTAStorageConfigured);
67+
}
68+
}
69+
}
70+
71+
/**************************************************************************************/
72+
5273
TEST_CASE("OTAStorage initialisation fails", "[OTAStorage::init() -> returns false]")
5374
{
5475
Mock<OTAStorage> ota_storage;

Diff for: src/utility/ota/OTALogic.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
******************************************************************************/
4343

4444
OTALogic::OTALogic()
45-
: _ota_storage{nullptr}
45+
: _is_configured{false}
46+
, _ota_storage{nullptr}
4647
, _ota_state{OTAState::Init}
4748
, _ota_error{OTAError::None}
4849
{
@@ -54,8 +55,24 @@ OTALogic::OTALogic()
5455
* PUBLIC MEMBER FUNCTIONS
5556
******************************************************************************/
5657

58+
void OTALogic::setOTAStorage(OTAStorage & ota_storage)
59+
{
60+
_ota_storage = &ota_storage;
61+
_is_configured = true;
62+
}
63+
5764
OTAError OTALogic::update()
5865
{
66+
/* This if clause should never happen. None the less we
67+
* should insure ourselves against this scenario because
68+
* otherwise we'll have a nullptr dereferencing.
69+
*/
70+
if (!_is_configured) {
71+
_ota_state = OTAState::Error;
72+
_ota_error = OTAError::NoOTAStorageConfigured;
73+
return _ota_error;
74+
}
75+
5976
OTAState prev_ota_state;
6077
/* The purpose of this loop is to allow the transition of
6178
* more than one state per a singular call of 'update'. If

Diff for: src/utility/ota/OTALogic.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ enum class OTAError : int
5656
StorageWriteFailed = 3,
5757
ChecksumMismatch = 4,
5858
ReceivedDataOverrun = 5,
59-
RenameOfTempFileFailed = 6
59+
RenameOfTempFileFailed = 6,
60+
NoOTAStorageConfigured = 7
6061
};
6162

6263
/******************************************************************************
@@ -71,7 +72,7 @@ class OTALogic
7172
OTALogic();
7273

7374

74-
inline void setOTAStorage(OTAStorage & ota_storage) { _ota_storage = &ota_storage; }
75+
void setOTAStorage(OTAStorage & ota_storage);
7576

7677

7778
OTAError update();
@@ -99,6 +100,7 @@ class OTALogic
99100
crc_t crc32;
100101
} sOTABinaryData;
101102

103+
bool _is_configured;
102104
OTAStorage * _ota_storage;
103105
OTAState _ota_state;
104106
OTAError _ota_error;

0 commit comments

Comments
 (0)