Skip to content

Commit

Permalink
include: sys: time_units: fix 32-bit near conversion for overflow
Browse files Browse the repository at this point in the history
Adjusting the input value to allow round to nearest can cause an
overflow which invalidates the expectation that the 32-bit result is
the low 32 bits of the 64-bit result.  If the adjustment overflows do
the full-precision conversion and truncate in the caller.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
  • Loading branch information
pabigot authored and carlescufi committed Jun 2, 2020
1 parent 7ff3f8a commit 76d8f8f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/sys/time_units.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static TIME_CONSTEXPR ALWAYS_INLINE u64_t z_tmcvt(u64_t t, u32_t from_hz,
*/
if (div_ratio) {
t += off;
if (result32) {
if (result32 && (t < BIT64(32))) {
return ((u32_t)t) / (from_hz / to_hz);
} else {
return t / (from_hz / to_hz);
Expand Down
1 change: 1 addition & 0 deletions tests/kernel/timer/timer_api/src/timer_convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ void test_time_conversions(void)
test_conversion(&tests[i], 1);
test_conversion(&tests[i], 0x7fffffff);
test_conversion(&tests[i], 0x80000000);
test_conversion(&tests[i], 0xfffffff0);
if (tests[i].precision == 64) {
test_conversion(&tests[i], 0xffffffff);
test_conversion(&tests[i], 0x100000000ULL);
Expand Down

0 comments on commit 76d8f8f

Please sign in to comment.