Fix memory leaks by adding missing `free()` calls before early returns in `MQTT::onReceive` (#5439)

This fix addresses memory leaks in the `MQTT::onReceive` function by ensuring that dynamically allocated resources (`e.channel_id`, `e.gateway_id` and `e.packet`) are properly freed before each early return. Previously, these resources were only freed at the end of the function, leaving them unhandled in certain exit paths. Adding the missing `free()` calls prevents memory leaks and ensures proper resource cleanup in all scenarios.
pull/5436/head^2
César de Tassis Filho 2024-11-25 01:05:06 -03:00 zatwierdzone przez GitHub
rodzic ad9d7a4972
commit 37da78919a
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 10 dodań i 1 usunięć

Wyświetl plik

@ -167,17 +167,26 @@ void MQTT::onReceive(char *topic, byte *payload, size_t length)
if (isFromUs(p)) {
LOG_INFO("Ignore downlink message we originally sent");
packetPool.release(p);
free(e.channel_id);
free(e.gateway_id);
free(e.packet);
return;
}
if (p->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
if (moduleConfig.mqtt.encryption_enabled) {
LOG_INFO("Ignore decoded message on MQTT, encryption is enabled");
packetPool.release(p);
free(e.channel_id);
free(e.gateway_id);
free(e.packet);
return;
}
if (p->decoded.portnum == meshtastic_PortNum_ADMIN_APP) {
LOG_INFO("Ignore decoded admin packet");
packetPool.release(p);
free(e.channel_id);
free(e.gateway_id);
free(e.packet);
return;
}
p->channel = ch.index;
@ -771,4 +780,4 @@ bool MQTT::isPrivateIpAddress(const char address[])
int octet2Num = atoi(octet2);
return octet2Num >= 16 && octet2Num <= 31;
}
}