From c79c6723131e631f58d8eefb7f414ac79a2de259 Mon Sep 17 00:00:00 2001 From: Dual Tachyon Date: Thu, 7 Sep 2023 13:02:32 +0100 Subject: [PATCH] Prevent potential UB. --- app/main.c | 2 +- frequencies.c | 15 +++++++-------- radio.c | 4 ++-- ui/helper.c | 2 +- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/main.c b/app/main.c index 091aca8..726454b 100644 --- a/app/main.c +++ b/app/main.c @@ -113,7 +113,7 @@ static void MAIN_Key_DIGITS(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) } gInputBoxIndex = 0; Channel = (gInputBox[0] * 10) + gInputBox[1]; - if ((Channel - 1) < 10) { + if (Channel >= 1 && Channel <= 10) { Channel += NOAA_CHANNEL_FIRST; gAnotherVoiceID = (VOICE_ID_t)Key; gEeprom.NoaaChannel[Vfo] = Channel; diff --git a/frequencies.c b/frequencies.c index 0ad34b5..c7a8496 100644 --- a/frequencies.c +++ b/frequencies.c @@ -72,29 +72,28 @@ const uint16_t StepFrequencyTable[6] = { FREQUENCY_Band_t FREQUENCY_GetBand(uint32_t Frequency) { - if ((Frequency - 5000000) < 2600001) { + if (Frequency >= 5000000 && Frequency <= 7600000) { return BAND1_50MHz; } - if ((Frequency - 10800000) < 2799991) { + if (Frequency >= 10800000 && Frequency <= 13599990) { return BAND2_108MHz; } - if ((Frequency - 13600000) < 3799991) { + if (Frequency >= 13600000 && Frequency <= 17399990) { return BAND3_136MHz; } - if ((Frequency - 17400000) < 17599991) { + if (Frequency >= 17400000 && Frequency <= 34999990) { return BAND4_174MHz; } - if ((Frequency - 35000000) < 4999991) { + if (Frequency >= 35000000 && Frequency <= 39999990) { return BAND5_350MHz; } - if ((Frequency - 40000000) < 6999991) { + if (Frequency >= 40000000 && Frequency <= 46999990) { return BAND6_400MHz; } - if ((Frequency - 47000000) < 13000001) { + if (Frequency >= 47000000 && Frequency <= 60000000) { return BAND7_470MHz; } - // TODO: Double check the assembly return BAND6_400MHz; } diff --git a/radio.c b/radio.c index 88130c4..67fdfd3 100644 --- a/radio.c +++ b/radio.c @@ -320,7 +320,7 @@ void RADIO_ConfigureChannel(uint8_t VFO, uint32_t Arg) } pRadio->ConfigRX.Frequency = Frequency; - if (Frequency - 10800000 < 2799991) { + if (Frequency >= 10800000 && Frequency <= 13599990) { gEeprom.VfoInfo[VFO].FREQUENCY_DEVIATION_SETTING = FREQUENCY_DEVIATION_OFF; } else if (!IS_MR_CHANNEL(Channel)) { Frequency = FREQUENCY_FloorToStep(gEeprom.VfoInfo[VFO].FREQUENCY_OF_DEVIATION, gEeprom.VfoInfo[VFO].StepFrequency, 0); @@ -344,7 +344,7 @@ void RADIO_ConfigureChannel(uint8_t VFO, uint32_t Arg) if (!gSetting_350EN) { FREQ_Config_t *pConfig = gEeprom.VfoInfo[VFO].pCurrent; - if (pConfig->Frequency - 35000000 < 4999991) { + if (pConfig->Frequency >= 35000000 && pConfig->Frequency <= 39999990) { pConfig->Frequency = 41001250; } } diff --git a/ui/helper.c b/ui/helper.c index e731d91..ed1f74a 100644 --- a/ui/helper.c +++ b/ui/helper.c @@ -79,7 +79,7 @@ void UI_PrintString(const char *pString, uint8_t Start, uint8_t End, uint8_t Lin Start += (((End - Start) - (Length * Width)) + 1) / 2; } for (i = 0; i < Length; i++) { - if (pString[i] - ' ' < 0x5F) { + if (pString[i] >= ' ' && pString[i] < 0x7F) { uint8_t Index = pString[i] - ' '; memcpy(gFrameBuffer[Line + 0] + (i * Width) + Start, &gFontBig[Index][0], 8); memcpy(gFrameBuffer[Line + 1] + (i * Width) + Start, &gFontBig[Index][8], 8);