[riot-notifications] [RIOT-OS/RIOT] periph/rtt: fix overflow in tick conversion macros (#15431)
notifications at github.com
Wed Nov 11 17:11:24 CET 2020
### Contribution description
The tick conversion macros provided by the `periph/rtt` driver are subject to overflow for large values. E.g. when doing something like
uint32_t ticks = RTT_TICKS_TO_MS(RTT_MAX_VALUE + 1);
// with RTT_MAX_VALUE = 0x00ffffff -> 24 bit counter
// and RTT_FREQUENCY 1024 -> e.g. on the nrf52dk
the resulting tick value will be wrong. Reason is this line:
#define RTT_TICKS_TO_US(ticks) ((uint32_t)((uint64_t)(ticks) * 1000000UL / RTT_FREQUENCY))
-> the value of `ticks * 10000000 / RTT_FREQUENCY` is larger then `UINT32_MAX`, and therefore overflows. And as this macro is used in a `RTT_TICKS_TO_MIN() -> RTT_TICKS_TO_SEC() -> RTT_TICKS_TO_MS() -> RTT_TICKS_TO_US()` cascade, all resulting values will be flawed.
The same goes for the time-to-tick conversion the other way around...
This PR fixes this by omitting the explicit cast to `uint32_t` in both directions. Now when assiging `uint32_t foo = RTT_TICKS_TO_US()` with a (static) value larger then `UINT32_MAX` the compiler will shout at you :-)
Also added some basic tests for the tick conversion to the test application.
### Testing procedure
Run the included version of the test application on any platform of your choice, it should fail. Run it with the fix included in this PR -> it should run successfully..
### Issues/PRs references
You can view, comment on, or merge this pull request online at:
-- Commit Summary --
* periph/rtt: fix tick conversion macros
* tests/periph_rtt: add test for tick conversions
-- File Changes --
M drivers/include/periph/rtt.h (10)
M tests/periph_rtt/main.c (21)
-- Patch Links --
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the notifications