Skip to content

fix: avoid memory leak when possibly malformed packet is received#9781

Merged
thebentern merged 3 commits intomeshtastic:developfrom
m1nl:fix_channels_memory_leak
Mar 1, 2026
Merged

fix: avoid memory leak when possibly malformed packet is received#9781
thebentern merged 3 commits intomeshtastic:developfrom
m1nl:fix_channels_memory_leak

Conversation

@m1nl
Copy link
Contributor

@m1nl m1nl commented Feb 28, 2026

getByIndex in Channels.cpp allocates memory and returns dummy channel whenever chIndex validation fails. Comment implies this may happen when malformed packet is received. The fix changes implementation so static dummyChannel is returned in such case.

  • I have tested that my proposed changes behave as described.
  • I have tested that my proposed changes do not cause any obvious regressions on the following devices:
    • Heltec (Lora32) V3
    • LilyGo T-Deck
    • LilyGo T-Beam
    • RAK WisBlock 4631
    • Seeed Studio T-1000E tracker card
    • Other (please specify below)

getByIndex allocates memory and returns dummy channel whenever
chIndex validation fails. Comment implies this may happen
when malformed packet is received. The fix changes implementation
so static dummyChannel is returned in such case.
@github-actions github-actions bot added the bugfix Pull request that fixes bugs label Feb 28, 2026
@m1nl m1nl changed the title fix: avoid memory leak when possibly malformed packet received fix: avoid memory leak when possibly malformed packet is received Feb 28, 2026
@thebentern thebentern requested a review from Copilot February 28, 2026 23:16
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the channel lookup fallback path to avoid dynamically allocating a “dummy” meshtastic_Channel when an invalid channel index is encountered (e.g., due to a malformed packet), returning a static sentinel channel instead.

Changes:

  • Introduces a dummyChannel sentinel with index = -1.
  • Updates Channels::getByIndex() to return the sentinel on invalid index rather than allocating via malloc().
Comments suppressed due to low confidence (2)

src/mesh/Channels.cpp:315

  • getByIndex() used to memset() the fallback channel on every invalid index access. Returning a persistent dummyChannel without reinitializing means any accidental writes via the returned non-const reference (e.g., setChannel() called with an out-of-range cc.index) can leave stale has_settings/settings data that later malformed packets will observe. Please re-zero the dummy on each invalid access (and then set index = -1), or keep a function-local static dummy that is reset before returning, to preserve the prior semantics while avoiding malloc().
        LOG_ERROR("Invalid channel index %d > %d, malformed packet received?", chIndex, channelFile.channels_count);
        return dummyChannel;
    }

src/mesh/Channels.cpp:26

  • dummyChannel is only used within this translation unit, but as written it has external linkage. Consider giving it internal linkage (e.g., static or anonymous namespace), or moving it into getByIndex() as a function-local static, to avoid exporting an unnecessary global symbol.
meshtastic_Channel dummyChannel = {.index = -1};

@thebentern thebentern merged commit 80af726 into meshtastic:develop Mar 1, 2026
22 of 70 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Pull request that fixes bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants