Skip to content
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

Rust programs crash when dividing by zero #944

Closed
jruderman opened this issue Sep 20, 2011 · 3 comments
Closed

Rust programs crash when dividing by zero #944

jruderman opened this issue Sep 20, 2011 · 3 comments
Labels
A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
Milestone

Comments

@jruderman
Copy link
Contributor

fn main() {
    let y = 0;
    assert 1 / y != 0;
}

SIGFPE / Floating point exception

Is this the intended behavior?

@jruderman
Copy link
Contributor Author

Task failure would be friendlier.

@mbrubeck
Copy link
Contributor

mbrubeck commented Nov 2, 2011

Rust uses the LLVM sdiv and udiv instructions for integer division, which have undefined results for division by zero. Using the current Rust compiler built from master on my 32-bit Linux machine, fn main() { log_err 1/0 } prints 0 when compiled with optimization. When compiled without optimization, it yields different results each time it is executed, for example 139268064.

I'm kind of suprised by both of these results; I'd have expected a hardware exception like the one that jruderman reported. Does clang do anything different for integer division? See this thread for some discussion (but no current solutions) from the LLVM point of view.

@graydon
Copy link
Contributor

graydon commented Feb 15, 2012

Should trap SIGFPE and route to failure. Runtime issue.

ZuseZ4 pushed a commit to EnzymeAD/rust that referenced this issue Mar 7, 2023
coastalwhite pushed a commit to coastalwhite/rust that referenced this issue Aug 5, 2023
The implementation is the same (where possible), and it unblocks rust-lang#904

Signed-off-by: Joe Richey <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
Projects
None yet
Development

No branches or pull requests

3 participants