From cd4d75679094e1f0e13b5ffc24035da32f450d3d Mon Sep 17 00:00:00 2001 From: Bruce Luckcuck Date: Fri, 14 Aug 2020 18:59:47 -0400 Subject: [PATCH] Fix underflow in channel based RSSI calculations for out of range values If the RSSI channel PWM values were outside the expected 1000-2000 range, the `scaleRange()` function does not work properly. In particular if the value was below the minimum the result would be negative. Then this would cause an underflow when applied as a `uint16` to `setRssiDirect()`. This fix constrains the input range from 1000 to 2000. --- src/main/rx/rx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/rx/rx.c b/src/main/rx/rx.c index 95e7a5d5f9..31438d821c 100644 --- a/src/main/rx/rx.c +++ b/src/main/rx/rx.c @@ -757,7 +757,7 @@ static void updateRSSIPWM(void) int16_t pwmRssi = rcData[rxConfig()->rssi_channel - 1]; // Range of rawPwmRssi is [1000;2000]. rssi should be in [0;1023]; - setRssiDirect(scaleRange(pwmRssi, PWM_RANGE_MIN, PWM_RANGE_MAX, 0, RSSI_MAX_VALUE), RSSI_SOURCE_RX_CHANNEL); + setRssiDirect(scaleRange(constrain(pwmRssi, PWM_RANGE_MIN, PWM_RANGE_MAX), PWM_RANGE_MIN, PWM_RANGE_MAX, 0, RSSI_MAX_VALUE), RSSI_SOURCE_RX_CHANNEL); } static void updateRSSIADC(timeUs_t currentTimeUs)