From 0fcc4cde9e47709c37d8c7cf92db33e76ef022a5 Mon Sep 17 00:00:00 2001 From: Johannes Schmitt Date: Fri, 4 Oct 2024 14:32:54 +0200 Subject: [PATCH] Throw an error in case of division by zero instead of crashing hard (#1828) --- src/julia/Integer.jl | 3 +++ test/julia/Integers-test.jl | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/julia/Integer.jl b/src/julia/Integer.jl index 18abe39122..9632609ad1 100644 --- a/src/julia/Integer.jl +++ b/src/julia/Integer.jl @@ -177,6 +177,7 @@ function divexact(a::Integer, b::Integer; check::Bool=true) end function divexact(a::BigInt, b::BigInt; check::Bool=true) + iszero(b) && throw(DivideError()) q = BigInt() if check r = BigInt() @@ -191,6 +192,7 @@ function divexact(a::BigInt, b::BigInt; check::Bool=true) end function divexact(a::BigInt, b::Int; check::Bool=true) + iszero(b) && throw(DivideError()) q = BigInt() sgn = b < 0 if check @@ -206,6 +208,7 @@ function divexact(a::BigInt, b::Int; check::Bool=true) end function divexact(a::BigInt, b::UInt; check::Bool=true) + iszero(b) && throw(DivideError()) q = BigInt() if check r = BigInt() diff --git a/test/julia/Integers-test.jl b/test/julia/Integers-test.jl index a9c6bd2e1b..b55d96d7c2 100644 --- a/test/julia/Integers-test.jl +++ b/test/julia/Integers-test.jl @@ -66,6 +66,13 @@ end @test_throws ArgumentError divexact(big(10), big(4)) @test_throws ArgumentError divexact(big(10), 4) + @test_throws DivideError divexact(big(10), big(0)) + @test_throws DivideError divexact(big(10), big(0), check = false) + @test_throws DivideError divexact(big(10), 0) + @test_throws DivideError divexact(big(10), 0, check = false) + @test_throws DivideError divexact(big(10), UInt(0)) + @test_throws DivideError divexact(big(10), UInt(0), check = false) + for iter = 1:1000 a1 = rand(R, -100:100) a2 = rand(R, -100:100)