ESP32MQTTClient icon indicating copy to clipboard operation
ESP32MQTTClient copied to clipboard

memmove crach in onMessageReceivedCallback(const char *topic, char *payload, unsigned int length)

Open AlbertEngelbrechtZA opened this issue 1 year ago • 1 comments

Hi I was getting a memmove exception and i believe you were creating a buffer overrun by doing this payload[strTerminationPos] = '\0'; payload[length] = '\0';

and

payloadStr=String(payload); was creating a memmove exception

void ESP32MQTTClient::onMessageReceivedCallback(const char *topic, char *payload, unsigned int length) {

// Convert the payload into a String
unsigned int strTerminationPos;
if (strlen(topic) + length + 9 >= _mqttMaxInPacketSize)
{
    strTerminationPos = length;

    if (_enableSerialLogs)
        log_i("MQTT! Your message may be truncated, please set setMaxPacketSize() to a higher value.\n");
}
else
    strTerminationPos = length;

// Second, we add the string termination code at the end of the payload and we convert it to a String object
String payloadStr;

if (payload) 
{
 // payload[strTerminationPos] = '\0';
//  payloadStr=String(payload);
  for (std::size_t i = 0; i < length; i++)
  {
      payloadStr += payload[i];
  }
} else 
{
    payloadStr="";
}
String topicStr(topic);
// Logging
if (_enableSerialLogs)
    log_i("MQTT >> [%s] %s\n", topic, payloadStr.c_str());

// Send the message to subscribers
for (std::size_t i = 0; i < _topicSubscriptionList.size(); i++)
{
    if (mqttTopicMatch(_topicSubscriptionList[i].topic, String(topic)))
    {
        if (_topicSubscriptionList[i].callback != NULL)
            _topicSubscriptionList[i].callback(payloadStr); // Call the callback
        if (_topicSubscriptionList[i].callbackWithTopic != NULL)
            _topicSubscriptionList[i].callbackWithTopic(topicStr, payloadStr); // Call the callback
    }
}

}

AlbertEngelbrechtZA avatar Nov 02 '24 12:11 AlbertEngelbrechtZA

Hi, would you please paste your code and raw error? Thanks in advanced.

cyijun avatar Nov 04 '24 10:11 cyijun

@AlbertEngelbrechtZA Please update to the latest code, if it exist you can reopen the issue

dzungpv avatar Jun 22 '25 02:06 dzungpv