Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Memory leaks in MQTT::onReceive #5549

Closed
esev opened this issue Dec 10, 2024 · 5 comments · Fixed by #5592
Closed

[Bug]: Memory leaks in MQTT::onReceive #5549

esev opened this issue Dec 10, 2024 · 5 comments · Fixed by #5592
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@esev
Copy link
Contributor

esev commented Dec 10, 2024

Category

Other

Hardware

Not Applicable

Firmware Version

2.5.16.f81d3b0

Description

I believe there are two potential memory leaks in src/mqtt/MQTT.cpp MQTT::onReceive

  1. return;
    Any pointers within the meshtastic_ServiceEnvelope that are non-NULL are leaked.

  2. if (isToUs(p) || (tx && tx->has_user && rx && rx->has_user))
    The meshtastic_MeshPacket *p is leaked when the if statement evaluates to false

There are several places in MQTT::onReceive where calls to free/release are needed. If I may offer a suggestion; use std::unique_ptr to help ensure these are always freed/released on return

Example:

// make sure to free both strings and the MeshPacket (passing in NULL is acceptable)
struct ServiceEnvelopeCleaner {
    void operator()(meshtastic_ServiceEnvelope *e)
    {
        free(e->channel_id);
        free(e->gateway_id);
        free(e->packet);
    }
};
using ScopedServiceEnvelopeReleaser = std::unique_ptr<meshtastic_ServiceEnvelope, ServiceEnvelopeCleaner>;
struct PacketPoolMeshPacketReleaser {
    void operator()(meshtastic_MeshPacket *p)
    {
        packetPool.release(p);
    }
};
using PacketPoolMeshPacketPtr = std::unique_ptr<meshtastic_MeshPacket, PacketPoolMeshPacketReleaser>;

...

ScopedServiceEnvelopeReleaser cleanup_e(&e);

...

PacketPoolMeshPacketPtr p(packetPool.allocCopy(*e.packet));

...

router->enqueueReceivedMessage(p.release());

Relevant log output

No response

@esev esev added the bug Something isn't working label Dec 10, 2024
@thebentern
Copy link
Contributor

Great analysis! Would you be willing to submit a PR making the proposed changes?

@esev
Copy link
Contributor Author

esev commented Dec 10, 2024

Great analysis! Would you be willing to submit a PR making the proposed changes?

I would love to, and normally I would. But I can't currently sign the CLA due to restrictions at work. I'm working on resolving any conflicts there, but can't send PRs for now.

The above has been resolved. I'll send a PR.

@Matzebhv
Copy link

Same as here -> #5458

@leshniak
Copy link

Thank you @esev for the contribution, I'm already testing your changes on my device.

@Matzebhv
Copy link

I will test as soon as this pull is available in the coming alpha.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants