Always copy MQTT payload

This commit is contained in:
cschwinne 2021-05-17 10:38:07 +02:00
parent bfb27c49a2
commit e0f17e1778
2 changed files with 7 additions and 14 deletions

View File

@ -62,17 +62,10 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
DEBUG_PRINTLN(F("no payload -> leave")); DEBUG_PRINTLN(F("no payload -> leave"));
return; return;
} }
char* payloadStr; //make a copy of the payload to 0-terminate it
bool alloc = false; char* payloadStr = new char[len+1];
// check if payload is 0-terminated
if (payload[len-1] == '\0') {
payloadStr = payload;
} else {
payloadStr = new char[len+1];
strncpy(payloadStr, payload, len); strncpy(payloadStr, payload, len);
payloadStr[len] = '\0'; payloadStr[len] = '\0';
alloc = true;
}
if (payloadStr == nullptr) return; //no mem if (payloadStr == nullptr) return; //no mem
DEBUG_PRINTLN(payloadStr); DEBUG_PRINTLN(payloadStr);
@ -86,7 +79,7 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
} else { } else {
// Non-Wled Topic used here. Probably a usermod subscribed to this topic. // Non-Wled Topic used here. Probably a usermod subscribed to this topic.
usermods.onMqttMessage(topic, payloadStr); usermods.onMqttMessage(topic, payloadStr);
if (alloc) delete[] payloadStr; delete[] payloadStr;
return; return;
} }
} }
@ -113,7 +106,7 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
// topmost topic (just wled/MAC) // topmost topic (just wled/MAC)
parseMQTTBriPayload(payloadStr); parseMQTTBriPayload(payloadStr);
} }
if (alloc) delete[] payloadStr; delete[] payloadStr;
} }

View File

@ -8,7 +8,7 @@
*/ */
// version code in format yymmddb (b = daily build) // version code in format yymmddb (b = daily build)
#define VERSION 2105120 #define VERSION 2105170
//uncomment this if you have a "my_config.h" file you'd like to use //uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG //#define WLED_USE_MY_CONFIG