Commit dae8157
committed
fix(IWatchdog): guard reload/window register computation against underflow
IWatchdogClass::set() computes the IWDG reload register with
reload = (uint32_t)(t_sec / div) - 1;
(and the equivalent for the optional 'window' register), with no check
that (uint32_t)(t_sec / div) is non-zero before subtracting 1.
IWDG_TIMEOUT_MIN, the documented and enforced lower bound for the
'timeout' argument, is defined as (4*1000000)/LSI_VALUE using integer
division. On most series (LSI_VALUE 32000 or 40000) this divides evenly,
so calling begin(IWDG_TIMEOUT_MIN) is always safe. On STM32L0xx/STM32L1xx
(LSI_VALUE = 37000), 4000000/37000 is not an integer (108.108...), so the
macro truncates down to 108 -- a value IS_IWDG_TIMEOUT() accepts, but
that is below the true precision needed: t_sec works out to 3.996 there,
so (uint32_t)(t_sec / 4) floors to 0, and "0 - 1" underflows reload to
0xFFFFFFFF. LL_IWDG_SetReloadCounter() then masks that into the 12-bit
RLR register as 0x0FFF (4095), the maximum possible value, instead of 0.
Net effect: on STM32L0/L1, IWatchdog.begin(IWDG_TIMEOUT_MIN) -- the
shortest legal, documented watchdog timeout -- silently programs a
timeout of about 443 ms instead of about 108 us (roughly 4100x longer),
with no error indication. The same underflow shape is reachable through
the optional 'window' parameter for the same reason.
Guard both computations with an explicit "ticks > 0" check before
subtracting, returning 0 (the safe minimum reload) instead of
underflowing, without changing the public API or behavior on any series
where the computation was already safe.
Signed-off-by: 94xhn <87560781+94xhn@users.noreply.github.com>1 parent 46d4cc5 commit dae8157
1 file changed
Lines changed: 14 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
97 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
98 | 108 | | |
99 | 109 | | |
100 | 110 | | |
| |||
111 | 121 | | |
112 | 122 | | |
113 | 123 | | |
114 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
115 | 127 | | |
116 | 128 | | |
117 | 129 | | |
| |||
0 commit comments