From 2b7f2d4744bac0c3aac2eb28603d2d4fb1eee2d7 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Tue, 11 May 2021 16:20:43 +0200 Subject: [PATCH] MQTT null termination fix. Nigtttime detection for PIR fix. --- .../usermod_PIR_sensor_switch.h | 7 +----- wled00/mqtt.cpp | 23 +++++++++++++------ wled00/wled.h | 2 +- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h b/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h index cf57fff2..23635d0c 100644 --- a/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h +++ b/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h @@ -108,20 +108,15 @@ private: uint8_t hr = hour(localTime); uint8_t mi = minute(localTime); - DEBUG_PRINTF("--Time: %02d:%02d\n", (int)hr, (int)mi); if (sunrise && sunset) { - DEBUG_PRINTLN(F("--Sunrise & sunset detected.")); - if (hour(sunrise)>hr && hour(sunset)hr) { isDayTime = true; - DEBUG_PRINTLN(F("--It is daytime.")); } else { if (hour(sunrise)==hr && minute(sunrise)mi) { isDayTime = true; - DEBUG_PRINTLN(F("--It is daytime.")); } } } diff --git a/wled00/mqtt.cpp b/wled00/mqtt.cpp index 9e04ebe5..2754e940 100644 --- a/wled00/mqtt.cpp +++ b/wled00/mqtt.cpp @@ -64,6 +64,13 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties } DEBUG_PRINTLN(payload); + // payload is not always null terminated + char *payload0 = new char[len+1]; + if (payload0==nullptr) return; // out of memory + strncpy(payload0,payload,len); + payload0[len] = '\0'; + DEBUG_PRINTLN(payload0); + size_t topicPrefixLen = strlen(mqttDeviceTopic); if (strncmp(topic, mqttDeviceTopic, topicPrefixLen) == 0) { topic += topicPrefixLen; @@ -73,7 +80,8 @@ 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, payload0); + delete[] payload0; return; } } @@ -81,25 +89,26 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties //Prefix is stripped from the topic at this point if (strcmp_P(topic, PSTR("/col")) == 0) { - colorFromDecOrHexString(col, (char*)payload); + colorFromDecOrHexString(col, payload0); colorUpdated(NOTIFIER_CALL_MODE_DIRECT_CHANGE); } else if (strcmp_P(topic, PSTR("/api")) == 0) { - if (payload[0] == '{') { //JSON API + if (payload0[0] == '{') { //JSON API DynamicJsonDocument doc(JSON_BUFFER_SIZE); - deserializeJson(doc, payload); + deserializeJson(doc, payload0); deserializeState(doc.as()); } else { //HTTP API String apireq = "win&"; - apireq += (char*)payload; + apireq += payload0; handleSet(nullptr, apireq); } } else if (strlen(topic) != 0) { // non standard topic, check with usermods - usermods.onMqttMessage(topic, payload); + usermods.onMqttMessage(topic, payload0); } else { // topmost topic (just wled/MAC) - parseMQTTBriPayload(payload); + parseMQTTBriPayload(payload0); } + delete[] payload0; } diff --git a/wled00/wled.h b/wled00/wled.h index 506a08c4..382b1047 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2105111 +#define VERSION 2105112 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG