Skip to content

rustc: Avoid UB with signed division/remainder #14681

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

Closed
wants to merge 1 commit into from

Conversation

alexcrichton
Copy link
Member

Division and remainder by 0 are undefined behavior, and are detected at runtime.
This commit adds support for ensuring that MIN / -1 is also checked for at
runtime, as this would cause signed overflow, or undefined behvaior.

Closes #8460

// example, by doing a 32-bit division of -2147483648 by -1.
//
// In order to avoid undefined behavior, we perform runtime checks for
// signed division/remainder which would trigger overflow. For signed
Copy link
Member

Choose a reason for hiding this comment

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

Missing an "un" on the last word here :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, thanks!

@sfackler
Copy link
Member

sfackler commented Jun 5, 2014

Looks like some tests depended on UB!

Division and remainder by 0 are undefined behavior, and are detected at runtime.
This commit adds support for ensuring that MIN / -1 is also checked for at
runtime, as this would cause signed overflow, or undefined behvaior.

Closes rust-lang#8460
@alexcrichton
Copy link
Member Author

Oops, my condition was wrong as rhs == 0 || lhs == MIN || rhs == -1 when it should have been rhs == 0 || (lhs == MIN && rhs == -1). The tests weren't actually relying on undefined behavior. (fixed now).

@bors bors closed this in e87e180 Jun 7, 2014
@alexcrichton alexcrichton deleted the issue-8460 branch June 7, 2014 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

signed division has undefined behaviour on overflow
3 participants