1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-23 00:05:33 +03:00

Protect vtxCommonLookupXXX against null vtxDevice call

This commit is contained in:
jflyper 2019-01-17 13:52:51 +09:00
parent 80c1cfa6db
commit 53ab1ad23e

View file

@ -128,30 +128,44 @@ bool vtxCommonGetDeviceCapability(const vtxDevice_t *vtxDevice, vtxDeviceCapabil
const char *vtxCommonLookupBandName(const vtxDevice_t *vtxDevice, int band) const char *vtxCommonLookupBandName(const vtxDevice_t *vtxDevice, int band)
{ {
return vtxDevice->bandNames[band]; if (vtxDevice) {
return vtxDevice->bandNames[band];
} else {
return "?";
}
} }
char vtxCommonLookupBandLetter(const vtxDevice_t *vtxDevice, int band) char vtxCommonLookupBandLetter(const vtxDevice_t *vtxDevice, int band)
{ {
return vtxDevice->bandLetters[band]; if (vtxDevice) {
return vtxDevice->bandLetters[band];
} else {
return '?';
}
} }
const char *vtxCommonLookupChannelName(const vtxDevice_t *vtxDevice, int channel) const char *vtxCommonLookupChannelName(const vtxDevice_t *vtxDevice, int channel)
{ {
return vtxDevice->channelNames[channel]; if (vtxDevice) {
return vtxDevice->channelNames[channel];
} else {
return "?";
}
} }
//Converts frequency (in MHz) to band and channel values. //Converts frequency (in MHz) to band and channel values.
bool vtxCommonLookupBandChan(const vtxDevice_t *vtxDevice, uint16_t freq, uint8_t *pBand, uint8_t *pChannel) bool vtxCommonLookupBandChan(const vtxDevice_t *vtxDevice, uint16_t freq, uint8_t *pBand, uint8_t *pChannel)
{ {
// Use reverse lookup order so that 5880Mhz if (vtxDevice) {
// get Raceband 7 instead of Fatshark 8. // Use reverse lookup order so that 5880Mhz
for (int band = vtxDevice->capability.bandCount - 1 ; band >= 0 ; band--) { // get Raceband 7 instead of Fatshark 8.
for (int channel = 0 ; channel < vtxDevice->capability.channelCount ; channel++) { for (int band = vtxDevice->capability.bandCount - 1 ; band >= 0 ; band--) {
if (vtxDevice->frequencyTable[band * vtxDevice->capability.channelCount + channel] == freq) { for (int channel = 0 ; channel < vtxDevice->capability.channelCount ; channel++) {
*pBand = band + 1; if (vtxDevice->frequencyTable[band * vtxDevice->capability.channelCount + channel] == freq) {
*pChannel = channel + 1; *pBand = band + 1;
return true; *pChannel = channel + 1;
return true;
}
} }
} }
} }
@ -168,21 +182,31 @@ bool vtxCommonLookupBandChan(const vtxDevice_t *vtxDevice, uint16_t freq, uint8_
// Returns frequency value (in MHz), or 0 if band/channel out of range. // Returns frequency value (in MHz), or 0 if band/channel out of range.
uint16_t vtxCommonLookupFrequency(const vtxDevice_t *vtxDevice, int band, int channel) uint16_t vtxCommonLookupFrequency(const vtxDevice_t *vtxDevice, int band, int channel)
{ {
if (vtxDevice && band > 0 && band <= vtxDevice->capability.bandCount && if (vtxDevice) {
channel > 0 && channel <= vtxDevice->capability.channelCount) { if (band > 0 && band <= vtxDevice->capability.bandCount &&
return vtxDevice->frequencyTable[(band - 1) * vtxDevice->capability.channelCount + (channel - 1)]; channel > 0 && channel <= vtxDevice->capability.channelCount) {
return vtxDevice->frequencyTable[(band - 1) * vtxDevice->capability.channelCount + (channel - 1)];
}
} }
return 0; return 0;
} }
const char *vtxCommonLookupPowerName(const vtxDevice_t *vtxDevice, int index) const char *vtxCommonLookupPowerName(const vtxDevice_t *vtxDevice, int index)
{ {
return vtxDevice->powerNames[index]; if (vtxDevice) {
return vtxDevice->powerNames[index];
} else {
return "?";
}
} }
uint16_t vtxCommonLookupPowerValue(const vtxDevice_t *vtxDevice, int index) uint16_t vtxCommonLookupPowerValue(const vtxDevice_t *vtxDevice, int index)
{ {
return vtxDevice->powerValues[index]; if (vtxDevice) {
return vtxDevice->powerValues[index];
} else {
return 0;
}
} }
#endif #endif