Skip to content

Commit feefb7d

Browse files
akasonakartben
authored andcommitted
kernel/sched: correct k_sleep() return value calculation
Fix the issue: #79863 The expected_wakeup_ticks and sys_clock_tick_get_32() are uint32_t values, and may wrap around individually. If the expected_wakeup_ticks has a wraparound and sys_clock_tick_get_32() doesn't, so expected_wakeup_ticks < sys_clock_tick_get_32(), the API return value will be corrupted. The API return value, that is the remaining time, should be calculated in 32bit-unsigned-integer manner, and any wraparound will be treated properly. Signed-off-by: Akaiwa Wataru <[email protected]>
1 parent 4fb6ce3 commit feefb7d

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

kernel/sched.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,11 @@ static int32_t z_tick_sleep(k_ticks_t ticks)
11171117

11181118
__ASSERT(!z_is_thread_state_set(arch_current_thread(), _THREAD_SUSPENDED), "");
11191119

1120-
ticks = (k_ticks_t)expected_wakeup_ticks - sys_clock_tick_get_32();
1120+
/* We require a 32 bit unsigned subtraction to care a wraparound */
1121+
uint32_t left_ticks = expected_wakeup_ticks - sys_clock_tick_get_32();
1122+
1123+
/* To handle a negative value correctly, once type-cast it to signed 32 bit */
1124+
ticks = (k_ticks_t)(int32_t)left_ticks;
11211125
if (ticks > 0) {
11221126
return ticks;
11231127
}

0 commit comments

Comments
 (0)