Fixed possibility of non-0-terminated MQTT payloads

This commit is contained in:
cschwinne 2021-05-13 01:04:33 +02:00
parent cb7b7f1dca
commit bfb27c49a2
5 changed files with 29 additions and 10 deletions

View file

@ -2,6 +2,11 @@
### Builds after release 0.12.0 ### Builds after release 0.12.0
#### Build 2105120
- Fixed possibility of non-0-terminated MQTT payloads
- Fixed two warnings regarding integer comparison
#### Build 2105112 #### Build 2105112
- Usermod settings page no usermods message - Usermod settings page no usermods message

View file

@ -42,7 +42,7 @@ const size_t numBrightnessSteps = sizeof(brightnessSteps) / sizeof(uint8_t);
void incBrightness() void incBrightness()
{ {
// dumb incremental search is efficient enough for so few items // dumb incremental search is efficient enough for so few items
for (int index = 0; index < numBrightnessSteps; ++index) for (uint8_t index = 0; index < numBrightnessSteps; ++index)
{ {
if (brightnessSteps[index] > bri) if (brightnessSteps[index] > bri)
{ {

View file

@ -62,7 +62,19 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
DEBUG_PRINTLN(F("no payload -> leave")); DEBUG_PRINTLN(F("no payload -> leave"));
return; return;
} }
DEBUG_PRINTLN(payload); char* payloadStr;
bool alloc = false;
// check if payload is 0-terminated
if (payload[len-1] == '\0') {
payloadStr = payload;
} else {
payloadStr = new char[len+1];
strncpy(payloadStr, payload, len);
payloadStr[len] = '\0';
alloc = true;
}
if (payloadStr == nullptr) return; //no mem
DEBUG_PRINTLN(payloadStr);
size_t topicPrefixLen = strlen(mqttDeviceTopic); size_t topicPrefixLen = strlen(mqttDeviceTopic);
if (strncmp(topic, mqttDeviceTopic, topicPrefixLen) == 0) { if (strncmp(topic, mqttDeviceTopic, topicPrefixLen) == 0) {
@ -73,7 +85,8 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
topic += topicPrefixLen; topic += topicPrefixLen;
} 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, payload); usermods.onMqttMessage(topic, payloadStr);
if (alloc) delete[] payloadStr;
return; return;
} }
} }
@ -81,25 +94,26 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
//Prefix is stripped from the topic at this point //Prefix is stripped from the topic at this point
if (strcmp_P(topic, PSTR("/col")) == 0) { if (strcmp_P(topic, PSTR("/col")) == 0) {
colorFromDecOrHexString(col, (char*)payload); colorFromDecOrHexString(col, (char*)payloadStr);
colorUpdated(NOTIFIER_CALL_MODE_DIRECT_CHANGE); colorUpdated(NOTIFIER_CALL_MODE_DIRECT_CHANGE);
} else if (strcmp_P(topic, PSTR("/api")) == 0) { } else if (strcmp_P(topic, PSTR("/api")) == 0) {
if (payload[0] == '{') { //JSON API if (payload[0] == '{') { //JSON API
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
deserializeJson(doc, payload); deserializeJson(doc, payloadStr);
deserializeState(doc.as<JsonObject>()); deserializeState(doc.as<JsonObject>());
} else { //HTTP API } else { //HTTP API
String apireq = "win&"; String apireq = "win&";
apireq += (char*)payload; apireq += (char*)payloadStr;
handleSet(nullptr, apireq); handleSet(nullptr, apireq);
} }
} else if (strlen(topic) != 0) { } else if (strlen(topic) != 0) {
// non standard topic, check with usermods // non standard topic, check with usermods
usermods.onMqttMessage(topic, payload); usermods.onMqttMessage(topic, payloadStr);
} else { } else {
// topmost topic (just wled/MAC) // topmost topic (just wled/MAC)
parseMQTTBriPayload(payload); parseMQTTBriPayload(payloadStr);
} }
if (alloc) delete[] payloadStr;
} }

View file

@ -79,7 +79,7 @@ void _overlayAnalogClock()
void _overlayAnalogCountdown() void _overlayAnalogCountdown()
{ {
if (now() < countdownTime) if ((unsigned long)now() < countdownTime)
{ {
long diff = countdownTime - now(); long diff = countdownTime - now();
double pval = 60; double pval = 60;

View file

@ -8,7 +8,7 @@
*/ */
// version code in format yymmddb (b = daily build) // version code in format yymmddb (b = daily build)
#define VERSION 2105112 #define VERSION 2105120
//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