Skip to content

Rust programs crash when dividing by zero #944

Description

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

SIGFPE / Floating point exception

Is this the intended behavior?

Activity

  1. jruderman commented on Sep 20, 2011

    @jruderman
    ContributorAuthor

    Task failure would be friendlier.

  2. mbrubeck commented on Nov 2, 2011

    @mbrubeck
    Contributor

    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.

  3. graydon commented on Feb 15, 2012

    @graydon
    Contributor

    Should trap SIGFPE and route to failure. Runtime issue.

  4. added a commit that references this issue on Mar 7, 2023
  5. added 2 commits that reference this issue on Aug 21, 2026
  6. added a commit that references this issue on Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-runtimeArea: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflowsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions