avoid memory leak when possibly malformed packet received (#9781)
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. Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
@@ -22,6 +22,8 @@ const char *Channels::serialChannel = "serial";
|
|||||||
const char *Channels::mqttChannel = "mqtt";
|
const char *Channels::mqttChannel = "mqtt";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
meshtastic_Channel dummyChannel = {.index = -1};
|
||||||
|
|
||||||
uint8_t xorHash(const uint8_t *p, size_t len)
|
uint8_t xorHash(const uint8_t *p, size_t len)
|
||||||
{
|
{
|
||||||
uint8_t code = 0;
|
uint8_t code = 0;
|
||||||
@@ -309,13 +311,7 @@ meshtastic_Channel &Channels::getByIndex(ChannelIndex chIndex)
|
|||||||
return *ch;
|
return *ch;
|
||||||
} else {
|
} else {
|
||||||
LOG_ERROR("Invalid channel index %d > %d, malformed packet received?", chIndex, channelFile.channels_count);
|
LOG_ERROR("Invalid channel index %d > %d, malformed packet received?", chIndex, channelFile.channels_count);
|
||||||
|
return dummyChannel;
|
||||||
static meshtastic_Channel *ch = (meshtastic_Channel *)malloc(sizeof(meshtastic_Channel));
|
|
||||||
memset(ch, 0, sizeof(meshtastic_Channel));
|
|
||||||
// ch.index -1 means we don't know the channel locally and need to look it up by settings.name
|
|
||||||
// not sure this is handled right everywhere
|
|
||||||
ch->index = -1;
|
|
||||||
return *ch;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user