Skip to content

Commit ce4c582

Browse files
authored
Fix crash computing zero(ZZ[t]) - zero(ZZ) (#1930)
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`.
1 parent 2de7b3a commit ce4c582

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/flint/fmpz_poly.jl

+7-1
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,13 @@ function sub!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::ZZPolyRingElemO
875875
end
876876

877877
function sub!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::ZZRingElemOrPtr)
878-
@ccall libflint.fmpz_poly_sub_fmpz(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Ref{ZZRingElem})::Nothing
878+
if is_zero(y)
879+
# HACK HACK HACK: workaround a crash in fmpz_poly_sub_fmpz when subtracting
880+
# 0 from a zero polynomial; see https://github.com/flintlib/flint/pull/2102
881+
set!(z, x)
882+
else
883+
@ccall libflint.fmpz_poly_sub_fmpz(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Ref{ZZRingElem})::Nothing
884+
end
879885
return z
880886
end
881887

test/flint/fmpz_poly-test.jl

+3
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ end
152152
@test 12 - g == -x^3-3*x+10
153153

154154
@test ZZRingElem(12) - g == -x^3-3*x+10
155+
156+
# verify bugfix (used to crash)
157+
@test is_zero(zero(R) - ZZ(0))
155158
end
156159

157160
@testset "ZZPolyRingElem.comparison" begin

0 commit comments

Comments
 (0)