Skip to content

equeue: Fix overflow in rtos-less timeout code #7782

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

Merged
merged 1 commit into from
Aug 16, 2018
Merged
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
2 changes: 1 addition & 1 deletion events/equeue/equeue_mbed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ bool equeue_sema_wait(equeue_sema_t *s, int ms) {
if (ms == 0) {
return false;
} else if (ms > 0) {
timeout.attach_us(callback(equeue_sema_timeout, s), ms*1000);
timeout.attach_us(callback(equeue_sema_timeout, s), (us_timestamp_t)ms*1000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be (us_timestamp_t)(ms * 1000) ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would it be ? result of the expression (ms * 1000) is int which means it is already truncated if it has overflowed.
We want to promote either ms or 1000 to make the calculation happen in the us_timestamp_t number system.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, we can cast ms to us first and then do the multiplication, though I think what i suggested is clearer to the intention but I don't care that much tbh :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must be missing something but what you suggested is very different and produce different result; see here

}

core_util_critical_section_enter();
Expand Down