fix: avoid memory leak when possibly malformed packet is received#9781
Merged
thebentern merged 3 commits intomeshtastic:developfrom Mar 1, 2026
Merged
fix: avoid memory leak when possibly malformed packet is received#9781thebentern merged 3 commits intomeshtastic:developfrom
thebentern merged 3 commits intomeshtastic:developfrom
Conversation
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.
thebentern
approved these changes
Feb 28, 2026
Contributor
There was a problem hiding this comment.
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
dummyChannelsentinel withindex = -1. - Updates
Channels::getByIndex()to return the sentinel on invalid index rather than allocating viamalloc().
Comments suppressed due to low confidence (2)
src/mesh/Channels.cpp:315
getByIndex()used tomemset()the fallback channel on every invalid index access. Returning a persistentdummyChannelwithout reinitializing means any accidental writes via the returned non-const reference (e.g.,setChannel()called with an out-of-rangecc.index) can leave stalehas_settings/settingsdata that later malformed packets will observe. Please re-zero the dummy on each invalid access (and then setindex = -1), or keep a function-local static dummy that is reset before returning, to preserve the prior semantics while avoidingmalloc().
LOG_ERROR("Invalid channel index %d > %d, malformed packet received?", chIndex, channelFile.channels_count);
return dummyChannel;
}
src/mesh/Channels.cpp:26
dummyChannelis only used within this translation unit, but as written it has external linkage. Consider giving it internal linkage (e.g.,staticor anonymous namespace), or moving it intogetByIndex()as a function-local static, to avoid exporting an unnecessary global symbol.
meshtastic_Channel dummyChannel = {.index = -1};
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
getByIndexinChannels.cppallocates memory and returns dummy channel wheneverchIndexvalidation fails. Comment implies this may happen when malformed packet is received. The fix changes implementation so static dummyChannel is returned in such case.