Skip to content

Commit

Permalink
protection logs
Browse files Browse the repository at this point in the history
  • Loading branch information
xlyric committed Nov 21, 2024
1 parent b2f561b commit 81daae4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ constexpr size_t DALLAS_TIMEOUT=1500;
// #define TRIGGER 10
#define TIMERDELAY 5 // delay before switch off

constexpr size_t LOG_MAX_STRING_LENGTH=1800 ;// taille max des logs stockées
constexpr size_t LOG_MAX_STRING_LENGTH=1500 ;// taille max des logs stockées

// activation mode debug
// #define Debug
Expand Down
39 changes: 23 additions & 16 deletions src/config/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#endif

constexpr const int MAX_DALLAS=8; // nombre de sonde Dallas

bool lock_log; // lock log pour éviter les problèmes de mémoire

/// @brief partie délicate car pas mal d'action sur la variable log_init et donc protection de la variable ( pour éviter les pb mémoire )
struct Logs {
Expand All @@ -41,23 +41,30 @@ struct Logs {

/// setter log_init
public: void Set_log_init(String setter, bool logtime=false) {
// Vérifier si la longueur de la chaîne ajoutée ne dépasse pas LOG_MAX_STRING_LENGTH
size_t setterLength = strlen(setter.c_str()); // NOSONAR
size_t logInitLength = strlen(log_init); // NOSONAR
size_t logUptimeLength = strlen(loguptime()); // NOSONAR
size_t maxLength = static_cast<size_t>(MaxString); // NOSONAR

if ( setterLength + logInitLength < maxLength ) {

if ( logtime && ( setterLength + logInitLength + logUptimeLength < maxLength)) {
// protection dépassements de tampon (buffer overflow)
strncat(log_init, loguptime(), maxLength - logInitLength - 1); // NOSONAR
}
// protection dépassements de tampon (buffer overflow)
strncat(log_init, setter.c_str(), maxLength - logInitLength - 1); // NOSONAR
if (lock_log) {
return;
} else {
// Si la taille est trop grande, réinitialiser le log_init
reset_log_init();
lock_log = true;
// Vérifier si la longueur de la chaîne ajoutée ne dépasse pas LOG_MAX_STRING_LENGTH
size_t setterLength = strlen(setter.c_str()); // NOSONAR
size_t logInitLength = strlen(log_init); // NOSONAR
size_t logUptimeLength = strlen(loguptime()); // NOSONAR
size_t maxLength = static_cast<size_t>(MaxString); // NOSONAR

if ( setterLength + logInitLength < maxLength ) {

if ( logtime && ( setterLength + logInitLength + logUptimeLength < maxLength)) {
// protection dépassements de tampon (buffer overflow)
strncat(log_init, loguptime(), maxLength - logInitLength - 1); // NOSONAR
}
// protection dépassements de tampon (buffer overflow)
strncat(log_init, setter.c_str(), maxLength - logInitLength - 1); // NOSONAR
} else {
// Si la taille est trop grande, réinitialiser le log_init
reset_log_init();
}
lock_log = false;
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,6 @@ String routeur="PV-ROUTER";
bool AP = false;
bool discovery_temp;





byte present = 0;

byte data[12]; // NOSONAR
Expand All @@ -215,7 +211,6 @@ int refresh = 60;
int refreshcount = 0;
int deviceCount = 0;


/***************************
* End Settings
**************************/
Expand Down
13 changes: 10 additions & 3 deletions src/tasks/dallas.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ void mqttdallas() {
dallaspresent();
devices_init(); // initialisation des devices HA
}

}



}

//***********************************
Expand Down Expand Up @@ -231,7 +232,7 @@ bool dallaspresent () {
if (!sensors.getAddress(addr[i], i)) Serial.println("Unable to find address for Device 1");
else {
sensors.setResolution(addr[i], TEMPERATURE_PRECISION);
}
}
}

for (int a = 0; a < deviceCount; a++) {
Expand Down Expand Up @@ -281,7 +282,13 @@ bool dallaspresent () {
ds.select(addr[a]);
ds.write(0xBE); // Read Scratchpad


/// cas d'une adresse à 0 0 0 0 0 0 0 0
for (int i = 0; i < deviceCount; i++) {
if (addr[i][0] == 0 && addr[i][1] == 0 && addr[i][2] == 0 && addr[i][3] == 0 && addr[i][4] == 0 && addr[i][5] == 0 && addr[i][6] == 0 && addr[i][7] == 0) {
logging.Set_log_init("Dallas " + String(i) + " : " + "Adresse 0 0 0 0 0 0 0 0" + "\r\n",true);
present = 0;
}
}
}
return true;
}
Expand Down

0 comments on commit 81daae4

Please sign in to comment.