Possibly fix buffer boundaries not being checked

This commit is contained in:
GHOSCHT 2022-02-05 22:23:17 +01:00
parent 6e96e5ca86
commit 73bbc2b1ca
No known key found for this signature in database
GPG key ID: A35BD466B8871994
2 changed files with 32 additions and 7 deletions

View file

@ -3,20 +3,41 @@
#include <Arduino.h> #include <Arduino.h>
#include <EEPROM.h> #include <EEPROM.h>
LightController::LightController(const int bjtPins[], int bjtCount) LightController::LightController(const int bjtPins[], __SIZE_TYPE__ bjtCount)
{ {
this->bjtCount = bjtCount; this->bjtCount = bjtCount;
this->bjtPins = bjtPins; this->bjtPins = bjtPins;
this->bjtState = new int[bjtCount]; this->bjtState = new int[bjtCount];
initializePins();
initializeState();
}
for (int i = 0; i < bjtCount; i++) void LightController::initializeState()
{
initializeStateDefaultValues();
restoreState();
}
void LightController::initializePins()
{
for (__SIZE_TYPE__ i = 0; i < bjtCount; i++)
{
pinMode(bjtPins[i], OUTPUT);
}
}
void LightController::initializeStateDefaultValues()
{
for (__SIZE_TYPE__ i = 0; i < bjtCount && i < sizeof(bjtState) / sizeof(bjtState[0]); i++)
{ {
bjtState[i] = 0; bjtState[i] = 0;
} }
}
for (int i = 0; i < bjtCount; i++) void LightController::restoreState()
{
for (__SIZE_TYPE__ i = 0; i < bjtCount && i < sizeof(bjtState) / sizeof(bjtState[0]); i++)
{ {
pinMode(bjtPins[i], OUTPUT);
bjtState[i] = EEPROM.read(i); bjtState[i] = EEPROM.read(i);
commitPinState(i); commitPinState(i);
} }
@ -24,7 +45,7 @@ LightController::LightController(const int bjtPins[], int bjtCount)
void LightController::updateState(const char data[], int steps) void LightController::updateState(const char data[], int steps)
{ {
for (int i = 0; i < bjtCount; i++) for (__SIZE_TYPE__ i = 0; i < bjtCount; i++)
{ {
parseRelativeState(data, i, steps); parseRelativeState(data, i, steps);
setAbsoluteState(data, i); setAbsoluteState(data, i);

View file

@ -1,7 +1,7 @@
class LightController class LightController
{ {
protected: protected:
int bjtCount; __SIZE_TYPE__ bjtCount;
const int *bjtPins; const int *bjtPins;
int *bjtState; int *bjtState;
@ -11,9 +11,13 @@ private:
int clampState(int stateValue); int clampState(int stateValue);
void commitState(int index); void commitState(int index);
void commitPinState(int index); void commitPinState(int index);
void initializeStateDefaultValues();
void restoreState();
void initializeState();
void initializePins();
public: public:
LightController(const int bjtPins[], int bjtCount); LightController(const int bjtPins[], __SIZE_TYPE__ bjtCount);
void updateState(const char data[], int steps); void updateState(const char data[], int steps);
int getBjtCount(); int getBjtCount();
const int *getBjtPins(); const int *getBjtPins();