Skip to content

Commit

Permalink
Fixed possibility of non-0-terminated MQTT payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Aircoookie committed May 12, 2021
1 parent cb7b7f1 commit bfb27c4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

### Builds after release 0.12.0

#### Build 2105120

- Fixed possibility of non-0-terminated MQTT payloads
- Fixed two warnings regarding integer comparison

#### Build 2105112

- Usermod settings page no usermods message
Expand Down
2 changes: 1 addition & 1 deletion wled00/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const size_t numBrightnessSteps = sizeof(brightnessSteps) / sizeof(uint8_t);
void incBrightness()
{
// 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)
{
Expand Down
28 changes: 21 additions & 7 deletions wled00/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
DEBUG_PRINTLN(F("no payload -> leave"));
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);
if (strncmp(topic, mqttDeviceTopic, topicPrefixLen) == 0) {
Expand All @@ -73,33 +85,35 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
topic += topicPrefixLen;
} else {
// 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;
}
}

//Prefix is stripped from the topic at this point

if (strcmp_P(topic, PSTR("/col")) == 0) {
colorFromDecOrHexString(col, (char*)payload);
colorFromDecOrHexString(col, (char*)payloadStr);
colorUpdated(NOTIFIER_CALL_MODE_DIRECT_CHANGE);
} else if (strcmp_P(topic, PSTR("/api")) == 0) {
if (payload[0] == '{') { //JSON API
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
deserializeJson(doc, payload);
deserializeJson(doc, payloadStr);
deserializeState(doc.as<JsonObject>());
} else { //HTTP API
String apireq = "win&";
apireq += (char*)payload;
apireq += (char*)payloadStr;
handleSet(nullptr, apireq);
}
} else if (strlen(topic) != 0) {
// non standard topic, check with usermods
usermods.onMqttMessage(topic, payload);
usermods.onMqttMessage(topic, payloadStr);
} else {
// topmost topic (just wled/MAC)
parseMQTTBriPayload(payload);
parseMQTTBriPayload(payloadStr);
}
if (alloc) delete[] payloadStr;
}


Expand Down
2 changes: 1 addition & 1 deletion wled00/overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void _overlayAnalogClock()

void _overlayAnalogCountdown()
{
if (now() < countdownTime)
if ((unsigned long)now() < countdownTime)
{
long diff = countdownTime - now();
double pval = 60;
Expand Down
2 changes: 1 addition & 1 deletion wled00/wled.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

// 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
//#define WLED_USE_MY_CONFIG
Expand Down

0 comments on commit bfb27c4

Please sign in to comment.