Skip to content

kernel: sched: fix possible integer overflow in z_tick_sleep() #92996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions kernel/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -1117,13 +1117,11 @@ static int32_t z_tick_sleep(k_timeout_t timeout)
}

/* We require a 32 bit unsigned subtraction to care a wraparound */
uint32_t left_ticks = expected_wakeup_ticks - sys_clock_tick_get_32();
uint32_t now = sys_clock_tick_get_32();
int32_t remaining = (int32_t)(expected_wakeup_ticks - now);

/* To handle a negative value correctly, once type-cast it to signed 32 bit */
k_ticks_t ticks = (k_ticks_t)(int32_t)left_ticks;

if (ticks > 0) {
return ticks;
if (remaining > 0) {
return (k_ticks_t)remaining;
}

return 0;
Expand Down