Skip to content

Fix dynamic power headroom underflow when config power lowered while armed - #3639

Merged
pkendall64 merged 1 commit into
ExpressLRS:masterfrom
georgebashi:fix/dynpower-headroom-underflow
May 19, 2026
Merged

pkendall64 merged 1 commit into
ExpressLRS:masterfrom
georgebashi:fix/dynpower-headroom-underflow

Conversation

@georgebashi

Copy link
Copy Markdown
Contributor

Problem

When a user lowers their configured max power via Lua script while armed with dynamic power enabled, POWERMGNT::currPower() can exceed config.GetPower(). The unsigned subtraction on line 117 of dynpower.cpp:

  uint8_t powerHeadroom = (uint8_t)config.GetPower() - (uint8_t)POWERMGNT::currPower();

wraps to ~255 (uint8_t underflow), causing the powerHeadroom > 0 guards to pass. This allows incPower() to ramp transmit power up to hardware maximum, exceeding the user's configured limit.

On FCC 915MHz and ISM 2400 (non-EU) builds, there is no compile-time cap, so power can reach the hardware maximum. EU CE 2400 builds have a getMaxPower() cap at 100mW which partially mitigates the issue.

Fix

Guard the subtraction so powerHeadroom is 0 when currPower >= configPower:

  uint8_t configPower = (uint8_t)config.GetPower();
  uint8_t currPower = (uint8_t)POWERMGNT::currPower();
  uint8_t powerHeadroom = (configPower > currPower) ? configPower - currPower : 0;

How to reproduce

  1. Set dynamic power enabled, configure max power to e.g. 250mW
  2. Arm and fly — dynamic power raises output to 250mW
  3. While still armed, lower configured max power to 25mW via Lua
  4. Observe: power ramps to hardware max instead of staying at or below 25mW

When a user lowers their configured max power via Lua while armed,
currPower can exceed config.GetPower(). The unsigned subtraction
wraps to ~255, bypassing the powerHeadroom > 0 guard and allowing
dynamic power to ramp output up to hardware maximum.

Guard the subtraction so powerHeadroom is 0 when currPower >= config.

@mha1 mha1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch

@pkendall64
pkendall64 merged commit 9fb8e2c into ExpressLRS:master May 19, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants