From 8d68f0d7eb6a7ee820b345d0ac914f9696fafc49 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Thu, 3 Sep 2026 10:59:31 +0200 Subject: [PATCH 1/2] Prevent invalid custom device registration on allocation failure --- Template/MFCustomDevice.cpp | 9 +++++---- Template/MFCustomDevice.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Template/MFCustomDevice.cpp b/Template/MFCustomDevice.cpp index fb74305..353803d 100644 --- a/Template/MFCustomDevice.cpp +++ b/Template/MFCustomDevice.cpp @@ -67,9 +67,9 @@ MFCustomDevice::MFCustomDevice() will be called ********************************************************************************** */ -void MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfig, bool configFromFlash) +bool MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfig, bool configFromFlash) { - if (adrPin == 0) return; + if (adrPin == 0) return false; /* ********************************************************************************** Do something which is required to setup your custom device @@ -98,7 +98,7 @@ void MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi if (!mem) { // Error Message to Connector cmdMessenger.sendCmd(kStatus, F("Custom Device does not fit in Memory")); - return; + return false; } /* ********************************************************************************************** Read the pins from the EEPROM or Flash, copy them into a buffer @@ -155,7 +155,7 @@ void MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi if (!mem) { // Error Message to Connector cmdMessenger.sendCmd(kStatus, F("Custom Device does not fit in Memory")); - return; + return false; } /* ********************************************************************************************** @@ -208,6 +208,7 @@ void MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi } else { cmdMessenger.sendCmd(kStatus, F("Custom Device is not supported by this firmware version")); } + return true; } /* ********************************************************************************** diff --git a/Template/MFCustomDevice.h b/Template/MFCustomDevice.h index 7ad66f4..fce2abe 100644 --- a/Template/MFCustomDevice.h +++ b/Template/MFCustomDevice.h @@ -12,7 +12,7 @@ class MFCustomDevice { public: MFCustomDevice(); - void attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfig, bool configFromFlash = false); + bool attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfig, bool configFromFlash = false); void detach(); void update(); void set(int16_t messageID, char *setPoint); From 8de5a796683744176438222abecda4d94def726d Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Fri, 4 Sep 2026 05:05:11 +0200 Subject: [PATCH 2/2] more error handling --- Template/MFCustomDevice.cpp | 38 ++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/Template/MFCustomDevice.cpp b/Template/MFCustomDevice.cpp index 353803d..76174df 100644 --- a/Template/MFCustomDevice.cpp +++ b/Template/MFCustomDevice.cpp @@ -84,11 +84,16 @@ bool MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi The string get's NOT stored as this would need a lot of RAM, instead a variable is used to store the type ********************************************************************************** */ - getStringFromMem(adrType, parameter, configFromFlash); + if (!getStringFromMem(adrType, parameter, configFromFlash)) + return false; if (strcmp(parameter, "MOBIFLIGHT_TEMPLATE") == 0) _customType = MY_CUSTOM_DEVICE_1; - if (strcmp(parameter, "MOBIFLIGHT_TEMPLATE2") == 0) + else if (strcmp(parameter, "MOBIFLIGHT_TEMPLATE2") == 0) _customType = MY_CUSTOM_DEVICE_2; + else { + cmdMessenger.sendCmd(kStatus, F("Custom Device is not supported by this firmware version")); + return false; + } if (_customType == MY_CUSTOM_DEVICE_1) { /* ********************************************************************************** @@ -104,22 +109,28 @@ bool MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi Read the pins from the EEPROM or Flash, copy them into a buffer If you have set '"isI2C": true' in the device.json file, the first value is the I2C address ********************************************************************************************** */ - getStringFromMem(adrPin, parameter, configFromFlash); + if (!getStringFromMem(adrPin, parameter, configFromFlash)) + return false; + /* ********************************************************************************************** Split the pins up into single pins. As the number of pins could be different between multiple devices, it is done here. ********************************************************************************************** */ params = strtok_r(parameter, "|", &p); + if (!params) return false; _pin1 = atoi(params); params = strtok_r(NULL, "|", &p); + if (!params) return false; _pin2 = atoi(params); params = strtok_r(NULL, "|", &p); + if (!params) return false; _pin3 = atoi(params); /* ********************************************************************************** Read the configuration from the EEPROM or Flash, copy it into a buffer. ********************************************************************************** */ - getStringFromMem(adrConfig, parameter, configFromFlash); + if (!getStringFromMem(adrConfig, parameter, configFromFlash)) + return false; /* ********************************************************************************** Split the config up into single parameter. As the number of parameters could be different between multiple devices, it is done here. @@ -131,8 +142,10 @@ bool MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi uint16_t Parameter1; char *Parameter2; params = strtok_r(parameter, "|", &p); + if (!params) return false; Parameter1 = atoi(params); params = strtok_r(NULL, "|", &p); + if (!params) return false; Parameter2 = params; /* ********************************************************************************** @@ -162,22 +175,28 @@ bool MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi Read the pins from the EEPROM or Flash, copy them into a buffer If you have set '"isI2C": true' in the device.json file, the first value is the I2C address ********************************************************************************************** */ - getStringFromMem(adrPin, parameter, configFromFlash); + if (!getStringFromMem(adrPin, parameter, configFromFlash)) + return false; + /* ********************************************************************************************** split the pins up into single pins, as the number of pins could be different between multiple devices, it is done here ********************************************************************************************** */ params = strtok_r(parameter, "|", &p); + if (!params) return false; _pin1 = atoi(params); params = strtok_r(NULL, "|", &p); + if (!params) return false; _pin2 = atoi(params); params = strtok_r(NULL, "|", &p); + if (!params) return false; _pin3 = atoi(params); /* ********************************************************************************** Read the configuration from the EEPROM or Flash, copy it into a buffer. ********************************************************************************** */ - getStringFromMem(adrConfig, parameter, configFromFlash); + if (!getStringFromMem(adrConfig, parameter, configFromFlash)) + return false; /* ********************************************************************************** split the config up into single parameter. As the number of parameters could be different between multiple devices, it is done here. @@ -189,8 +208,10 @@ bool MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi uint16_t Parameter1; char *Parameter2; params = strtok_r(parameter, "|", &p); + if (!params) return false; Parameter1 = atoi(params); params = strtok_r(NULL, "|", &p); + if (!params) return false; Parameter2 = params; /* ********************************************************************************** @@ -205,9 +226,8 @@ bool MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi // or this function could be called from the custom constructor or attach() function _mydevice->begin(); _initialized = true; - } else { - cmdMessenger.sendCmd(kStatus, F("Custom Device is not supported by this firmware version")); - } + } + return true; }