Skip to content

Commit

Permalink
Fix crash computing zero(ZZ[t]) - zero(ZZ)
Browse files Browse the repository at this point in the history
The following lead to a crash (uncovered by Nemocas/AbstractAlgebra.jl#1867)
```julia
using Nemo
R, t = ZZ[:t]
zero(R) - ZZ(0)
```
This is due to a bug in FLINT's `fmpz_poly_sub_fmpz`.
  • Loading branch information
fingolfin committed Oct 28, 2024
1 parent 9dd324d commit 5544d49
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/flint/fmpz_poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,13 @@ function sub!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::ZZPolyRingElemO
end

function sub!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::ZZRingElemOrPtr)
@ccall libflint.fmpz_poly_sub_fmpz(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Ref{ZZRingElem})::Nothing
if is_zero(y)
# HACK HACK HACK: workaround a crash in fmpz_poly_sub_fmpz when subtracting
# 0 from a zero polynomial; see https://github.com/flintlib/flint/pull/2102
set!(z, x)
else
@ccall libflint.fmpz_poly_sub_fmpz(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Ref{ZZRingElem})::Nothing
end
return z
end

Expand Down
3 changes: 3 additions & 0 deletions test/flint/fmpz_poly-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ end
@test 12 - g == -x^3-3*x+10

@test ZZRingElem(12) - g == -x^3-3*x+10

# verify bugfix (used to crash)
@test is_zero(zero(R) - ZZ(0))
end

@testset "ZZPolyRingElem.comparison" begin
Expand Down

0 comments on commit 5544d49

Please sign in to comment.