- avoid possible buffer overflows by using snprintf

- uniqueId is a field now - optimizes string copy
- store long strings in FLASH memory
This commit is contained in:
Nicu Hodos 2024-10-18 09:14:49 +02:00
parent 138c7b5cd8
commit baa3d2d0b8
2 changed files with 15 additions and 16 deletions

View File

@ -17,8 +17,8 @@ namespace HaESP {
uint8_t hfrag; uint8_t hfrag;
ESP.getHeapStats(&hfree, &hmax, &hfrag); ESP.getHeapStats(&hfree, &hmax, &hfrag);
char value[256]; char value[128];
sprintf(value, "{\"fragmentation\":%d,\"heap\":{\"Heap free\":\"%d B\",\"Heap max free block\":\"%d B\"}}", hfrag, hfree, hmax); snprintf_P(value, sizeof(value), PSTR("{\"fragmentation\":%d,\"heap\":{\"Heap free\":\"%d B\",\"Heap max free block\":\"%d B\"}}"), hfrag, hfree, hmax);
Sensor::mapSensors["heap_fragmentation"]->updateState(value); Sensor::mapSensors["heap_fragmentation"]->updateState(value);
Sensor::mapSensors["heap_free"]->updateState(to_string(hfree).c_str()); Sensor::mapSensors["heap_free"]->updateState(to_string(hfree).c_str());
Sensor::mapSensors["heap_max_free_block"]->updateState(to_string(hmax).c_str()); Sensor::mapSensors["heap_max_free_block"]->updateState(to_string(hmax).c_str());

View File

@ -7,6 +7,7 @@ using namespace std;
#define JSON_SIZE 512 #define JSON_SIZE 512
#define TOPIC_SIZE 255 #define TOPIC_SIZE 255
#define BASE_TOPIC "homeassistant/%s/%s/%s"
namespace Ha { namespace Ha {
uint16_t(*publisher)(const char* topic, const char* message); uint16_t(*publisher)(const char* topic, const char* message);
@ -116,12 +117,13 @@ namespace Ha {
} }
protected: protected:
virtual void buildUniqueId(char* uniqueId) { char uniqueId[50];
sprintf(uniqueId, "%s_%s", MAIN_DEVICE_ID, id); virtual void buildUniqueId() {
snprintf(uniqueId, sizeof(uniqueId), "%s_%s", MAIN_DEVICE_ID, id);
} }
virtual void buildConfigTopic() { virtual void buildConfigTopic() {
sprintf(configTopic, "homeassistant/%s/%s/%s/config", type, MAIN_DEVICE_ID, id); snprintf(configTopic, sizeof(configTopic), BASE_TOPIC"/config", type, MAIN_DEVICE_ID, id);
} }
virtual void buildComponentConfig(JsonDocument& jsonDoc) = 0; virtual void buildComponentConfig(JsonDocument& jsonDoc) = 0;
@ -131,8 +133,7 @@ namespace Ha {
if (entityCategory) jsonDoc["entity_category"] = entityCategory; if (entityCategory) jsonDoc["entity_category"] = entityCategory;
if (deviceClass) jsonDoc["device_class"] = deviceClass; if (deviceClass) jsonDoc["device_class"] = deviceClass;
jsonDoc["name"] = name; jsonDoc["name"] = name;
char uniqueId[50]; buildUniqueId();
buildUniqueId(uniqueId);
jsonDoc["unique_id"] = uniqueId; jsonDoc["unique_id"] = uniqueId;
buildConfigTopic(); buildConfigTopic();
@ -261,7 +262,7 @@ namespace Ha {
static unordered_map<string, Command*> mapCommands; static unordered_map<string, Command*> mapCommands;
Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type), f(f) { Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type), f(f) {
sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id); snprintf(commandTopic, sizeof(commandTopic), BASE_TOPIC"/set", type, MAIN_DEVICE_ID, id);
mapCommands.insert({ string(commandTopic), this }); mapCommands.insert({ string(commandTopic), this });
} }
@ -297,7 +298,7 @@ namespace Ha {
StateConfig(Component* cmp) : cmp(cmp) {} StateConfig(Component* cmp) : cmp(cmp) {}
void withStateTopic() { void withStateTopic() {
sprintf(stateTopic, "homeassistant/%s/%s/%s/state", cmp->type, MAIN_DEVICE_ID, cmp->id); snprintf(stateTopic, sizeof(stateTopic), BASE_TOPIC"/state", cmp->type, MAIN_DEVICE_ID, cmp->id);
} }
void updateState(const char* message) { void updateState(const char* message) {
@ -336,9 +337,7 @@ namespace Ha {
: Command(name, id, "number", f), StateConfig(this), min(min), max(max), step(step) {} : Command(name, id, "number", f), StateConfig(this), min(min), max(max), step(step) {}
void updateState(unsigned int value) { void updateState(unsigned int value) {
char message[32]; StateConfig::updateState(to_string(value).c_str());
sprintf(message, "%u", value);
StateConfig::updateState(message);
} }
void restoreFromState() { void restoreFromState() {
@ -366,17 +365,17 @@ namespace Ha {
} }
protected: protected:
void buildUniqueId(char* uniqueId) override { void buildUniqueId() override {
if (multiValueComponent && deviceClass) { if (multiValueComponent && deviceClass) {
sprintf(uniqueId, "%s_%s_%s", MAIN_DEVICE_ID, deviceClass, id); snprintf(uniqueId, sizeof(uniqueId), "%s_%s_%s", MAIN_DEVICE_ID, deviceClass, id);
} else { } else {
Component::buildUniqueId(uniqueId); Component::buildUniqueId();
} }
} }
void buildConfigTopic() override { void buildConfigTopic() override {
if (multiValueComponent && deviceClass) { if (multiValueComponent && deviceClass) {
sprintf(configTopic, "homeassistant/%s/%s/%s_%s/config", type, MAIN_DEVICE_ID, deviceClass, id); snprintf(configTopic, sizeof(configTopic), BASE_TOPIC"_%s""/config", type, MAIN_DEVICE_ID, deviceClass, id);
} else { } else {
Component::buildConfigTopic(); Component::buildConfigTopic();
} }