Skip to content

Commit

Permalink
Fix memory leaks by adding missing free() calls before early return…
Browse files Browse the repository at this point in the history
…s in `MQTT::onReceive` (meshtastic#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.
  • Loading branch information
CTassisF authored and fifieldt committed Dec 14, 2024
1 parent 66e219d commit fe1ed0d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/mqtt/MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -771,4 +780,4 @@ bool MQTT::isPrivateIpAddress(const char address[])

int octet2Num = atoi(octet2);
return octet2Num >= 16 && octet2Num <= 31;
}
}

0 comments on commit fe1ed0d

Please sign in to comment.